IdentifiantMot de passe
Mot de passe oublié ?Je m'inscris ! (gratuit)
Description of Objects in VPython

The curve Object

The curve object displays straight lines between points, and if the points are sufficiently close together you get the appearance of a smooth curve. In addition to its basic use for displaying curves, the curve object has powerful capabilities for other uses, such as efficient plotting of functions.

Some attributes, such as pos and color, can be different for each point in the curve. These attributes are stored as Numeric arrays. The Numeric module for Python provides powerful array processing capabilities; for example, two entire arrays can be added together. Numeric arrays can be accessed using standard Python rules for referring to the nth item in a sequence (that is, seq[0] is the first item in seq, seq[1] is the second, seq[2] is the third, etc). For example, anycurve.pos[0] is the position of the first point in anycurve.

You can give curve an explicit list of coordinates enclosed in brackets, like all Python sequences. Here is an example of a 2D square:

square = curve(pos=[(0,0),(0,1),(1,1),(1,0),(0,0)])

Essentially, (1,1) is shorthand for (1,1,0). However, you cannot mix 2D and 3D points in one list.

Curves can have thickness, specified by the radius of a cross section of the curve (the curve has a thickness or diameter that is twice this radius):

curve(pos=[(0,0,0), (1,0,0), (2,1,0)], radius=0.05)

The default radius is 0, which draws a thin curve. A nonzero radius makes a "thick" curve, but a very small radius may make a curve that is too thin to see.

In the following example, the arange() function (provided by the Python Numeric module, which is imported by the Visual module, gives a sequence of values from 0 to 20 in steps of 0.1 (not including the last value, 20).

c = curve( x = arange(0,20,0.1) ) # Draw a helix

c.y = sin( 2.0*c.x )

c.z = cos( 2.0*c.x )

The x, y, and z attributes allow curves to be used to graph functions easily:

curve( x=arange(100), y=arange(100)**0.5,

A function grapher looks like this (a complete program!):

eqn = raw_input('Equation in x: ')

x = arange( 0, 10, 0.1 )

curve( x=x, y=eval(eqn) )

Parametric graphing is also easy:

t = arange(0, 10, 0.1)

curve( x = sin(t), y = 1.0/(1+t), z = t**0.5,
red = cos(t), green = 0, blue = 0.5*(1-cos(t)) )

Here are the curve attributes:

pos[] Array of position of points in the curve: pos[0], pos[1], pos[2]....
The current number of points is given by len(curve.pos)

x[ ], y[ ], z[ ] Components of pos; each defaults to [0,0,0,0,...]

color[ ] Color of points in the curve

red[ ], green[ ], blue[ ] Color components of points in the curve

radius Radius of cross-section of curve
The default radius=0 makes a thin curve


Adding more points to a curve

Curves can be created incrementally with the append() function. A new point by default shares the characteristics of the last point.

helix = curve( color = color.cyan )

for t in arange(0, 2*pi, 0.1):

    helix.append( pos=(t,sin(t),cos(t)) )

One of the many uses of curves is to leave a trail behind a moving object. For example, if ball is a moving sphere, this will add a point to its trail:

trail = curve()

ball = sphere()

...# Every time you update the position of the ball:




The curve machinery interpolates from one point to the next. For example, suppose the first three points are red but the fourth point is blue, as in the following example. The lines connecting the first three points are all red, but the line going from the third point (red) to the fourth point (blue) is displayed with a blend going from red to blue.

c = curve( pos=[(0,0,0), (1,0,0)], )

c.append( pos=(1,1,0) ) # add a red point

c.append( pos=(0,1,0), # add blue point

If you want an abrupt change in color or thickness, simply add another point at the same location. In the following example, adding a blue point at the same location as the third (red) point makes the final line be purely blue.

c = curve( pos=[(0,0,0), (1,0,0)], )

c.append( pos=(1,1,0) ) # add a red point

c.append( pos=(1,1,0), # same point, blue

c.append( pos=(0,1,0) ) # add blue point