-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLine.h
79 lines (67 loc) · 1.44 KB
/
Line.h
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
#pragma once
#include <cmath>
#include <GL/glut.h>
#include "Color.h"
void putPixel(int x, int y, Color color)
{
glColor3f(color.r, color.g, color.b);
glPointSize(1); //setss the size in pixel
glBegin(GL_POINTS); //writes pixel in the brame buffer
glVertex2i(x, y); //sets vertices
glEnd();
}
void Bressenham(float x1, float y1, float x2, float y2, Color color)
{
float dx, dy;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
float lx, ly;
if (x2 > x1)
lx = 1;
else
lx = -1;
if (y2 > y1)
ly = 1;
else
ly = -1;
float x = x1, y = y1;
// slope < 1
if (dx > dy)
{
float p = 2 * dy - dx;
for (float k = 0; k <= dx; k++)
{
putPixel(round(x), round(y), color);
if (p < 0)
{
x += lx;
p += 2 * dy;
}
else
{
x += lx;
y += ly;
p += 2 * dy - 2 * dx;
}
}
}
else
{ //slope > 1
float p = 2 * dx - dy;
for (float k = 0; k <= dy; k++)
{
putPixel(round(x), round(y), color);
if (p < 0)
{
y += ly;
p += 2 * dx;
}
else
{
x += lx;
y += ly;
p += 2 * dx - 2 * dy;
}
}
}
}