Python Tutorial 2: Circles and Rectangles
Last updated
Was this helpful?
Last updated
Was this helpful?
In this tutorial, you will learn two of the important objects of the Tychos world, namely the object, and the object as well as how to use the object to visualize vector quantities.
You will learn:
How to create objects.
How to create objects.
How to change certain attributes such as the position, size and color of these objects.
How to represent these objects using images rather than the default graphic representations.
Click on the link below to open the Tychos scenario in a new browser tab where you can follow along, or use the embedded frame that contains the scenario below:
Now it's time to actual simulate something in the simulated environment, and we are going to define the simplest, most basic objects of our simulation - the rectangle
and circle
objects.
Let's learn how you can create simulated objects in Tychos and how you can place those objects at different positions within the simulated 2D space.
objects are meant to be representative of any object in your simulated world. They could represent a spherical object such as a planet or a ball, but could also represent any object, as long as it serves your purpose of simply representing the position of the object.
If all you need is a general sense of the object's size and position, then a Tychos is probably the object that you are going to use.
image
— A URL that identifies a JPEG, GIF, SVG or PNG image.
motion_map
— This flag tells Tychos to attach a series of strobe images called a motion map. Note: You also have to have the Motion Map setting turned on in the Settings pane.
label_color
- This specifies the color of the label text.
Notice that the attributes follow a pattern:
The first text is the name of the attribute, and then an equals sign, and then the value of the attribute. The order that you have them in does not matter, just as long as you get the names correct:
This line of code is read "The object c1's position is now x = 100 and y = 0".
image
— A URL that identifies a JPEG, GIF, SVG or PNG image.
motion_map
— This flag tells Tychos to attach a series of strobe images called a motion map. Note: You also have to have the Motion Map setting turned on in the Settings pane.
label_color
- This specifies the color of the label text.
You are not limited to visually representing your objects using only simple circles and rectangles. You can also use any image from the web.
This is done by changing the image
attribute like this:
Tychos will now replace the simple image of a solid colored circle with this image:
It is recommended that you use either svg images that are scalable, or png and gif files that allow you to store transparency values.
Keep in mind that the physical dimensions of the image will be scaled to the object size
defined in either the radius
attribute or the size attribute of the object.
To create a in the simulation, you need to first identify a valid variable name to store the reference to the object. You then assign the variable to the result of a command that invokes or essentially brings the object into existence:
This particular object is created and placed in the virtual world at the position of x = 0 and y = 0 with a default radius of 10 and a default color.
The above code uses the default attributes of a object, but you can actually create one with your own initial values. For example, you can also define the size of the .
Here is how you can create a object with a radius of 10, and a color of green:
This changes the visual size of the particle on the screen. The text inside the parentheses is a list of name and value pairs that define the attributes of the object. There a number of these attributes that can be set or changed in your simulation:
pos
— The initial position of your in x,y
coordinates.
radius
— The radius of the that is drawn in the World View to represent this particle.
color
— The will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
opacity
— The will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
visible
— The can be hidden from view by setting this flag to false
.
label_text
- You can attach a label to the object by indicating the text
and color
of the label.
border_size
- The object can be displayed with a border with a stroke width.
border_color
- The object's border color.
border_style
- The object's border can be set to dash
or dot
, or none
by default.
So for example, you can also change the initial position or color of the object when you create one:
You can create more than one , you just have to assign it a different variable name. The code below creates two different objects. The names that you use can be almost anything:
Again, here the objects are representing a green car and another representing a red car.
You can change the position of a object by referencing its "pos" attribute. This is done by using a common computer science notation called "dot" notation. Its quite easy:
The object is another Tychos object that you can use to represent of an object in your simulated world. They could represent objects such as a box, or a table top, but could also represent any object. The big difference between a object and a object is that objects have a length and a width rather than a radius.
If you need an object that visually represents the important difference between the length and width of the object, then a Tychos object is probably the object that you are going to use.
To create a object in the simulation, once again you need to first identify a valid variable name to store the reference to the object. Just like with the the object, you create a object by assigning the variable to the result of a command that invokes or essentially brings the object into existence:
This object is created and placed in the virtual world at the position of x = 0 and y = 0 with a default length and width of 5 and 5.
Just as with the object, you can override the default attribute values of your object by giving it a set of name: value pairs in a list:
Rather than defining a radius as the visual size attribute, the size attribute here gives your object a width (or X size value) and length (or Y size value).
The other attributes of the object are very similar to the object. Here is a full listing of those attributes:
pos
— The initial position of your in x, y
coordinates.
size
— The width and height of the that is drawn in the World View to represent this particle.
color
— The will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
opacity
— The will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
visible
— The can be hidden from view by setting this flag to false
.
label_text
- You can attach a label to the object by indicating the text
and color
of the label.
border_size
- The object can be displayed with a border with a stroke width.
border_color
- The object's border color.
border_style
- The object's border can be set to dash
or dot
, or none
by default.