-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathvector.html
More file actions
134 lines (122 loc) · 8.55 KB
/
Copy pathvector.html
File metadata and controls
134 lines (122 loc) · 8.55 KB
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
<!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" --><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>VPython Help</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
<!-- InstanceEndEditable -->
</head>
<body>
<div id="wrapper">
<div id="leftmenu">
<p class="Normal"><a href="index.html"> Home</a></p>
<p class="Normal"> If you're new to Python <br />
and VPython: <a href="VisualIntro.html">Introduction</a></p>
<p class="Normal"> A VPython <a href="VPython_Intro.pdf" target="_blank">tutorial</a></p>
<p class="Normal"><a href="videos.html"> Introductory Videos</a></p>
<p class="Normal"><a href="primitives.html"> Pictures</a> of 3D objects</p>
<p> <select id="menu1" onChange="jumpMenu(this)"></select></p>
<p> <select id="menu2" onChange="jumpMenu(this)"></select></p>
<p> <select id="menu3" onChange="jumpMenu(this)"></select></p>
<p class="Normal"><a href="new_features.html"> What's new</a></p>
<p class="Normal"><a href="https://vpython.org" target="_blank"> Classic VPython web site</a><br />
<a href="license.txt" target="_blank"> VPython license</a><br />
<a href="https://www.python.org" target="_blank"> Python web site</a> <br /></p></td>
</div>
<div id="content">
<!-- InstanceBeginEditable name="content" -->
<h1 class="Heading-1"> <font color="#0000a0">The vector Object</font></h1>
<p class="Normal"> The vector object is not a displayable object but is
a powerful aid to 3D computations. Its properties are similar to
vectors used in science and engineering.</p>
<p class="program">vector(x,y,z)</p>
<p class="Normal">This creates a 3D vector object with the given components x, y, and z.</p>
<p class="Normal"> Vectors can be added or subtracted from each other, or multiplied by an
ordinary number. For example,</p>
<p class="program">v1 = vector(1,2,3)<br />
v2 = vector(10,20,30)<br />
print(v1+v2) # displays <1 22 33><br />
print(2*v1) # displays <2 4 6></p>
<p class="Normal"> You can refer to individual components of a vector:</p>
<p class="Normal"> <span class="attribute">v2.x</span> is 10, <span
class="attribute">v2.y</span> is 20, <span class="attribute">v2.z</span> is 30</p>
<p class="Normal"> It is okay to make a vector from a vector: <span
class="attribute">vector(v2)</span> is still <span class="attribute">vector(10,20,30)</span>. This is a convenient way to make a separate copy of a vector.</p>
<p class="Normal"><strong><font color="#0000a0">Vector functions</font></strong></p>
<p class="Normal">The following functions are available for working with vectors: </p>
<p class="Normal"><strong>mag(A) = A.mag</strong> = |A|, the magnitude of a vector<br />
<br />
<strong>mag2(A) = A.mag2</strong> = |A|*|A|, the vector's magnitude squared<br />
<br />
<strong>norm(A) = A.norm()</strong> = A/|A|, a unit vector in the direction of the vector<br />
<br />
<strong>hat(A) = A.hat</strong> =
A/|A|, a unit vector in the direction of the vector; an alternative to A.norm(), based on the fact that unit vectors are customarily written in the form <strong>ĉ</strong>, with a "hat" over the vector<br />
<br />
For convenience, <span class="attribute">norm(vec(0,0,0))</span> or<span class="attribute"> vec(0,0,0).hat</span> is calculated to be <span class="attribute">vec(0,0,0)</span>. <span class="attribute"></span><br />
<br />
<strong>dot(A,B) = A.dot(B)</strong> = A dot B, the scalar dot product between two vectors<br />
<br />
<strong>cross(A,B) = A.cross(B</strong>), the vector cross product between two vectors<br />
<br />
<strong>diff_angle(A,B) = A.diff_angle(B)</strong>, the angle between two vectors, in radians<br />
<br />
<strong>proj(A,B) = A.proj(B) = dot(A,norm(B))*norm(B)</strong>, the vector projection of A along B<br />
<br />
<strong>comp(A,B) = A.comp(B) = dot(A,norm(B))</strong>, the scalar projection of A along B<br />
<br />
<strong>A.equals(B)</strong> is True if <strong>A</strong> and <strong>B</strong> have the same components (which means that they have the same magnitude and the same direction). You can also use the forms <strong>A == B</strong> or <strong>A != B</strong>.<br />
<br />
<strong>vector.random() </strong>produces a vector each of whose components is a random number in the range -1 to +1<br />
<br />
Some examples:</p>
<p class="program"> mag(A) # calculates length of A<br />
mag(vector(1,1,1)) # = sqrt(3) = 1.732...<br />
mag2(vector(1,1,1)) # = 3, the magnitude
squared</p>
<p class="Normal">It is possible to reset the magnitude or the
magnitude squared of a vector:</p>
<p class="program"> v2.mag = 5 # sets magnitude to 5; no change in direction<br />
v2.mag2 = 2.7 # sets squared magnitude of v2 to
2.7</p>
<p class="Normal">You can reset the magnitude to 1 with norm():</p>
<p class="program">norm(A) # A/|A|, normalized; magnitude of 1<br />
norm(vector(1,1,1)) = vector(1,1,1)/sqrt(3)</p>
<p class="Normal">You can also write <span class="attribute">v1.norm()</span><span
class="attribute"></span> or <span class="attribute">v1.hat</span>. </p>
<p class="Normal">You can change the direction of a vector without changing its magnitude:</p>
<p class="program"> v2.hat = v1 # changes the direction of v2 to that of v1<br />
# but not the magnitude of v2</p>
<p class="Normal">To calculate the angle between two vectors (the "difference"
of the angles of the two vectors).</p>
<p class="program">diff_angle(v1,v2)</p>
<p class="Normal"> You can also write <span class="attribute">v1.diff_angle(v2)</span><span
class="attribute"></span>. For convenience, if either of the vectors has zero
magnitude, the difference of the angles is calculated to be zero<span class="attribute"></span>.</p>
<p class="Normal"> <strong>cross(A,B)</strong> or <strong>A.cross(B)</strong> gives the cross product of two vectors, a vector perpendicular to the plane defined by <strong>A</strong> and <strong>B</strong>,
in a direction defined by the right-hand rule: if the fingers of the right
hand bend from <strong>A</strong> toward <strong>B</strong>, the thumb points in the direction
of the cross product. The magnitude of this vector is equal <strong>mag(A)*mag(B)*sin(diff_angle(A,B))</strong>.</p>
<p class="Normal"> <strong>dot(A,B)</strong> or <strong>A.dot(B)</strong> gives the dot product of two vectors,
which is an ordinary number equal to <strong>mag(A)*mag(B)*cos(diff_angle(A,B))</strong>. If the
two vectors are normalized, the dot product gives the cosine of the angle
between the vectors, which is often useful. </p>
<p class="Normal"><font color="#0000a0"><strong>Rotating a vector</strong></font></p>
<p class="Normal"> There is a function for rotating a vector:<font color="#0000a0"></font></p>
<p class="program"> v2 = rotate(v1, angle=a, axis=vector(x,y,z))</p>
<p class="Normal">The angle must be in radians. The default axis is
(0,0,1), for a rotation in the xy plane around the z axis. There
is no origin for rotating a vector. You can also write <span class="attribute">v2
= v1.rotate(angle=a, axis=vector(x,y,z))</span>. There is also a <a href="rotation.html">rotate capability for
objects</a>.</p>
<p class="Normal">There are functions for converting between degrees
and radians, where there are 2*pi radians in 360 degrees: </p>
<p class="program">radians(360) is equivalent to 2*pi</p>
<p class="program">degrees(2*pi) is equivalent to 360</p>
<!-- InstanceEndEditable -->
</div>
</div>
</body>
<script type="text/javascript" language="javascript" src="navigation.js"></script>
<!-- InstanceEnd --></html>