绘制任意阶Bezier曲线

#include <iostream>
#include <windows.h>
#include <GL/glut.h>
#include<algorithm>
#define n 7
using namespace std;
//#pragma comment(lib, "glut32.lib")    
int index;
POINT point[n];
GLfloat oldx,oldy,newx,newy;
double ratio;
int flag;
struct draw
{
	double x;
	double y;
}get;
draw Array[n][1200000];
draw Poi;
int cnt;
void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
draw reckon(draw x, draw y, double ratio)
{
	draw ans;
	ans.x = y.x - x.x;
	ans.y = y.y - x.y;
	ans.x *= ratio;
	ans.y *= ratio;
	ans.x += x.x;
	ans.y += x.y;
	return ans;
}
//draw compute(POINT x, POINT y, double ratio)
//{
//	POINT temp;
//	temp.x = y.x - x.x;
//	temp.y = y.y - x.y;
//	draw ans;
//	ans.x = (FLOAT)temp.x*1.00*ratio / 400 + (FLOAT)x.x*1.00 / 400;
//	ans.y = (FLOAT)temp.y*1.00*ratio / 400 + (FLOAT)x.y*1.00 / 400;
//	return ans;
//}
draw GetPoint(int row, int col)
{
	if (row == 0)
		return Array[0][col];
	return reckon(GetPoint(row - 1, col), GetPoint(row - 1, col + 1), ratio);
}
void display(void)
{
	memset(Array, sizeof(Array), 0);
	flag = 0;
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_LINES);
	int number = 0;
	for (int i = 0; i < n-1; i++)
	{
		glColor3f((i*1.0+0.5)/(n*1.0), (i*0.9+0.6)/(n*1.0),1.5);
		GLfloat oldx = (FLOAT)point[i].x*1.00 / 400;
		GLfloat oldy = (FLOAT)point[i].y*1.00 / 400;
		GLfloat newx = (FLOAT)point[i+1].x*1.00 / 400;
		GLfloat newy = (FLOAT)point[i+1].y*1.00 / 400;
		Array[0][i].x = point[i].x*1.00 / 400;;
		Array[0][i].y = point[i].y*1.00 / 400;
		Array[0][i+1].x = point[i + 1].x*1.00 / 400;
		Array[0][i+1].y = point[i + 1].y*1.00 / 400;
		glBegin(GLU_POINT);
		glVertex2f(Array[0][i].x, Array[0][i].y);
		glBegin(GLU_POINT);
		glVertex2f(Array[0][i + 1].x, Array[0][i + 1].y);
		glBegin(GL_LINES);
		glVertex2f(Array[0][i].x, Array[0][i].y);
		glVertex2f(Array[0][i + 1].x, Array[0][i + 1].y);
		glEnd();
		}
	cnt = 0;
	int temp = n-1;
	for (double deep = 0.0;deep < abs(Array[0][0].x-Array[0][1].x); deep += 0.001/40)
	{
		ratio = deep / abs(Array[0][0].x - Array[0][1].x)*1.0;
		draw poi = GetPoint(n - 1, 0);
		Array[n - 1][cnt].x = poi.x;
		Array[n - 1][cnt++].y = poi.y;
		//while (temp > 0)//wo qu ni ma de while xun huan  lao zi xie di gui le 
		//{
		//	for (int i = 0; i < temp; i++)
		//	{
		//		Poi = reckon(Array[n - 1 - temp][i], Array[n - 1 - temp][i + 1], ratio);
		//		Array[n - temp][i].x = Poi.x;
		//		Array[n - temp][i].y = Poi.y;
		//	}
		//	temp--;
		//}
		//draw arg1 = reckon(Array[n - 2][0], Array[n - 2][1], ratio);
		//Array[n - 1][cnt].x = arg1.x;

		//Array[n - 1][cnt].y = arg1.y;
		//cnt += 1;
		//for (int i = 0; i < 2; i++)
		//{
			//draw arg11 = reckon(Array[0][0], Array[0][1],ratio);
			//Array[1][0].x = arg11.x;
			//Array[1][0].y = arg11.y;

			//draw arg12 = reckon(Array[0][1], Array[0][2], ratio);
			//Array[1][1].x = arg12.x;
			//Array[1][1].y = arg12.y;

			//draw arg13 = reckon(Array[0][2], Array[0][3], ratio);
			//Array[1][2].x = arg13.x;
			//Array[1][2].y = arg13.y;
			draw arg14 = reckon(arg11,arg12, ratio);
			draw arg15 = reckon(arg12, arg13, ratio);
			draw arg16 = reckon(arg14, arg15, ratio);
			//draw arg14 = reckon(Array[1][0], Array[1][1], ratio);
			//draw arg15 = reckon(Array[1][1], Array[1][2], ratio);
			//draw arg16 = reckon(arg14, arg15, ratio);
			//Array[2][cnt].x = arg16.x;
			//Array[2][cnt++].y = arg16.y;
		//}
	}
	glBegin(GL_POINTS);
	for (int i = 0; i < cnt; i++)
	{
		glColor3f(cos(index), sin(index), 55);
		glVertex2f(Array[n - 1][i].x, Array[n - 1][i].y);
	}
	glutSwapBuffers();
	glEnd();
	glFlush();
}
void motion(int x, int y)
{
	point[index].x = x;
	point[index].y = 400 - y;
}
void mouse(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON)
	{
		if (state == GLUT_DOWN)
		{
			glutMotionFunc(motion);
			if (index < n)
			{
				cout << point[index].x << endl;
				cout << point[index++].y << endl;
			}

		}
	}
}
int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(400, 400);
	glutCreateWindow("I love OpenGl");
	init();
	glutDisplayFunc(display);
	glutMouseFunc(mouse);

	glutPassiveMotionFunc(motion);
	glutIdleFunc(display);
	glutMainLoop();
	return 0;
}

 

 

define n为阶数,鼠标点击即可绘制。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值