-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
113 lines (95 loc) · 3.46 KB
/
main.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
import numpy as np
import numpy.linalg as la
# this returns the magnitude of a vector ai+bj+ck
def magnitude(*vec):
return la.norm(vec)
# this returns the angle between the vector and the axes
def angle(*vec):
mag = magnitude(*vec)
return [np.rad2deg(np.arccos(x / mag)) for x in vec]
#this takes the co-efficients and makes it a vector like form with rectangular unit vectors
def str_vec(x, y, z):
return f"{x}î + {y}ĵ + {z}k̂"
# this returns the sum of two vectors
def sum(i1, j1, k1, i2, j2, k2):
return str_vec(i1 + i2, j1 + j2, k1 + k2)
# this returns the unit vector
def unit(i, j, k):
v = np.array([i, j, k])
mag = magnitude(*v)
return str_vec(*(v / mag if mag != 0 else v))
#this returns the unit vector of the vector connecting the tip of two different vectors
def unitoftwo(i1, j1, k1, i2, j2, k2):
new_i = i2-i1
new_j = j2-j1
new_k = k2-k1
return unit(new_i, new_j, new_k)
# this returns the dot product of two vectors
def dot(i1, j1, k1, i2, j2, k2):
return i1 * i2 + j1 * j2 + k1 * k2
# this returns the cross product of two vectors
def cross(i1, j1, k1, i2, j2, k2):
return str_vec(j1 * k2 - k1 * j2, -1 * (i1 * k2 - k1 * i2), i1 * j2 - i2 * j1)
# angle between two vectors a and b
def angleoftwo(i1, j1, k1, i2, j2, k2):
angle = np.rad2deg(
np.arccos(
dot(i1, j1, k1, i2, j2, k2)
/ (magnitude(i1, j1, k1) * magnitude(i2, j2, k2))
)
)
return angle
# projection of a vector
def projection(i1, j1, k1, i2, j2, k2):
proj_ab = dot(i1, j1, k1, i2, j2, k2) / magnitude(
i2, j2, k2
) # this is the projection of a onto b
proj_ba = dot(i2, j2, k2, i1, j1, k1) / magnitude(
i1, j1, k1
) # this is the projection of b onto a
projection = [proj_ab, proj_ba]
return projection
# component function gives us the x_component and y_component of a vector.
def component(magnitude, theta):
theta = np.deg2rad(theta)
x_comp = magnitude * np.cos(theta)
y_comp = magnitude * np.sin(theta)
comp = [x_comp, y_comp]
return comp
# scaling the whole vector
def scale(magnitude, theta, scalingFactor):
theta = np.deg2rad(theta)
x_comp = scalingFactor * magnitude * np.cos(theta)
y_comp = scalingFactor * magnitude * np.sin(theta)
new_tip = [x_comp, y_comp]
return new_tip
# this xscale function only scale in the x axis
def xscale(magnitude, theta, xscalingfactor):
theta = np.deg2rad(theta)
x_comp = xscalingfactor * magnitude * np.cos(theta)
y_comp = magnitude * np.sin(theta)
new_tip = [x_comp, y_comp]
return new_tip
# this yscale function only scale in the y axis
def yscale(magnitude, theta, yscalingfactor):
theta = np.deg2rad(theta)
x_comp = magnitude * np.cos(theta)
y_comp = yscalingfactor * magnitude * np.sin(theta)
new_tip = [x_comp, y_comp]
return new_tip
# resultant of two vectors when magnitude of both of them and angle between them is given
def resultant(magnitude1, magnitude2, alpha):
alpha = np.deg2rad(alpha)
resultant = np.sqrt(
magnitude1**2 + magnitude2**2 + 2 * magnitude1 * magnitude2 * np.cos(alpha)
)
return resultant
# this angleofR function returns the angle of the resultant vector with respect to first vector
def angleofR(magnitude1, magnitude2, alpha):
alpha = np.deg2rad(alpha)
theta = np.rad2deg(
np.arctan(
(magnitude2 * np.sin(alpha)) / (magnitude1 + magnitude2 * np.cos(alpha))
)
)
return theta