-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBresenham Line Drawing Algorithm.cpp
79 lines (65 loc) · 1.5 KB
/
Bresenham Line Drawing Algorithm.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
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int X_1, X_2, Y_1, Y_2;
void putpixel(int x, int y){
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex2d(x,y);
glEnd();
}
void drawAxis(){
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (int i=-500; i<=500; i++){
glVertex2d(i,0);
glVertex2d(0,i);
}
glEnd();
glFlush();
}
void bresenhamLineDrawing(){
int dx, dy, x, y, decision;
dx = abs(X_2 - X_1);
dy = abs(Y_2 - Y_1);
x = X_1;
y = Y_1;
decision = (2*dy) - dx;
while(x<=X_2){
putpixel(x,y);
if (decision<0){
decision = decision + (2*dy);
}
else{
decision = decision + 2* (dy-dx);
y++;
}
x++;
}
}
void display(){
bresenhamLineDrawing();
drawAxis();
glFlush();
}
int main(int argc, char **argv){
cout<< "INPUT X_1, Y_1, X_2, Y_2" << endl;
cin>>X_1>>Y_1>>X_2>>Y_2;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Bresenham Line Drawing Algorithm");
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-500,500,-500,500);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}