-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.html
More file actions
217 lines (201 loc) · 13.1 KB
/
vector.html
File metadata and controls
217 lines (201 loc) · 13.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>vector</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
<!-- InstanceEndEditable -->
<script type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<link href="VisualRef.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="800" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutDefaultTable-->
<tr>
<td width="10" valign="top" bgcolor="#FFFFFF"><!--DWLayoutEmptyCell--> </td>
<td width="10" height="272" valign="top" bgcolor="#DDDDDD"><p> </p> </td>
<td width="173" valign="top" bgcolor="#DDDDDD"><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="primitives.html">Pictures</a> of 3D objects</p>
<p class="Normal">Choose an object:</p>
<select name="jumpMenu4" id="jumpMenu4" onchange="MM_jumpMenu('parent',this,0)">
<option>Choose an object</option>
<option value="cylinder.html">Overview</option>
<option value="arrow.html">arrow</option>
<option value="box.html">box</option>
<option value="cone.html">cone</option>
<option value="convex.html">convex</option>
<option value="curve.html">curve</option>
<option value="cylinder.html">cylinder</option>
<option value="ellipsoid.html">ellipsoid</option>
<option value="extrusion.html">extrusion</option>
<option value="faces.html">faces</option>
<option value="frame.html">frame</option>
<option value="helix.html">helix</option>
<option value="label.html">label</option>
<option value="lights.html">lights</option>
<option value="points.html">points</option>
<option value="pyramid.html">pyramid</option>
<option value="ring.html">ring</option>
<option value="sphere.html">sphere</option>
<option value="text.html">text</option>
</select>
<p class="Normal">Work with objects:</p>
<select name="jumpMenu4" id="jumpMenu5" onchange="MM_jumpMenu('parent',this,0)">
<option>Choose an option</option>
<option value="color.html">Color/Opacity</option>
<option value="materials.html">Materials/Textures</option>
<option value="shapes.html">Shapes Library</option>
<option value="paths.html">Paths Library</option>
<option value="defaults.html">Defaults</option>
<option value="rate.html">Animation Speed</option>
<option value="rotation.html">Rotations</option>
<option value="options.html">Additional Options</option>
<option value="delete.html">Delete an Object</option>
<option value="float.html">3/4 = 0?</option>
<option value="controls.html">Buttons/Sliders</option>
<option value="graph.html">Graphs</option>
<option value="lights.html">Lighting</option>
<option value="files.html">Read/Write Files</option>
<option value="trail.html">Leaving a Trail</option>
<option value="vector.html">Vector Operations </option>
<option value="factorial.html">factorial/combin</option>
</select>
<p class="Normal">Windows & Events:</p>
<select name="jumpMenu4" id="jumpMenu6" onchange="MM_jumpMenu('parent',this,0)">
<option>Choose a topic</option>
<option value="display.html">Windows</option>
<option value="mouse.html">Mouse Events</option>
<option value="mouse_click.html"> Mouse Click</option>
<option value="mouse_drag.html"> Mouse Drag</option>
<option value="keyboard.html">Keyboard Events</option>
</select>
<p class="Normal">What's new in <a href="new_features.html">Visual 5</a></p>
<p class="Normal"><a href="http://vpython.org" target="_blank">VPython web site</a><br />
<a href="license.txt" target="_blank">Visual license</a><br />
<a href="http://www.python.org" target="_blank">Python web site</a> <br />
<a href="http://www.python.org/doc/2.5.2/lib/module-math.html" target="_blank">Math module</a> (sqrt etc.)<br />
<a href="http://www.scipy.org/Documentation" target="_blank">Numpy module</a> (arrays) </p></td>
<td width="21" valign="top" bgcolor="#FFFFFF"><!--DWLayoutEmptyCell--> </td>
<td width="586" rowspan="2" valign="top"><!-- 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. It can be used together with
numpy arrays . (numpy is a module added to Python to provide
high-speed computational capability through optimized array processing.
The numpy module is imported automatically by Visual.)</p>
<p class="program">vector(x,y,z)</p>
<p class="Normal"> Returns a vector object with the given components, which are made to be
floating-point (that is, 3 is converted to 3.0).</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>.</p>
<p class="Normal"> The form <span class="attribute">vector(10,12)</span> is shorthand for <span class="attribute">vector(10,12,0)</span>.</p>
<p class="Normal"> A vector is a Python sequence, so <span
class="attribute">v2.x</span> is the same as <span class="attribute">v2[0]</span>, <span class="attribute">v2.y</span> is the same as <span
class="attribute">v2[1]</span>, and <span class="attribute">v2.z</span> is the same as <span class="attribute">v2[2]</span>.</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>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>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>diff_angle(A,B) = A.diff_angle(B)</strong>, the angle between two vectors, in radians<br />
<br />
<strong>rotate(A,theta,B) = A.rotate(theta,B) = rotate(vector=A, angle=theta, axis=B)</strong>, result of rotating A through theta around B<br />
<br />
<strong>astuple(A) = A.astuple()</strong>, convert this vector to a tuple, much faster than using tuple(A)<br />
<br />
Some examples:</p>
<p class="program"> mag( vector ) # calculates length of vector <br />
mag(vector(1,1,1)) # = sqrt(3) = 1.732...<br />
mag2(vector(1,1,1)) # = 3, the magnitude
squared</p>
<p class="Normal"> You can also obtain the magnitude in the form <span
class="attribute">v2.mag</span> and the square of the magnitude as <span
class="attribute">v2.mag2</span>.</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( vector ) # 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>. For convenience, <span class="attribute">norm(vector(0,0,0))</span> is calculated to be <span class="attribute">vector(0,0,0)</span>. <span class="attribute"></span> </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> 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"> dot(A,B) 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">You can also say <span class="attribute">vector1.cross(vector2)</span> or <span class="attribute">vector1.dot(vector2)</span>).</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=theta, axis=(1,1,1))</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=theta, axis=(1,1,1))</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>
<p class="Normal"><strong><font color="#0000a0">Convenient conversion</font></strong></p>
<p class="Normal"> For convenience Visual automatically converts (a,b,c) into
vector(a,b,c), with floating-point values, when creating Visual objects:
sphere.pos=(1,2,3) is equivalent to sphere.pos=vector(1.,2.,3.). However,
using the form (a,b,c) directly in vector computations will give errors,
because (a,b,c) isn't a vector; write vector(a,b,c) instead.</p>
<p class="Normal">You can convert a vector <font color="#FF0000">vec1</font> to a Python tuple (a,b,c) by <font color="#FF0000">tuple(vec1)</font> or
by the much faster option <font color="#FF0000">vec1.astuple(</font>).</p>
<!-- InstanceEndEditable --></td>
</tr>
<tr>
<td height="16" colspan="4"></td>
</tr>
</table>
</body>
<!-- InstanceEnd --></html>