计算机图形学bresenham完整算法画直线椭圆和圆jobzrrxs
#include
#include
#include "stdio.h"
int m_PointNumber = 0; //动画时绘制点的数目
int m_DrawMode = 1; //绘制模式 1 DDA算法画直线
// 2 中点Bresenham算法画直线
// 3 改进Bresenham算法画直线
// 4 八分法绘制圆
// 5 四分法绘制椭圆
//绘制坐标线
void DrawCordinateLine(void)
{
int i = -250 ;
//坐标线为黑色
glColor3f(0.0f, 0.0f ,0.0f);
glBegin(GL_LINES);
for (i=-250;i<=250;i=i+10)
{
glVertex2f((float)(i), -250.0f);
glVertex2f((float)(i), 250.0f);
glVertex2f(-250.0f, (float)(i));
glVertex2f(250.0f, (float)(i));
}
glEnd();
}
//绘制一个点,这里用一个正方形表示一个点
void putpixel(GLsizei x, GLsizei y)
{
glRectf(10*x,10*y,10*x+10,10*y+10);
}
///
//DDA画线算法 //
// //
// //
// //
///
void DDACreateLine(GLsizei x0, GLsizei y0, GLsizei x1, GLsizei y1, GLsizei num)
{
//设置颜色
glColor3f(1.0f,0.0f,0.0f);
//对画线动画进行控制
if(num == 1)
printf("DDA画线算法:各点坐标\n");
else if(num==0)
return;
//画线算法的实现
GLsizei dx,dy,epsl,k;
GLfloat x,y,xIncre,yIncre;
dx = x1-x0;
dy = y1-y0;
x = x0;
y = y0;
if(abs(dx) > abs(dy)) epsl = abs(dx);
else epsl = abs(dy);
xIncre = (float)dx / epsl ;
yIncre = (float)dy / epsl ;
for(k = 0; k<=epsl; k++){
putpixel((int)(x+0.5), (int)(y+0.5));
if (k>=num-1) {
printf("x=%f , y=%f,取整后 x=%d,y=%d\n", x, y, (int)(x+0.5),(int)(y+0.5));
break;
}
x += xIncre;
y += yIncre;
if(x >= 25 || y >= 25) break;
}
}
///
//中点Bresenham算法画直线(0<=k<=1) //
// //
// //
//