#include "stdafx.h"
#include <iostream>
#include <gl\glut.h>
using namespace std;
double a, b;//长半轴a,短半轴b
/*Bresenham算法*/
void Bresenham(double a,double b)
{
/*计算初始值*/
double d = pow(b, 2) + pow(a, 2)*(-b + 0.25), x = 0, y = b;
/*当b2(x+1)<a2(y-0.5)时,重复步骤(3)和(4),否则转(6)*/
/*(3)绘制点(x,y)及其在四分象限上的另外3个对称点.*/
while (pow(b,2)*(x+1)<pow(a,2)*(y-0.5))
{
glVertex2d(x, y);//绘制点(x,y)
glVertex2d(-x, y);
glVertex2d(x, -y);
glVertex2d(-x, -y);
if (d <= 0)
{
d += pow(b, 2)*(2 * x + 3);
x += 1;
}
else
{
d += pow(b, 2)*(2 * x + 3) + pow(a, 2)*(-2 * y + 2);
x += 1;
y -= 1;
}
}
/*(6)用上半部分计算的最后点(x,y)来计算下半部分中d的初值*/
d = pow(b, 2)*pow(x + 0.5, 2) + pow(a, 2)*pow(y - 1, 2) - pow(a, 2)*pow(b, 2);
/*当y>=0时,重复步骤(7)和(8),否则结束*/
while (y >= 0)
{
/*(7)绘制点(x,y)及其在四分象限的另外3个对称点*/
glVertex2d(x, y);//绘制点(x,y)
glVertex2d(-x, y);
glVertex2d(x, -y);
glVertex2d(-x, -y);
/*(8)判断d的符号*/
if (d <= 0)
{
d = d + pow(b, 2)*(2 * x + 2) + pow(a, 2)*(-2 * y + 3);
x += 1;
y -= 1;
}
else
{
d += pow(a, 2)*(-2 * y + 3);
y -= 1;
}
}
}
/*椭圆的外观*/
void ellipseSegment()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);//设置点的颜色为红色
glBegin(GL_POINTS);
Bresenham(a,b);
glEnd();
glFlush();
}
/*主函数*/
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
cout << "输入椭圆的长半轴a,短半轴b:" << endl;
cin >> a >> b;//输入长半轴、短半轴
glutInitWindowPosition(50, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("中点Bresenham 画椭圆");
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-250, +250,-250, +250);
glutDisplayFunc(ellipseSegment);//参数直线的外观,见上面
glutMainLoop();
system(0);
return 0;
}
【计算机图形学】椭圆的扫描转换
最新推荐文章于 2021-07-28 17:17:52 发布