forked from uniquekarki/Computer-Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bresenham_line.py
88 lines (72 loc) · 2.14 KB
/
bresenham_line.py
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
import pygame, sys
from pygame.locals import *
from pygame import gfxdraw
pygame.init()
size = (700,700)
screen_surface = pygame.display.set_mode(size, 0, 32)
pygame.display.set_caption("Bresenham's Line Drawing Algorithm")
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,128,0)
BLUE = (0,0,255)
screen_surface.fill(WHITE)
def bresenham(x0,y0,x1,y1):
dx = x1 - x0
dy = y1 - y0
m = abs(dy/dx)
print('Slope (m) = {}'.format(m))
if m < 1:
print("Slope less than one.")
gfxdraw.pixel(screen_surface, round(x0), round(y0), BLACK)
bresenham_l(dx, dy,x0,y0)
# pass
else:
print("Sloper greater than one.")
gfxdraw.pixel(screen_surface, round(x0), round(y0), BLACK)
bresenham_g(dx,dy,x0,y0)
def bresenham_l(dx,dy,x0,y0):
#initial decision parameter
p0 = 2 * dy - dx
x,y = x0,y0
pk = p0
points = []
for i in range(abs(dx)):
if pk < 0:
x,y = x+1, y
gfxdraw.pixel(screen_surface, round(x), round(y), BLACK)
pk = pk + 2 * dy
points.append((pk,x,y))
else:
x,y = x + 1, y+1
gfxdraw.pixel(screen_surface, round(x), round(y), BLACK)
pk = pk + 2 * dy - 2 * dx
points.append((pk,x,y))
for point in points:
print(point)
def bresenham_g(dx,dy,x0,y0):
#initial decision parameter
p0 = 2 * dx - dy
x,y = x0,y0
pk = p0
points = []
for i in range(abs(dy)):
if pk < 0:
x,y = x, y+1
gfxdraw.pixel(screen_surface, round(x), round(y), BLACK)
pk = pk + 2 * dy
points.append((pk,x,y))
else:
x,y = x + 1, y+1
gfxdraw.pixel(screen_surface, round(x), round(y), BLACK)
pk = pk + 2 * dx - 2 * dy
points.append((pk,x,y))
for point in points:
print(point)
bresenham(600,700,100,100)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit( )