Extremely Fast Line Algorithm Variation A (Division)
Copyright 2001-2, By Po-Han Lin
November 29, 2001
The following is the source for the Extremely Fast Line Algorithm Variation
A (Division).
Return to Line Algorithms
// Extremely Fast Line Algorithm Var A (Division)
// Copyright 2001-2, By Po-Han Lin
// Freely useable in non-commercial applications as long as credits
// to Po-Han Lin and link to http://www.edepot.com is provided in
// source code and can been seen in compiled executable.
// Commercial applications please inquire about licensing the algorithms.
//
// Lastest version at http://www.edepot.com/phl.html
// used by myLine
void myPixel(SURFACE* surface, int x,int y) {
// PLOT x,y point on surface
}
// THE EXTREMELY FAST LINE ALGORITHM Variation A (Division)
void myLine(SURFACE* surface, int x, int y, int x2, int y2) {
bool yLonger=false;
int incrementVal;
int shortLen=y2-y;
int longLen=x2-x;
if (abs(shortLen)>abs(longLen)) {
int swap=shortLen;
shortLen=longLen;
longLen=swap;
yLonger=true;
}
if (longLen<0) incrementVal=-1;
else incrementVal=1;
double divDiff;
if (shortLen==0) divDiff=longLen;
else divDiff=(double)longLen/(double)shortLen;
if (yLonger) {
for (int i=0;i!=longLen;i+=incrementVal) {
myPixel(surface,x+(int)((double)i/divDiff),y+i);
}
} else {
for (int i=0;i!=longLen;i+=incrementVal) {
myPixel(surface,x+i,y+(int)((double)i/divDiff));
}
}
}
void mySquare(SURFACE* surface,int x, int y, int x2, int y2) {
myLine(surface,x,y,x2,y2);
myLine(surface,x2,y2,x2+(y-y2),y2+(x2-x));
myLine(surface,x,y,x+(y-y2),y+(x2-x));
myLine(surface,x+(y-y2),y+(x2-x),x2+(y-y2),y2+(x2-x));
}
void myRect(SURFACE* surface, int x, int y, int x2, int y2) {
myLine(surface,x,y,x2,y);
myLine(surface,x2,y,x2,y2);
myLine(surface,x2,y2,x,y2);
myLine(surface,x,y2,x,y);
}