MathJs Tutorial 2: Circles and Rectangles
Last updated
Was this helpful?
Last updated
Was this helpful?
In this lesson, students learn two of the important constituents of the Tychos world, namely the object, and the object as well as how to use the object to add vector arrows to visualize matrix quantities.
Students 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.
You are going to learn today 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.
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:
pos
— The initial position of your Circle
in [X,Y]
coordinates.
radius
— The radius of the circle that is drawn in the World View to represent this particle.
color
— The circle will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
image
— A URL that identifies a JPEG, GIF, SVG or PNG image.
opacity
— The circle will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
visible
— The circle can be hidden from view by setting this flag to false
.
motion_map
— This flag tells Tychos to attach a series of strobe images called a motion map.
label
- You can attach a label to the Circle
object by indicating a the text
and color
of the label.
Notice that the attributes follow a pattern:
The first text is the name of the attribute, and then a colon, 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".
This ends the introductory lessons for orienting you to the simulation tool that we will be using frequently to help us discover the scientific models that best describe and predict nature's behavior.
pos
— The initial position of your Rectangle
in [X,Y]
coordinates.
size
— The width and height of the Rectangle
that is drawn in the World View to represent this particle.
color
— The Rectangle
will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
image
— A URL that identifies a JPEG, GIF, SVG or PNG image.
opacity
— The Rectangle
will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
visible
— The Rectangle
can be hidden from view by setting this flag to false
.
motion_map
— This flag tells Tychos to attach a series of strobe images called a motion map.
label
- You can attach a label to the Rectangle
object by indicating a the text
and color
of the label. .
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.
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:
This changes the visual size of the particle on the screen. Notice the use of {
and }
. The text in between these curly braces 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:
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: