-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineartransformations.py
144 lines (124 loc) · 2.71 KB
/
lineartransformations.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
"""LinearTransformations.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1TZe0G_nKcUvFJSKNo68CIlwzkLQlIn5t
"""
import numpy as np
import plotly.graph_objects as go
MyShape = np.matrix([[0,1,-1,0],
[1,-1,-1,1]])
fig = go.Figure(
go.Scatter(
x = np.array(MyShape[0,:])[0],
y = np.array(MyShape[1,:])[0],
name = "original",
mode = "lines"
)
)
fig.update_layout(
xaxis = dict(range=[-4,4]),
yaxis = dict(range=[-4,4]),
autosize=False,
width=400,
height=400
)
fig.show()
"""Scaling Transformation:
$$T = \begin{pmatrix} a & 0 \\ 0 & b \end{pmatrix}$$
"""
def scale2(a,b):
return np.matrix([[a,0],
[0,b]])
"""Scale shape: $S \cdot Myshape"""
scale2(4,5)*MyShape
fig = go.Figure(
go.Scatter(
x = np.array(MyShape[0,:])[0],
y = np.array(MyShape[1,:])[0],
name = "original",
mode = "lines"
)
)
NewShape = scale2(4,5)* MyShape
fig.add_trace(
go.Scatter(
x = np.array(NewShape[0,:])[0],
y = np.array(NewShape[1,:])[0],
name = "scaled",
mode = "lines"
)
)
fig.update_layout(
xaxis = dict(range=[-6,6]),
yaxis = dict(range=[-6,6]),
autosize=False,
width=400,
height=400
)
fig.show()
"""Rotation matrix:
$$R = \begin{pmatrix} cos(x) & -sin(x) \\ sin(x) & cos(x) \end{pmatrix}$$
"""
theta = np.radians(30)
c, s = np.cos(theta), np.sin(theta)
R = np.array(((c, -s), (s, c)))
print(R)
R * MyShape
fig = go.Figure(
go.Scatter(
x = np.array(MyShape[0,:])[0],
y = np.array(MyShape[1,:])[0],
name = "original",
mode = "lines"
)
)
NewShape1 = R * MyShape
fig.add_trace(
go.Scatter(
x = np.array(NewShape1[0,:])[0],
y = np.array(NewShape1[1,:])[0],
name = "Rotated",
mode = "lines"
)
)
fig.update_layout(
xaxis = dict(range=[-6,6]),
yaxis = dict(range=[-6,6]),
autosize=False,
width=400,
height=400
)
fig.show()
"""Shear
$$S = \begin{pmatrix} 1 & k \\ 0 & 1 \end{pmatrix}$$
"""
def shear1(k):
return np.matrix([[1,k],
[0,1]])
shear1(5)*MyShape
fig = go.Figure(
go.Scatter(
x = np.array(MyShape[0,:])[0],
y = np.array(MyShape[1,:])[0],
name = "original",
mode = "lines"
)
)
NewShape = shear1(5)*MyShape
fig.add_trace(
go.Scatter(
x = np.array(NewShape[0,:])[0],
y = np.array(NewShape[1,:])[0],
name = "Shear",
mode = "lines"
)
)
fig.update_layout(
xaxis = dict(range=[-6,6]),
yaxis = dict(range=[-6,6]),
autosize=False,
width=400,
height=400
)
fig.show()