您的位置:首页 > 编程语言

opengl绘制花环(点的个数可以手动输入) 源代码

2013-10-24 13:47 162 查看
/*之前绘制的花环没有附上源码,现在附上,只希望当有人需要绘制的时候,以及用到相关数据结构时,不要花像我一样长的时间,不过我当时也问老师了就是的了。

图片是程序对应的图片,多边形的点数可以输入的,合法输入:大于三的正整数,但是当点数大于45时,会出现缺口,不知道是什么原因,如果您知道是什么原因,请留言。本人感激不尽(予人玫瑰,手有余香)*/

#include <Windows.h>

#include<gl/glut.h>

#include <math.h>

#include <vector>

#include <iostream>

using namespace std;

const GLint screenWidth = 620;

const GLint screenHeight = 620;

const GLint xPosition = 100;

const GLint yPosition = 100;

const GLdouble PI = 3.1415926;

static GLint numPoint = 0;

static GLint size = 300;

struct GLPoint{

GLdouble x ,y;

};

//花环

/************************************************************************/

/* 三角形:180°,矩形:360°,五边形:540°,六边形:720°,七边形:900°

1*180, 2*180, 3*180 4*180, 5*180

( n -2 ) * 180 n>2,n∈正整数

相邻两点分别与中心连线,夹角<AOB = 360 / numPoint;

三角形 180, 四边形 90 ..... */

/************************************************************************/

void myDisplay(void);

void myInit(void);

void setWindow(GLdouble left, GLdouble right, GLdouble botton, GLdouble top);

void setViewport(GLint left, GLint right, GLint botton, GLint top);

int main(int argc, char ** argv){

glutInit(&argc,argv);

cout<<"多边形顶点数:";

cin>>numPoint;

glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

glutInitWindowSize(screenWidth,screenHeight);

glutInitWindowPosition(xPosition,yPosition);

glutCreateWindow("Rosette");

glutDisplayFunc(myDisplay);

myInit();

setWindow(-310,310,-310,310);

setViewport(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);

glutMainLoop();

return 0;

}

void setWindow(GLdouble left, GLdouble right, GLdouble botton, GLdouble top){

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D( left, right, botton, top);

}

void setViewport(GLint left, GLint right, GLint botton, GLint top){

glViewport(left,botton,right-left,top-botton);

}

void myDisplay(void){

glClear(GL_COLOR_BUFFER_BIT);

if (numPoint<3)

{

return;

}

vector<GLPoint> pt;

//GLPoint topPoint = {0,300};

cout<<"pt.size() = "<<pt.size()<<endl;

GLdouble angle =(GLdouble) PI*(360 / numPoint)/180.0;

for (int i = 0; i< numPoint; i++)

{

GLPoint *pp = new GLPoint();

/*pp->x = (GLdouble)topPoint.x*sin(angle*i);

pp->y = (GLdouble)topPoint.y*cos(angle*i);*/

pp->x = (GLdouble)size*sin(angle*i) /* * A + C*/;

pp->y = (GLdouble)size*cos(angle*i) /** B + D*/;

cout<<"pt->x = "<<pp->x<<" pt->y = "<<pp->y<<endl;

pt.push_back(*pp);

}

cout<<"pt.size() = "<<pt.size()<<endl;

////多边形

//glBegin(GL_LINE_LOOP);

// for (int j = 0; j< numPoint; j++)

// {

// glVertex2d(pt[j].x,pt[j].y);

// }

//glEnd();

//连线

glBegin(GL_LINES);

for( int m = 0; m <numPoint; m++ ){

for ( int n = m+1 ; n < numPoint; n++)

{

glVertex2d(pt[m].x,pt[m].y);

glVertex2d(pt
.x,pt
.y);

}

}

glEnd();

glFlush();

}

void myInit(void){

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.0f,0.0f,0.0f);

glLineWidth(2.0);

}

//下图为20个顶点的花环

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐