图片是程序对应的图片,多边形的点数可以输入的,合法输入:大于三的正整数,但是当点数大于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[n].x,pt[n].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);
}
相关知识
OpenGL 画荷花
怎么画圣诞花环?教你绘制简易圣诞花环的画法!
TensorFlow学习记录(八)
概念、手算、软件设置,带你全方位彻底搞定「梅花形布置」
cdr椭圆形工具怎么绘制创意的花环图形?
illustrator绘制花朵图案的教程
java基础判断一个数是不是水仙花数
那些年曾经看过的书——工作篇
python turtle玫瑰花绘制效果和源代码
Tutorial:花朵种植
网址: opengl绘制花环(点的个数可以手动输入) 源代码 https://m.huajiangbk.com/newsview566118.html
上一篇: 儿童画漂亮的小花怎么画? |
下一篇: 杭州园艺作品又获奖了!“钱塘小院 |