void Mid_point(const unsigned& x1, const unsigned& y1, const unsigned& x2, const unsigned& y2)
{
int dx = x2 - x1, dy = y2 - y1;
enum Main_ChangeDirection { x, y };
Main_ChangeDirection MainChange;
if (abs(dx) >= abs(dy))
MainChange = x;
else MainChange = y;
if ((MainChange == x && x1 > x2) || (MainChange == y && y1 > y2))
{
Mid_point(x2, y2, x1, y1);
return;
}
initgraph(800, 640);
if (dx == 0)
{
for (unsigned y = y1; y <= y2; ++y)
{
putpixel(x1, y, RED);
}
_getch();
closegraph();
return;
}
enum trend { Positive, Negative };
trend k;
if (dx * dy >= 0)
k = Positive;
else k = Negative;
int a = -dy, b = dx;
if (MainChange == x)
{
if (k == Positive)
{
int d = 2 * a + b;
int delta1 = 2 * a;
int delta2 = 2 * (a + b);
for (unsigned x = x1, y = y1; x <= x2; ++x)
{
putpixel(x, y, BLUE);
if (d > 0)
{
d += delta1;
}
else
{
d += delta2;
y++;
}
}
}
else if (k == Negative)
{
int d = 2 * a - b;
int delta1 = 2 * (a - b);
int delta2 = 2 * a;
for (unsigned x = x1, y = y1; x <= x2; ++x)
{
putpixel(x, y, BLUE);
if (d > 0)
{
d += delta1;
--y;
}
else
{
d += delta2;
}
}
}
}
else if (MainChange == y)
{
if (k == Positive)
{
int d = a + 2 * b;
int delta1 = 2 * (a + b);
int delta2 = 2 * b;
for (unsigned y = y1, x = x1; y <= y2; ++y)
{
putpixel(x, y, BLUE);
if (d > 0)
{
d += delta1;
++x;
}
else
{
d += delta2;
}
}
}
else if (k == Negative)
{
int d = 2 * b - a;
int delta1 = 2 * b;
int delta2 = 2 * (b - a);
for (unsigned y = y1, x = x1; y <= y2; ++y)
{
putpixel(x, y, BLUE);
if (d > 0)
{
d += delta1;
}
else
{
d += delta2;
x--;
}
}
}
}
_getch();
closegraph();
}
注:使用该函数需要头文件<graphics.h>和<conio.h>,前一个头文件不在系统库中,可以直接百度免费下载。
|