-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathvector.html
More file actions
142 lines (129 loc) · 8.59 KB
/
Copy pathvector.html
File metadata and controls
142 lines (129 loc) · 8.59 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
<!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>VPython Help</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="VPythonDocs/VisualRef.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="800" border="0" cellpadding="1" 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><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="http://vpython.org" target="_blank">Classic VPython web site</a><br />
<a href="license.txt" target="_blank">VPython license</a><br />
<a href="http://www.python.org" target="_blank">Python web site</a> <br /></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.</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">Currently it is not possible to use <span class="attribute"><strong>+=</strong></span>,<strong> <span class="attribute">-=</span></strong>, <span class="attribute"><strong>*=</strong></span><strong>, </strong>or <strong><span class="attribute">/=</span></strong> with vectors.</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 />
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">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"><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=vector(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=vector(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>
<!-- InstanceEndEditable --></td>
</tr>
<tr>
<td height="16" colspan="4"></td>
</tr>
</table>
</body>
<script type="text/javascript" language="javascript" src="navigation.js"></script>
<!-- InstanceEnd --></html>