VPython Object: Difference between revisions
m (claimed page) |
(updated intro and 'The Main Idea' section) |
||
Line 1: | Line 1: | ||
Claimed by | Claimed by Vnistala3 (F2017) | ||
Claimed by Ddebord3 (2015) | Claimed by Ddebord3 (2015) | ||
A VPython Object is a representation of data in a specific in VPython both visually and numerically. Each object represents data through its attributes, specific characteristics assigned to each individual object. | A VPython Object is a representation of data in a specific in VPython both visually and numerically. Each object represents data through its attributes, specific characteristics assigned to each individual object. Objects play an essential role in the necessary knowledge for the coding portion of PHYS 2211 and 2212, as most of your programs will require the extensive usage and manipulation of objects. This page is intended to provide you a background on these objects. | ||
== | ==The Main Idea== | ||
Objects in VPython are intended to represent data in ways that can be easily visualized and understood by both the user and the computer. Each object has a type it belongs to, which determines its default attributes, as well as how the computer will choose to display it when the program runs. "Attribute" essentially refers to any name following a dot — for example, in the expression abc.xyz, xyz is an attribute of the object abc. Objects must be uniquely named if you wish to refer to them again later in your program, and any graphical object you create (spheres, boxes, curves, arrows, etc.) continue to exist for as long as your program remains running - VPython will continue to display them regardless of where they are positioned. If you change an attribute of an object, such as its position or color, VPython will automatically display the object in its new location and/or with its new color. | |||
For example, let's say you create a sphere called earth - by default, earth has attributes pre-assigned to it, such as earth.pos (the .pos attribute signifies the position of the sphere) or earth.color</code> (the .color attribute allows you to change the color of the sphere). Also, in addition to the preset attributes, you can also create new ones. Besides the position and radius, you can also create attributes for mass (earth.mass), velocity (earth.vel), momentum (earth.momentum), or anything else you see fit. Of course, not all attributes need to be added manually. They can be left blank and filled in with a default value which the program automatically assigns them | |||
=== | The general syntax for creating an object is as follows. | ||
Many objects in VPython carry common attributes regardless of type. | |||
objectName = objectType([insert attributes here]) | |||
===Attributes=== | |||
Many objects in VPython carry common attributes regardless of their type. This section will cover the most significant ones. | |||
====Pos (Position)==== | ====Pos (Position)==== |
Revision as of 19:35, 29 November 2017
Claimed by Vnistala3 (F2017) Claimed by Ddebord3 (2015)
A VPython Object is a representation of data in a specific in VPython both visually and numerically. Each object represents data through its attributes, specific characteristics assigned to each individual object. Objects play an essential role in the necessary knowledge for the coding portion of PHYS 2211 and 2212, as most of your programs will require the extensive usage and manipulation of objects. This page is intended to provide you a background on these objects.
The Main Idea
Objects in VPython are intended to represent data in ways that can be easily visualized and understood by both the user and the computer. Each object has a type it belongs to, which determines its default attributes, as well as how the computer will choose to display it when the program runs. "Attribute" essentially refers to any name following a dot — for example, in the expression abc.xyz, xyz is an attribute of the object abc. Objects must be uniquely named if you wish to refer to them again later in your program, and any graphical object you create (spheres, boxes, curves, arrows, etc.) continue to exist for as long as your program remains running - VPython will continue to display them regardless of where they are positioned. If you change an attribute of an object, such as its position or color, VPython will automatically display the object in its new location and/or with its new color.
For example, let's say you create a sphere called earth - by default, earth has attributes pre-assigned to it, such as earth.pos (the .pos attribute signifies the position of the sphere) or earth.color (the .color attribute allows you to change the color of the sphere). Also, in addition to the preset attributes, you can also create new ones. Besides the position and radius, you can also create attributes for mass (earth.mass), velocity (earth.vel), momentum (earth.momentum), or anything else you see fit. Of course, not all attributes need to be added manually. They can be left blank and filled in with a default value which the program automatically assigns them
The general syntax for creating an object is as follows.
objectName = objectType([insert attributes here])
Attributes
Many objects in VPython carry common attributes regardless of their type. This section will cover the most significant ones.
Pos (Position)
Pos is a common attribute held by almost every object used to describe the position of that object's center or its endpoint. It is of the vector type and has three components of its own. It can be accessed using objectName.pos. Its individual components can be accessed using objectName.pos.direction where direction is either an x, y, or z depending on the component desired.
Color
Color is an attribute that defines the object's color when it appears in the display window. This attribute is important simply for distinguishing objects from one another. Color can be assigned in one of two ways. The first way it can be assigned is through a pre-set color such as red or cyan. The syntax for creating a red color is:
color = color.red.
Another way of assigning color is through an RGB vector. The vector contains three numbers from 0 to 1. The first number represents the red saturation, the second represents green, and the third represents blue. The higher the value of the number, the more it resembles that particular color. The syntax for doing this is:
color = (1, 0.5, 0).
Common Objects
In order to display the data effectively, there are some common objects that are important to understand.
Sphere
A sphere object is a representation of a sphere with a set radius, position, and color. A magenta sphere center centered at the origin with radius one can be created through the syntax:
nameSphere = sphere(radius = 1, pos = vector(0,0,0), color=color.magenta)
Arrow
An arrow object creates an arrow with a position, an axis, and a color. The axis is a vector representing the arrow's direction and length. Position is the location of its tail, not its center. A yellow arrow centered at (0,7,2) in the direction of (3,-9,1) can be created through:
nameArrow = arrow(pos=(0,7,2), axis=(3,-9,1), color=color.yellow)
Curve
A curve is series of connections between points. This can be used to show the path of an object through the append function. This function adds an additional point to the curve allowing the curve to follow each position added. To create a red curve and add the point (4,4,4) use the syntax:
nameCurve = curve(color.color = red)
nameCurve.append(4,4,4)
Examples
Simple
Create a green sphere named "answer" with a radius of .7 located at the origin.
Use the following to create the sphere:
answer = sphere(radius=.7, pos=vector(0,0,0), color=color.green)
Middling
Create two objects. One, a magenta sphere of radius 3.5 named sphereOne at the position (9,0,8). Two, a cyan arrow named arrowTwo that points from the origin to the center of sphereOne.
sphereOne = sphere(radius=3.5, pos=vector(9,0,8), color=color.magenta)
position = vector(0,0,0)
arrowTwo = arrow(pos=position, axis=(sphereOne.pos - position), color = color.cyan)
Note: the position vector is used here because arrowTwo.pos cannot be called yet. The arrow has yet to be created in line 3.
Difficult
Create a two spheres and a red curve. Make the first sphere have a radius of 2 and locate it at (-4,-3,8). Make the second sphere have a radius of 1, and locate it at (6,11,0). Start the curve at the origin, then bring it to the first sphere, and then the second sphere.
sphereOne = sphere(radius = 2, pos=vector(-4,-3,8))
sphereTwo = sphere(radius = 1, pos=vector(6,11,0))
curveOne = curve(color=color.red, pos = (0,0,0))
curveOne.append(sphereOne.pos)
curveOne.append(sphereTwo.pos)
Connectedness
This topic is important because objects are the primary way to visually make sense of data managed by the program. Understanding this process will allow the user to implement physics concepts such as moving charges and electric fields into computer code form.
See Also
References
http://vpython.org/contents/docs/color.html
http://vpython.org/contents/docs/primitives.html This section contains the the references you used while writing this page