?? bezier曲線.txt
字號:
#include <glut.h>
#include "iostream.h"
double x0;double y0;
double x1;double y1;
double x2;double y2;
double x3;double y3;
void drawpixel(float x1,float y1,float x2,float y2)
{
glBegin(GL_POINTS);
//glVertex2f(m,n);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
glFlush();
}
//void Bresenhamline(int x0,int y0,int x1, int y1)
void Bezier(double x0,double y0,
double x1,double y1,
double x2,double y2,
double x3,double y3)
{
double x,y,X,Y;
x=x0;y=y0;
for (double t=0.0; t<= 1.0;t=t+0.001)
{
X=x;Y=y;
x=(1-t)*(1-t)*(1-t)*x0+3*t*(1-t)*(1-t)*x1+3*t*t*(1-t)*x2+t*t*t*x3;
y=(1-t)*(1-t)*(1-t)*y0+3*t*(1-t)*(1-t)*y1+3*t*t*(1-t)*y2+t*t*t*y3;
drawpixel(X,Y,x,y);
}
}
void Render(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glPointSize(1.0);
Bezier(x0,y0,x1,y1,x2,y2,x3,y3);
//Bresenhamline(0,0,0,100);
}
//該函數用于設置渲染狀態
void SetupRC(void)
{
glClearColor(0.0f, 1.0f, 0.0f,0.0f);
//設置背景的顏色
}
//當窗口大小改變時由GLUT函數調用,保證所繪正方形的形狀
void ChangeSize(GLsizei width, GLsizei Height)
{
GLfloat aspectRatio;
if (Height == 0) {
Height = 1;
}
glViewport(0, 0, width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)width / (GLfloat) Height;
if (width <= Height) {
glOrtho(-200.0, 200.0, -200.0 / aspectRatio, 200.0 / aspectRatio, 1.0, -1.0);
}
else{
glOrtho(-200.0 * aspectRatio, 200.0 * aspectRatio, -200.0, 200.0, 1.0, -1.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//主程序入口
void main(void)
{
cout<<"請輸入坐標:"<<endl;
cin>>x0>>y0>>x1>>y1>>x2>>y2>>x3>>y3;
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow(" Bezier畫線 ");
glutDisplayFunc(Render);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -