-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation.py
126 lines (94 loc) · 3 KB
/
transformation.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
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
from bresenham import bresenham
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
img = Image.fromarray(np.zeros((200, 200), dtype=np.float32), mode= "F")
def line(x1,y1,x2,y2):
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)
coords = bresenham(x1,y1,x2,y2)
for coord in coords:
img.putpixel(coord,1)
def translate(x1, y1, x2, y2, tx, ty):
# Coordinate Matrix
X = np.array([[x1,y1,1],
[x2,y2,1]])
# Translation Matrix
T = np.array([[1,0,0],
[0,1,0],
[tx,ty,1]])
result = np.matmul(X,T)
print(result)
line(result[0][0], result[0][1], result[1][0], result[1][1])
def rotation(x1, y1, x2, y2, theta):
# Coordinate Matrix
X = np.array([[x1,y1,1],[x2,y2,1]])
'''
Translation matrices for rotation about origin
'''
T = np.array([[1,0,0],[0,1,0],[-x1,-y1,1]])
Tinv = np.array([[1,0,0],[0,1,0],[x1,y1,1]])
R = np.array([
[np.cos(np.deg2rad(theta)), np.sin(np.deg2rad(theta)), 0],
[np.sin(np.deg2rad(-theta)), np.cos(np.deg2rad(theta)), 0],
[0, 0, 1]])
result = np.matmul(X,T)
result1 = np.matmul(result,R)
result2 = np.matmul(result1,Tinv)
print(result2)
line(result2[0][0], result2[0][1], result2[1][0], result2[1][1])
def scaling(x1, y1, x2, y2, sx, sy):
# Coordinate Matrix
X = np.array([[x1,y1,1],
[x2,y2,1]])
# Scaling Matrix
T = np.array([[sx,0,0],
[0,sy,0],
[0,0,1]])
result = np.matmul(X,T)
print(result)
line(result[0][0], result[0][1], result[1][0], result[1][1])
def x_reflection(x1, y1, x2, y2):
# Coordinate Matrix
X = np.array([[x1,y1,1],[x2,y2,1]])
# Reflection about x-axis Matrix
T = np.array([[1,0,0],
[0,-1,0],
[0,0,1]])
result = np.matmul(X,T)
print(result)
line(result[0][0], result[0][1], result[1][0], result[1][1])
def y_reflection(x1, y1, x2, y2):
# Coordinate Matrix
X = np.array([[x1,y1,1],[x2,y2,1]])
# Reflection about x-axis Matrix
T = np.array([[-1,0,0],
[0,1,0],
[0,0,1]])
result = np.matmul(X,T)
print(result)
line(result[0][0], result[0][1], result[1][0], result[1][1])
# Call each functions separately
# Translation
line(20, 30, 100, 80)
translate(20, 30, 100, 80, 30, 40)
# Rotation
line(20, 30, 100, 80)
rotation(20, 30, 100, 80, -45)
# Scaling
line(25,25,25,175)
line(25,175,175,175)
line(175,25,175,175)
line(25,25,175,25)
scaling(25,25,25,175,0.5,0.5)
scaling(25,175,175,175,0.5,0.5)
scaling(175,25,175,175,0.5,0.5)
scaling(25,25,175,25,0.5,0.5)
# Reflection
line(100+20, 100+30, 100+90, 100+80)
x_reflection(100+20, 100+30, 100+90, 100+80)
y_reflection(100+20, 100+30, 100+90, 100+80)
plt.imshow(np.array(img))
plt.show()