File: vector.html

package info (click to toggle)
python-visual 3.2.9-4.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,796 kB
  • ctags: 2,664
  • sloc: cpp: 11,958; sh: 8,185; python: 3,709; ansic: 480; makefile: 311
file content (190 lines) | stat: -rw-r--r-- 8,646 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <meta name="GENERATOR"
 content="Adobe FrameMaker 5.5/HTML Export Filter">
  <link rel="STYLESHEET" href="VisualRef.css" charset="ISO-8859-1"
 type="text/css">
  <title>Description of Objects in VPython</title>
</head>
<body bgcolor="#ffffff">
<div class="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
  <tbody>
    <tr>
      <td><a href="float.html"><img src="icons/previous.gif" border="0"
 height="32" alt="Previous Page" width="32"></a></td>
      <td><a href="index.html"><img src="icons/up.gif" border="0"
 height="32" alt="Up One Level" width="32"></a></td>
      <td><a href="graph.html"><img src="icons/next.gif" border="0"
 height="32" alt="Next Page" width="32"></a></td>
      <td align="center" width="100%">Visual Reference</td>
      <td><a href="index.html"><img src="icons/contents.gif" border="0"
 height="32" alt="Contents" width="32"></a></td>
      <td><img src="icons/blank.gif" border="0" height="32" alt=""
 width="32"></td>
      <td><img src="icons/blank.gif" border="0" height="32" alt=""
 width="32"></td>
    </tr>
  </tbody>
</table>
<b class="navlabel">Previous:</b> <a class="sectref" href="float.html">Floating
Division</a>
<b class="navlabel">Up:</b> <a class="sectref" href="index.html">Contents</a>
<b class="navlabel">Next:</b> <a class="sectref" href="graph.html">Graph
Plotting</a>
<br>
<hr></div>
<div>
<h1 class="Heading-1"> <font color="#0000a0">The vector Object</font></h1>
</div>
<div>
<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
Numeric arrays. (Numeric is a module added to Python to provide
high-speed computational capability through optimized array processing.
The Numeric module is imported automatically by Visual.)</p>
<div>
<h2 class="program0">vector(x,y,z)</h2>
<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>
</div>
<div>
<h2 class="program0">v1 = vector(1,2,3)</h2>
<p class="program">
v2 = vector(10,20,30)</p>
<p class="program">
print v1+v2 # displays (11 22 33)</p>
<p class="program">
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>
</div>
<div>
<h2 class="program0"> mag( vector ) # calculates length of vector </h2>
<h2 class="program0"> mag(vector(1,1,1)) # is equal to sqrt(3)</h2>
<h2 class="program0">mag2(vector(1,1,1)) # is equal to 3, the magnitude
squared</h2>
<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>
<div>
<p class="Normal"></p>
</div>
<div>
<h2 class="program0"> v2.mag = 5 # sets magnitude of v2 to 5 </h2>
<h2 class="program0"> v2.mag2 = 2.7 # sets squared magnitude of v2 to
2.7</h2>
</div>
<p class="Normal">You can reset the magnitude to 1 with norm():</p>
</div>
<div>
<h2 class="program0">norm( vector ) # normalized; magnitude of 1</h2>
<h2 class="program0">norm(vector(1,1,1)) equals vector(1,1,1)/sqrt(3)</h2>
<p class="Normal">You can also write <span class="attribute">v1.norm()</span><span
 class="attribute"></span>. Since <span class="attribute">norm(v1) =
v1/mag(v1)</span>, it is not possible to normalize a zero-length
vector: <span class="attribute">norm(vector(0,0,0))</span> gives an
error, since division by zero is involved. <span class="attribute"></span>
</p>
<div>
<h2 class="program0">vector1.diff_angle(vector2)</h2>
<p class="Normal">
Calculates the angle between two vectors (the "difference" of the
angles of the two vectors)..</p>
</div>
<p class="Normal"><span class="attribute"></span></p>
</div>
<div>
<h2 class="program0">cross( vector1, vector2 )</h2>
<p class="Normal">
Creates the cross product of two vectors, which is a vector
perpendicular to the plane defined by vector1 and vector2, in a
direction defined by the right-hand rule: if the fingers of the right
hand bend from vector1 toward vector 2, the thumb points in the
direction of the cross product. The magnitude of this vector is equal
to the product of the magnitudes of vector1 and vector2, times the sine
of the angle between the two vectors.</p>
</div>
<div>
<h2 class="program0">dot( vector1, vector2 )</h2>
<p class="Normal"> Creates the dot product of two vectors, which is an
ordinary number equal to the product of the magnitudes of vector1 and
vector2, times the cosine of the angle between the two vectors. 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">&nbsp; </p>
<p class="Normal"><font color="#0000a0">Rotating a vector</font></p>
<h2 class="program0">
v2 = rotate(v1, angle=theta, axis=(1,1,1))</h2>
<p class="Normal"> 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.
Notice too that rotating a vector involves a function, <span
 class="attribute">v = rotate()</span>, as is the case with other
vector manipulations such as dot() or cross(), whereas rotation of
graphics objects involves attributes, in the form <span
 class="attribute">object.rotate()</span>.</p>
<p class="Normal">&nbsp; </p>
<p class="Normal"><font color="#0000a0">Convenient conversion</font></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.as_tuple(</font>).</p>
</div>
</div>
<br>
<hr>
<div class="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
  <tbody>
    <tr>
      <td><a href="float.html"><img src="icons/previous.gif" border="0"
 height="32" alt="Previous Page" width="32"></a></td>
      <td><a href="index.html"><img src="icons/up.gif" border="0"
 height="32" alt="Up One Level" width="32"></a></td>
      <td><a href="graph.html"><img src="icons/next.gif" border="0"
 height="32" alt="Next Page" width="32"></a></td>
      <td align="center" width="100%">Visual Reference</td>
      <td><a href="index.html"><img src="icons/contents.gif" border="0"
 height="32" alt="Contents" width="32"></a></td>
      <td><img src="icons/blank.gif" border="0" height="32" alt=""
 width="32"></td>
      <td><img src="icons/blank.gif" border="0" height="32" alt=""
 width="32"></td>
    </tr>
  </tbody>
</table>
<b class="navlabel">Previous:</b> <a class="sectref" href="float.html">Floating
Division</a>
<b class="navlabel">Up:</b> <a class="sectref" href="index.html">Contents</a>
<b class="navlabel">Next:</b> <a class="sectref" href="graph.html">Graph
Plotting</a>
<br>
<hr></div>
</body>
</html>