-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbresenham.cpp
128 lines (123 loc) · 1.84 KB
/
bresenham.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int a, b, c, d;
void drawpixel(int x, int y)
{
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void bresnham(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int m = dy / dx;
if (m < 1)
{
int dp = 2*dy - dx;
int x = x1;
int y = y1;
if (dx < 0)
{
x = x2;
y = y2;
x2 = x1;
}
drawpixel(x, y);
while (x>x2)
{
if (dp >= 0)
{
x = x + 1;
y = y + 1;
dp = dp + 2*dy - 2*dx;
}
else
{
x = x + 1;
y = y;
dp = dp + 2 * dy;
}
drawpixel(x,y);
}
}
if (m > 1)
{
int dp = 2 * dx - dy;
int x = x1;
int y = y1;
if (dy < 0)
{
x = x2;
y = y2;
y2 = y1;
}
drawpixel(x, y);
while (y>y2)
{
if (dp >= 0)
{
x = x + 1;
y = y + 1;
dp = dp + 2 * dx - 2 * dy;
}
else
{
x = x;
y = y+1;
dp = dp + 2 * dx;
}
drawpixel(x, y);
}
}
if (m == 1)
{
int x = x1;
int y = y1;
drawpixel(x, y);
while (x < x2)
{
x = x + 1;
y = y + 1;
drawpixel(x, y);
}
}
}
void myinit()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
bresnham(a, b, c, d);
glFlush();
}
int main(int argc, char** argv)
{
printf("Enter Scan Points (x1,y1) ");
scanf_s("%d%d", &a, &b);
printf("Enter Scan Points (x2,y2) ");
scanf_s("%d%d", &c, &d);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Brezenham");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}