绘制钻石图案。将一圆周进行n等分,然后将所有的等分用直线连接,构成图案。
例如:
n=8
q =2*p/n
X=2*r*cos(q)
Y=r*sin(q)
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define pi 3.14159265
int n=8;
int R=200,CX=250,CY=250;
int *point;
void initGraph(void);
void drawCircle(int cx, int cy, int r);
void display(void);
void drawLine(int x1, int y1, int x2, int y2);
void main(int argc, char **argv){
float w,wi;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB) ;
glutInitWindowSize(500,500);
glutCreateWindow("n阶钻石图案");
initGraph();
point=(int*)malloc(sizeof(int)*n*2);//申请存储空间
w=2*pi/n;
wi=w/2;
for(int i=0;i<2*n;i+=2){
point[i]=(int)(CX+R*cos(wi));
point[i+1]=(int)(CY+R*sin(wi));
wi+=w;
}
glutDisplayFunc(display);
glutMainLoop();
if(point!=NULL)free(point);//释放存储空间
}
void initGraph (void){
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0.0,500.0,0.0,500.0);//窗口坐标左下角(0,0), 右上角(500,500)
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT) ;
glColor3f(1.0, 1.0, 0.0) ;
for(int i=0;i<2*n-2;i+=2){
for(int j=i+2;j<2*n;j+=2){
drawLine(point[i],point[i+1],point[j],point[j+1]);
}
}
}
void drawLine(int x1, int y1, int x2, int y2) {
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
glFlush() ;
}