-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextremely_fast_line_algorithm.c
82 lines (70 loc) · 1.98 KB
/
extremely_fast_line_algorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Extremely Fast Line Algorithm Var E (Addition Fixed Point PreCalc)
// 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
// This version is for standard displays (up to 65536x65536)
// For small display version (256x256) visit http://www.edepot.com/lineex.html
// used by myLine
void myPixel(SURFACE* surface, int x,int y) {
// PLOT x,y point on surface
}
// THE EXTREMELY FAST LINE ALGORITHM Variation E (Addition Fixed Point PreCalc)
void myLine(SURFACE* surface, int x, int y, int x2, int y2) {
bool yLonger=false;
int shortLen=y2-y;
int longLen=x2-x;
if (abs(shortLen)>abs(longLen)) {
int swap=shortLen;
shortLen=longLen;
longLen=swap;
yLonger=true;
}
int decInc;
if (longLen==0) decInc=0;
else decInc = (shortLen << 16) / longLen;
if (yLonger) {
if (longLen>0) {
longLen+=y;
for (int j=0x8000+(x<<16);y<=longLen;++y) {
myPixel(surface,j >> 16,y);
j+=decInc;
}
return;
}
longLen+=y;
for (int j=0x8000+(x<<16);y>=longLen;--y) {
myPixel(surface,j >> 16,y);
j-=decInc;
}
return;
}
if (longLen>0) {
longLen+=x;
for (int j=0x8000+(y<<16);x<=longLen;++x) {
myPixel(surface,x,j >> 16);
j+=decInc;
}
return;
}
longLen+=x;
for (int j=0x8000+(y<<16);x>=longLen;--x) {
myPixel(surface,x,j >> 16);
j-=decInc;
}
}
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);
}