forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
100 lines (83 loc) · 2.18 KB
/
draw.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
89
90
91
92
93
94
95
96
97
98
99
100
from lpython import i32, f64, TypeVar
import numpy
H = TypeVar("H")
W = TypeVar("W")
def Pixel(H: i32, W: i32, Screen: i32[H, W], x: i32, y: i32) -> None:
Screen[y, x] = 255
def Clear(H: i32, W: i32, Screen: i32[H, W]):
i: i32
j: i32
for i in range(H):
for j in range(W):
Screen[i, j] = 0
def DisplayTerminal(H: i32, W: i32, Screen: i32[H, W]):
i: i32
j: i32
print("+", end = "")
for i in range(W):
print("-", end = "")
print("+")
for i in range(H):
print("|", end = "")
for j in range(W):
if bool(Screen[i, j]):
print(".", end = "")
else:
print(" ", end = "")
print("|")
print("+", end = "")
for i in range(W):
print("-", end = "")
print("+")
def Display(H: i32, W: i32, Screen: i32[H, W]):
i: i32
j: i32
print("P2")
print(W, H)
print(255)
for i in range(H):
for j in range(W):
print(Screen[i, j])
def Line(H: i32, W: i32, Screen: i32[H, W], x1: i32, y1: i32, x2: i32, y2: i32) -> None:
dx: i32 = abs(x2 - x1)
dy: i32 = abs(y2 - y1)
sx: i32
sy: i32
if x1 > x2:
sx = -1
else:
sx = 1
if y1 > y2:
sy = -1
else:
sy = 1
err: i32 = dx - dy
while x1 != x2 or y1 != y2:
Pixel(H, W, Screen, x1, y1)
e2: i32 = 2 * err
if e2 > -dy:
err -= dy
x1 += sx
if e2 < dx:
err += dx
y1 += sy
Pixel(H, W, Screen, x2, y2)
def Circle(H: i32, W: i32, Screen: i32[H, W], x: i32, y: i32, r: f64) -> None:
x0: i32 = i32(int(r))
y0: i32 = 0
err: i32 = 0
while x0 >= y0:
Pixel(H, W, Screen, x + x0, y + y0)
Pixel(H, W, Screen, x - x0, y + y0)
Pixel(H, W, Screen, x + x0, y - y0)
Pixel(H, W, Screen, x - x0, y - y0)
Pixel(H, W, Screen, x + y0, y + x0)
Pixel(H, W, Screen, x - y0, y + x0)
Pixel(H, W, Screen, x + y0, y - x0)
Pixel(H, W, Screen, x - y0, y - x0)
if err <= 0:
y0 += 1
err += 2 * y0 + 1
if err > 0:
x0 -= 1
err -= 2 * x0 + 1