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个顶点的花环
图片是程序对应的图片,多边形的点数可以输入的,合法输入:大于三的正整数,但是当点数大于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个顶点的花环
相关文章推荐
- 打印乘法口诀表(可以手动输入要打印的为几乘几的口诀表)
- ifstream类用法入门 (可以避免在控制台上手动输入数据)
- SG公务车:用车人职务级别改成可以手动输入(续)
- OpenGL源代码之多边形的绘制
- 自己封装的三维AABB包围盒类,用于三维模型静态碰撞检测,可以直接调用,使用OpenGL进行绘制
- winform自定义日期控件,要求可以手动输入日期DatePicker
- MFC中可以实现OpenGL跨进程绘制
- OpenGL源代码之五绘制3D图形
- 可以移动的恐龙--用键盘让恐龙具有生命 dino.dat文件绘制恐龙 a d w s移动恐龙 源代码
- SG公务车:用车人职务级别改成可以手动输入
- xpages的comboBox可以手动输入
- input框 既可以手动输入亦可以进行下拉模糊查找
- OpenGL_ES 创建一个平面 绘制纹理 响应输入事件
- 使NumericStepper组件调节的值可以/不可以手动输入.
- SG公务车:用车人职务级别改成可以手动输入(续:不删表数据,修改字段类型)
- 源代码:用层DIV模拟的类似C/S模式下的小窗口(可以拖动,展开关闭)
- 12-21C#电脑蓝屏效果(可以恶搞整人哦)、输入输出流(StreamReader/streamWriter)
- 文本框里显示提示语言,点击可以输入其他内容
- 字符串输入以空格、回车为终止 但回车可以被后面的getchar接收
- 1. webservice在输入命令的时候wsimport的时候会出现如下错误: wsimport不是内部或者外部命令。 2. javac不是内部或者外部命令 3 java 就可以显示配置成功。