This repository was archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.cpp
More file actions
100 lines (80 loc) · 1.99 KB
/
draw.cpp
File metadata and controls
100 lines (80 loc) · 1.99 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include"draw.h"
inline void swap(int &a, int &b)
{
int t;
t = a;
a = b;
b = t;
}
void drawWuLine(HDC hdc, int x0, int y0, int x1, int y1)
{
float alpha;
int color = 100;
//The calculation of the coordinates
int dx = (x1 > x0) ? (x1 - x0) : (x0 - x1);
int dy = (x1 > x0) ? (y1 - y0) : (y0 - y1);
//If the line is parallel to one of the axis, draw an usual line - fill all the pixels in a row
if (dx == 0) {
if (y1 < y0)
{
swap(x0, x1);
swap(y0, y1);
}
for (int i = y0; i <= y1; i++) {
SetPixel(hdc, x1, i, color);
}
return;
}
if (dy == 0) {
if (x1 < x0)
{
swap(x0, x1);
swap(y0, y1);
}
for (int i = x0; i <= x1; i++) {
SetPixel(hdc, i, y0, color);
}
return;
}
//For the X-line (slope coefficient < 1)
if (abs(dy) < dx)
{
//The first point must have a smaller x coordinate
if (x1 < x0)
{
swap(x0, x1);
swap(y0, y1);
}
fixed grad = fracToFixed(dy, dx);
fixed intery = intToFixed(y0) + grad;
//First point
SetPixel(hdc, x0, y0, color);
for (int x = x0 + 1; x < x1; x++)
{
alpha = (fractionalPart(intery));
SetPixel(hdc, x, fixedToInt(intery) , RGB(255*alpha, 255 * alpha, 255 * alpha));
SetPixel(hdc, x, fixedToInt(intery) +1, (RGB(255 *(1- alpha), 255 * (1 - alpha), 255 * (1 - alpha) )));
intery += grad;
}
//Last point
SetPixel(hdc, x1, y1, color);
}
//For the Y-line (slope coefficient > 1)
else
{
if (y1 < y0){
swap(x0, x1);
swap(y0, y1);
}
fixed grad = fracToFixed(dx , dy);
fixed interx = intToFixed(x0) + grad;
SetPixel(hdc, x0, y0, color);
for (int y = y0 + 1; y < y1; y++){
alpha = (fractionalPart(interx));
SetPixel(hdc, fixedToInt(interx), y, RGB(255 * alpha, 255 * alpha, 255 * alpha));
SetPixel(hdc, fixedToInt(interx) + 1, y, (RGB(255 * (1 - alpha), 255 * (1 - alpha), 255 * (1 - alpha))));
interx += grad;
}
SetPixel(hdc, x1, y1, color);
}
}