This topic is now archived and is closed to further replies.


Line Algorithm

Recommended Posts

This works if the slope is greater than 0 and less than 1, it''s normally known as Bresenham''s Line-Drawing Algorithm:
void Line(int xa, int ya, int xb, int yb) {
int dx = abs(xa-xb), dy = abs(ya-yb);
int p = 2*dy-dx;
int twoDy = 2*dy, twoDyDx = 2*(dy-dx);
int x, y, xEnd;

if(xa > xb) {
x = xb;
y = yb;
xEnd = xa;
} else {
x = xa;
y = ya;
xEnd = yb;

while(x < xEnd) {
if(p<0) p += twoDy;
else {
p += twoDyDx;


[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post

Link to post
Share on other sites

void line(float x1, float y1, float x2, float y2)
double hl=fabs(x2-x1);
double vl=fabs(y2-y1);
double length=hl;
float deltax=(x2-x1)/(float)length;
float deltay=(y2-y1)/(float)length;

if (hl < vl)
length = vl;

for (int i=0; i<(int)length; i++)
unsigned long x=(int)(x1+=deltax);
unsigned long y=(int)(y1+=deltay);
if ((x<640)&&(y<480)) SetPixel(x,y);

Share this post

Link to post
Share on other sites