forked from devaprasad-n-m/CG_LAB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_dda.py
More file actions
61 lines (53 loc) · 1.27 KB
/
2_dda.py
File metadata and controls
61 lines (53 loc) · 1.27 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
#import statements
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
# Function for round off the pixel value
def ROUND(a):
return int(a+0.5)
# init function
def init():
glClearColor(0.0,0.0,0.0,1.0)
glColor3f(0.0,0.0,1.0)
glPointSize(2.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(-100,100,-100,100)
#Display function
def Display():
glClear(GL_COLOR_BUFFER_BIT)
x1=int(input('x1 coordinate: '))
y1=int(input ('y1 coordinate: '))
x2=int(input('x2 coordinate: '))
y2=int(input('y2 coordinate: '))
lineDDA(x1,y1,x2,y2)
# for setting pixel coordinates
def setPixel(xcoordinate,ycoordinate):
glBegin(GL_POINTS)
glVertex2f(xcoordinate,ycoordinate)
glEnd()
glFlush()
# DDA function
def lineDDA(x1,y1,x2,y2):
dx=abs(x2-x1)
dy=abs(y2-y1)
x,y=x1,y1
steps=dx if dx>dy else dy
xincrement=dx/float(steps)
yincrement=dy/float(steps)
setPixel(ROUND(x),ROUND(y))
for i in range (steps):
x+=xincrement
y+=yincrement
setPixel(ROUND(x),ROUND(y))
# main function
def main():
glutInit()
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(200,200)
glutCreateWindow("Line using DDA")
glutDisplayFunc(Display)
init()
glutMainLoop()
main()