File: vector.html

package info (click to toggle)
python-visual 1%3A5.12-1.6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 7,708 kB
  • ctags: 7,635
  • sloc: cpp: 15,593; sh: 9,615; ansic: 6,631; python: 4,737; makefile: 384
file content (199 lines) | stat: -rw-r--r-- 11,986 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
191
192
193
194
195
196
197
198
199
<!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="/visual/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-->&nbsp;</td>
    <td width="10" height="272" valign="top" bgcolor="#DDDDDD"><p>&nbsp;</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 a 3D 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="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>
        </select>
        
      <p class="Normal">Work with 3D objects:</p>
        <select name="jumpMenu4" id="jumpMenu5" onchange="MM_jumpMenu('parent',this,0)">
          <option>Choose an option</option>
          <option value="color.html">Color &amp; Opacity</option>
          <option value="lights.html">Lighting</option>
          <option value="materials.html">Materials &amp; Textures</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>
        </select>
        
      <p class="Normal">Windows, Events, &amp; Files:</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="lights.html">Lighting</option>
          <option value="mouse.html">Mouse Events</option>
          <option value="mouse_click.html">&nbsp;&nbsp;&nbsp;Mouse Click</option>
          <option value="mouse_drag.html">&nbsp;&nbsp;&nbsp;Mouse Drag</option>
          <option value="keyboard.html">Keyboard Events</option>
          <option value="controls.html">Buttons and Sliders</option>
          <option value="files.html">Reading/Writing Files</option>
        </select>
        
      <p class="Normal"><a href="vector.html">Vector operations </a></p>
      <p class="Normal"><a href="graph.html">Graphs</a></p>
    <p class="Normal"><a href="factorial.html">factorial/combin</a></p>
    <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-->&nbsp;</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
          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>

          <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 (11 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="program"> mag( vector ) # calculates length of vector <br />
          mag(vector(1,1,1)) # is equal to sqrt(3)<br />
          mag2(vector(1,1,1)) # is equal to 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 of v2 to 5 <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)) equals 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 &quot;difference&quot; 
                  of the angles of the two vectors).</p>

            <p class="program">v1.diff_angle(v2)</p>
            <p class="Normal"> You can also write <span class="attribute">v1.diff_angle(v1,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"> There is a function for 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>

          <p class="program">cross( vector1, vector2 )</p>
          <p class="Normal"> There is a function for 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="program">dot( vector1, vector2 )</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">Rotating a vector</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 equal to 2*pi</p>
          <p class="program">degrees(2*pi) is equal 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>