繪製鑽石圖案(openGL和C++)
首先是環境配置,檔案自己下,具體配置看自己的環境,以下是範例
copyglut.h"C:/Program Files/Microsoft Visual Studio/VC98/Include/GL"
copyglut32.lib"C:/Program Files/Microsoft Visual Studio/VC98/Lib"
copyglut32.dll"C:/WINDOWS/system32"
以下是原始碼
#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() ;
}
以下是範例圖片