-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpen.cpp
More file actions
87 lines (74 loc) · 2.06 KB
/
pen.cpp
File metadata and controls
87 lines (74 loc) · 2.06 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
#include "pen.h"
void drawLine(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
bool steep = false;
if (abs(x0 - x1) < abs(y0 - y1)) {
swap(x0, y0);
swap(x1, y1);
steep = true;
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int dx = x1 - x0;
int dy = y1 - y0;
int derror2 = abs(dy) * 2;
int error2 = 0;
int y = y0;
int ydir = (y1 > y0) ? 1 : -1;
for (int x = x0; x <= x1; x++) {
if (steep) {
image.set(y, x, color);
} else {
image.set(x, y, color);
}
error2 += derror2;
if (error2 > dx) {
y += ydir;
error2 -= dx * 2;
}
}
}
void drawRect(int x, int y, int width, int height, TGAImage &image, TGAColor color)
{
drawLine(x, y, width - x, y, image, color);
drawLine(x, y, x, height - y, image, color);
drawLine(width, y, width - x, height - y, image, color);
drawLine(x, height, width - x, height - y, image, color);
}
void drawSquare(int x, int y, int width, TGAImage &image, TGAColor color)
{
drawRect(x, y, width, width, image, color);
}
void drawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, TGAImage &image, TGAColor color)
{
drawLine(x0, y0, x1, y1, image, color);
drawLine(x1, y1, x2, y2, image, color);
drawLine(x2, y2, x0, y0, image, color);
}
void drawCircle(int x1, int y1, int r, TGAImage &image, TGAColor color) {
int x = 0;
int y = r;
int delta = 1 - 2 * r;
int error = 0;
while(y >= 0) {
image.set(x1 + x, y1 + y, color);
image.set(x1 + x, y1 - y, color);
image.set(x1 - x, y1 + y, color);
image.set(x1 - x, y1 - y, color);
error = 2 * (delta + y) - 1;
if(delta < 0 && error <= 0) {
delta += 2 * ++x + 1;
continue;
}
error = 2 * (delta - x) - 1;
if(delta > 0 && error > 0) {
delta += 1 - 2 * --y;
continue;
}
else
x++;
delta += 2 * (x - y);
y--;
}
}