• 大小: 4KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: C/C++
  • 标签: 直线  

资源简介

用DDA、中点画线法、Bresenham算法绘制4个象限的直线。

资源截图

代码片段和文件信息

#include

#ifdef __APPLE__
#include 
#else
#include 
#endif

#include
#include
#include


void init (void)
{
glClearColor (1.0 1.0 1.0 0.0);  // Set display-window color to white.
glMatrixMode (GL_PROJECTION);       // Set projection parameters.
gluOrtho2D (0.0 200.0 0.0 150.0);
}

int round(const float a)
{
return (int)(a+0.5);
}
void lineWithMP(GLint x0 GLint y0 GLint xn GLint yn)
{
GLint a = yn - y0;
GLint b = -(xn - x0);
GLint c = xn*y0 - x0*yn;

GLint deltaA = 0;
GLint deltaB = 0;

if(0 != a)
{
deltaA = a / fabs(float(a));
}

if(0 != b)
{
deltaB = -1 * b / fabs(float(b));
}

GLfloat x = x0;
GLfloat y = y0;

    glColor3f(0.01.00.0);
glPointSize(3.0f);

if(fabs(float(a))  {
GLint steps = fabs(float(b));
for(GLint i = 0; i < steps; i ++)
{
// 注意屏幕坐标系和世界坐标系的不同。也就是为什么是大于 0 
if(0 < (a * (x + 1) + b * (y + 0.5) + c))
{
y += 1 * deltaA;
}

x += 1 * deltaB;

glBegin(GL_POINTS);
glVertex2i(x y);
glEnd();
glFlush();

}
}
else 
{
GLint steps = fabs(float(a));
for(GLint i = 0; i < steps; i ++)
{
if(0 <= (a * (x + 1) + b * (y + 0.5) + c))
{
x += 1 * deltaB;
}

y += 1 * deltaA;

glBegin(GL_POINTS);
glVertex2i(x y);
glEnd();
glFlush();

}
}
}
void lineDDA(int x0int y0int xEndint yEnd)
{
glColor3f(0.00.01.0);
glPointSize(3.0f);
int dx = xEnd - x0;
int dy = yEnd - y0;
int stepsk;
float xIncrementyIncrementx = x0y = y0;

if(fabs(float(dx)) > fabs(float(dy)))
steps = fabs(float(dy));
else
steps = fabs(float(dx));
xIncrement = (float)(dx)/(float)(steps);
yIncrement = (floa

评论

共有 条评论