- 注册时间
- 2009-2-12
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 22697
- 在线时间
- 小时
|
发表于 2011-1-13 20:24:28
|
显示全部楼层
4# forcal
算了,再给你补一贴吧:
用OpenGL 画你给的那个函数:
代码:- /* generates contours using marching squares */
-
- /* region size */
-
- #define X_MAX 8.0
- #define Y_MAX 8.0
- #define X_MIN -8.0
- #define Y_MIN -8.0
-
- /* number of cells */
-
- #define N_X 500
- #define N_Y 500
-
- /* contour value */
-
- #define THRESHOLD 0.0
-
- #include <GL/glut.h>
-
- void display()
- {
- double f(double,double);
- int cell(double, double, double, double);
- void lines(int, int, int, double, double, double, double);
-
- double data[N_X][N_Y];
- int i,j;
- int c;
-
- glClear(GL_COLOR_BUFFER_BIT);
- /* form data array from function */
-
- for(i=0;i<N_X;i++) for (j=0;j<N_Y;j++)
- data[i][j]=f(X_MIN+i*(X_MAX-(X_MIN))/(N_X-1.0), Y_MIN+j*(Y_MAX-(Y_MIN))/(N_Y-1.0));
-
- /* process each cell */
-
- for(i=0;i<N_X;i++) for (j=0;j<N_Y;j++)
- {
- c=cell(data[i][j], data[i+1][j], data[i+1][j+1], data[i][j+1]);
- lines(c,i,j,data[i][j], data[i+1][j], data[i+1][j+1], data[i][j+1]);
- }
- glFlush();
- }
-
- /* define function f(x,y) */
-
- double f(double x, double y)
- {
-
- double a=36;
-
- /* Ovals of Cassini */
-
- return (x*x+y*y)*(x*x+y*y)*(x*x+y*y)-a*(x*x-y*y)*(x*x-y*y);
- }
-
-
- /* define cell vertices */
-
- int cell(double a, double b, double c , double d)
- {
- int n=0;
- if(a>THRESHOLD) n+=1;
- if(b>THRESHOLD) n+=8;
- if(c>THRESHOLD) n+=4;
- if(d>THRESHOLD) n+=2;
- return n;
- }
-
-
- /* draw line segments for each case */
-
- void lines(int num, int i, int j, double a, double b, double c, double d)
- {
- void draw_one(int, int, int, double, double, double, double);
- void draw_adjacent(int, int, int, double, double, double, double);
- void draw_opposite(int, int, int, double, double, double, double);
- switch(num)
- {
- case 1: case 2: case 4: case 7: case 8: case 11: case 13: case 14:
- draw_one(num, i,j,a,b,c,d);
- break;
- case 3: case 6: case 9: case 12:
- draw_adjacent(num,i,j,a,b,c,d);
- break;
- case 5: case 10:
- draw_opposite(num, i,j,a,b,c,d);
- break;
- case 0: case 15: break;
- }
- }
-
- void draw_one(int num, int i, int j, double a, double b, double c, double d)
- {
- double x1, y1, x2, y2;
- double ox, oy;
- double dx, dy;
- dx=(X_MAX-(X_MIN))/(N_X-1.0);
- dy=(Y_MAX-(Y_MIN))/(N_Y-1.0);
- ox=X_MIN+i*(X_MAX-(X_MIN))/(N_X-1.0);
- oy=Y_MIN+j*(Y_MAX-(Y_MIN))/(N_Y-1.0);
- switch(num)
- {
- case 1: case 14:
- x1=ox;
- y1=oy+dy*(THRESHOLD-a)/(d-a);
- x2=ox+dx*(THRESHOLD-a)/(b-a);
- y2=oy;
- break;
- case 2: case 13:
- x1=ox;
- y1=oy+dy*(THRESHOLD-a)/(d-a);
- x2=ox+dx*(THRESHOLD-d)/(c-d);
- y2=oy+dy;
- break;
- case 4: case 11:
- x1=ox+dx*(THRESHOLD-d)/(c-d);
- y1=oy+dy;
- x2=ox+dx;
- y2=oy+dy*(THRESHOLD-b)/(c-b);
- break;
- case 7: case 8:
- x1=ox+dx*(THRESHOLD-a)/(b-a);
- y1=oy;
- x2=ox+dx;
- y2=oy+dy*(THRESHOLD-b)/(c-b);
- break;
- }
- glBegin(GL_LINES);
- glVertex2d(x1, y1);
- glVertex2d(x2, y2);
- glEnd();
- }
-
- void draw_adjacent(int num, int i, int j, double a, double b,
- double c, double d)
- {
- double x1, y1, x2, y2;
- double ox, oy;
- double dx, dy;
- dx=(X_MAX-(X_MIN))/(N_X-1.0);
- dy=(Y_MAX-(Y_MIN))/(N_Y-1.0);
- ox=X_MIN+i*(X_MAX-(X_MIN))/(N_X-1.0);
- oy=Y_MIN+j*(Y_MAX-(Y_MIN))/(N_Y-1.0);
- switch(num)
- {
- case 3: case 12:
- x1=ox+dx*(THRESHOLD-a)/(b-a);
- y1=oy;
- x2=ox+dx*(THRESHOLD-d)/(c-d);
- y2=oy+dy;
- break;
- case 6: case 9:
- x1=ox;
- y1=oy+dy*(THRESHOLD-a)/(d-a);
- x2=ox+dx;
- y2=oy+dy*(THRESHOLD-b)/(c-b);
- break;
- }
- glBegin(GL_LINES);
- glVertex2d(x1, y1);
- glVertex2d(x2, y2);
- glEnd();
-
- }
-
- void draw_opposite(int num, int i, int j, double a, double b,
- double c, double d)
- {
- double x1,y1,x2,y2,x3,y3,x4,y4;
- double ox, oy;
- double dx, dy;
- dx=(X_MAX-(X_MIN))/(N_X-1.0);
- dy=(Y_MAX-(Y_MIN))/(N_Y-1.0);
- ox=X_MIN+i*(X_MAX-(X_MIN))/(N_X-1.0);
- oy=Y_MIN+j*(Y_MAX-(Y_MIN))/(N_Y-1.0);
- switch(num)
- {
- case5:
- x1=ox;
- y1=oy+dy*(THRESHOLD-a)/(d-a);
- x2=ox+dx*(THRESHOLD-a)/(b-a);
- y2=oy;
- x3=ox+dx*(THRESHOLD-d)/(c-d);
- y3=oy+dy;
- x4=ox+dx;
- y4=oy+dy*(THRESHOLD-b)/(c-b);
- break;
- case 10:
- x1=ox;
- y1=oy+dy*(THRESHOLD-a)/(d-a);
- x2=ox+dx*(THRESHOLD-d)/(c-d);
- y2=oy+dy;
- x3=ox+dy*(THRESHOLD-a)/(b-a);
- y3=oy;
- x4=ox+dx;
- y4=oy+dy*(THRESHOLD-b)/(c-b);
- break;
- }
- glBegin(GL_LINES);
- glVertex2d(x1, y1);
- glVertex2d(x2, y2);
- glVertex2d(x3, y3);
- glVertex2d(x4, y4);
- glEnd();
- }
-
- void myReshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- if (w <= h) gluOrtho2D(X_MIN, X_MAX, Y_MIN * (GLfloat) h / (GLfloat) w, Y_MAX* (GLfloat) h / (GLfloat) w);
- else gluOrtho2D(X_MIN * (GLfloat) w / (GLfloat) h, X_MAX * (GLfloat) w / (GLfloat) h, Y_MIN, Y_MAX);
- glMatrixMode(GL_MODELVIEW);
- }
-
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitWindowSize(500, 500);
- glutCreateWindow("contour plot");
- glutReshapeFunc(myReshape);
- glutDisplayFunc(display);
- glClearColor(0.0,0.0,0.0,1.0);
- glColor3f(1.0,1.0,1.0);
- glutMainLoop();
- }
-
复制代码 |
|