#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>class Point{
public:
GLint x;
GLint y;
};Point point[10];
int gi = 0;
int DrawCircle(Point *pPoint, float t, int n);
void myInit(void);
void myDisplay(void);
void myMouse(int button, int state, int x, int y);int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设定显示模式为单个buffer和RGB模式
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Bezier"); myInit();
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutMainLoop(); return 0;
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);//设定背景刷新颜色
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0);//设定点的大小为1个像素
glMatrixMode(GL_PROJECTION);//设定投射方式
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
/*static int i = 0;
printf("%d", i++);*/
/*glBegin(GL_POINTS);
for(int i = 0; i < gi; i++)
glVertex2i(point[i].x, point[i].y);
glEnd();
glFinish();
if(gi > 0)
DrawCircle(point, 0.5, gi);*/
glFlush();
}
void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)//记录鼠标点击的位置
{
point[gi].x = x;
point[gi].y = 480 - y;
glBegin(GL_POINTS);
glVertex2i(point[gi].x, point[gi].y);
glEnd();
glFinish();
gi++;
}
if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
//glClear(GL_COLOR_BUFFER_BIT);
//glutPostRedisplay();
DrawCircle(point, 0.8, gi);
gi = 0;
}
}
int DrawCircle(Point *pPoint, float t, int n)
{
Point point1[10], point2[10]; //int dis = (pPoint[n].x - pPoint[0].x) * (pPoint[n].x - pPoint[0].x)
// + (pPoint[n].y - pPoint[0].y) * (pPoint[n].y - pPoint[0].y);
if((abs(pPoint[n - 1].x - pPoint[0].x) < n - 1) && (abs(pPoint[n - 1].y - pPoint[0].y) < n - 1))
{
glBegin(GL_POINTS);
for(int i = 0; i < n; i++)
glVertex2i(pPoint[i].x, pPoint[i].y);
glEnd();
/*glBegin(GL_LINES);
glVertex2i(pPoint[0].x, pPoint[0].y);
glVertex2i(pPoint[n - 1].x, pPoint[n - 1].y);
glEnd();*/ glFlush();
return 0;
}
for(int i = 0; i < n; i++)
{
point1[i] = pPoint[i];
point2[i] = pPoint[i];
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < n - i; j++)
{
point2[j].x = (1 - t) * point2[j].x + t * point2[j + 1].x;
point2[j].y = (1 - t) * point2[j].y + t * point2[j + 1].y;
}
point1[i] = point2[0];
}
DrawCircle(point1, t, n);
DrawCircle(point2, t, n);
}我觉得是最后各个点都相连了然后只需要画这些点就好了,可是我这样好像有问题,那个会有栈溢出
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>class Point{
public:
GLint x;
GLint y;
};Point point[10];
int gi = 0;
int DrawCircle(Point *pPoint, float t, int n);
void myInit(void);
void myDisplay(void);
void myMouse(int button, int state, int x, int y);int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设定显示模式为单个buffer和RGB模式
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Bezier"); myInit();
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutMainLoop(); return 0;
}
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);//设定背景刷新颜色
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0);//设定点的大小为1个像素
glMatrixMode(GL_PROJECTION);//设定投射方式
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
/*static int i = 0;
printf("%d", i++);*/
/*glBegin(GL_POINTS);
for(int i = 0; i < gi; i++)
glVertex2i(point[i].x, point[i].y);
glEnd();
glFinish();
if(gi > 0)
DrawCircle(point, 0.5, gi);*/
glFlush();
}
void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)//记录鼠标点击的位置
{
point[gi].x = x;
point[gi].y = 480 - y;
glBegin(GL_POINTS);
glVertex2i(point[gi].x, point[gi].y);
glEnd();
glFinish();
gi++;
}
if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
//glClear(GL_COLOR_BUFFER_BIT);
//glutPostRedisplay();
DrawCircle(point, 0.8, gi);
gi = 0;
}
}
int DrawCircle(Point *pPoint, float t, int n)
{
Point point1[10], point2[10]; //int dis = (pPoint[n].x - pPoint[0].x) * (pPoint[n].x - pPoint[0].x)
// + (pPoint[n].y - pPoint[0].y) * (pPoint[n].y - pPoint[0].y);
if((abs(pPoint[n - 1].x - pPoint[0].x) < n - 1) && (abs(pPoint[n - 1].y - pPoint[0].y) < n - 1))
{
glBegin(GL_POINTS);
for(int i = 0; i < n; i++)
glVertex2i(pPoint[i].x, pPoint[i].y);
glEnd();
/*glBegin(GL_LINES);
glVertex2i(pPoint[0].x, pPoint[0].y);
glVertex2i(pPoint[n - 1].x, pPoint[n - 1].y);
glEnd();*/ glFlush();
return 0;
}
for(int i = 0; i < n; i++)
{
point1[i] = pPoint[i];
point2[i] = pPoint[i];
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < n - i; j++)
{
point2[j].x = (1 - t) * point2[j].x + t * point2[j + 1].x;
point2[j].y = (1 - t) * point2[j].y + t * point2[j + 1].y;
}
point1[i] = point2[0];
}
DrawCircle(point1, t, n);
DrawCircle(point2, t, n);
}我觉得是最后各个点都相连了然后只需要画这些点就好了,可是我这样好像有问题,那个会有栈溢出
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货