-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab8.cpp
More file actions
87 lines (80 loc) · 2.13 KB
/
Copy pathlab8.cpp
File metadata and controls
87 lines (80 loc) · 2.13 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
#include <GL/glut.h>
int x_min = 100, y_min = 100, x_max = 500, y_max = 500; // Window coordinates
int xc = 300, yc = 300, r = 100; // Circle center and radius
int x1 = 150, y1 = 400, x2 = 450, y2 = 150; // Line endpoints
void setPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void mapCoordinates(int& x, int& y) {
// Map x-coordinate to screen space
x = ((float)(x - x_min)/(x_max - x_min)) * 600;
// Map y-coordinate to screen space and flip y-axis
y = 600 - ((float)(y - y_min)/(y_max - y_min)) * 600;
}
void bresenham_circle() {
int x = 0, y = r, d = 1 - r;
while (x <= y) {
mapCoordinates(xc + x, yc + y);
setPixel(xc + x, yc + y);
mapCoordinates(xc + x, yc - y);
setPixel(xc + x, yc - y);
mapCoordinates(xc - x, yc + y);
setPixel(xc - x, yc + y);
mapCoordinates(xc - x, yc - y);
setPixel(xc - x, yc - y);
mapCoordinates(xc + y, yc + x);
setPixel(xc + y, yc + x);
mapCoordinates(xc + y, yc - x);
setPixel(xc + y, yc - x);
mapCoordinates(xc - y, yc + x);
setPixel(xc - y, yc + x);
mapCoordinates(xc - y, yc - x);
setPixel(xc - y, yc - x);
if (d < 0) {
d = d + 2 * x + 3;
} else {
d = d + 2 * (x - y) + 5;
y--;
}
x++;
}
}
void bresenham_line() {
int dx = x2 - x1, dy = y2 - y1, d, inc1, inc2, x, y;
if (dx < 0) {
dx = -dx;
inc1 = -1;
} else {
inc1 = 1;
}
if (dy < 0) {
dy = -dy;
inc2 = -1;
} else {
inc2 = 1;
}
x = x1;
y = y1;
mapCoordinates(x, y);
setPixel(x, y);
if (dx > dy) {
d = 2 * dy - dx;
while (x != x2) {
if (d >= 0) {
y += inc2;
d -= 2 * dx;
}
x += inc1;
d += 2 * dy;
mapCoordinates(x, y);
setPixel(x, y);
}
} else {
d = 2 * dx - dy;
while (y != y2) {
if (d >= 0) {
x += inc1;
d -= 2 * dy;
}