Links

Tychos Python Language Reference

The following reference guide identifies the language syntax, built in variables, functions, and classes that are used to code your simulations using the Python Language. We have included documentation here for some of the helpful functions defined in the Python language. This is not a complete list of all functions available in Python, just ones that might be commonly used in Tychos for building scenarios as well as defining goals for those scenarios.

Comments

Comments are statements that you can insert in your code that do not get interpreted and executed by Tychos. To create a comment, you simply indicate a comment by using a hashtag:
# This is a comment.

Variables

To define a variable in Tychos all you need to do is identify a name for the variable. You then type an = sign to assign a value to the variable. The following demonstrates various variable declarations
# Assigns a variable called x the value of 10
x = 10
# Assign a new variable y the value of the x
y = x
# Assign x the value of itself plus 1
x = x + 1

Built-in Scenario Variables

There are a few variables that are built into Tychos. These variables are:
  • t — How many seconds has passed since this Scenario was started?
  • dt — Time between frames as seconds, e.g. 0.1 is 1/10th of a second.
  • frame_count — How many frames have passed? e.g. 1, 2, 3...

Common Math Operators and Functions

These are some common mathematical operators and functions for performing various calculations.

Mathematical Operators

Tychos uses the following operators to perform basic math calculations:
  • + — Addition
  • - — Subtraction
  • * - Multiplication
  • / - Division
  • ** - Exponent
  • % - Modulo

Basic Math Functions

You can also use the following basic math functions:

sqrt(positive_number)

The sqrt(positive_number) function takes a single non negative number and returns the real square root of the number.
# returns number 2
sqrt(4)

exp(power)

The exp(power) function calculates Euler's number (e) to the given power.
# returns 54.59815
exp(4)

log(x)

The log(x) returns the natural log (base e) of x.
# returns number 2
abs(-2)

ceil(x)

The ceil(x) function rounds a number up to the nearest integer.
# returns 5
exp(4.1)

floor(x)

The floor(x) function rounds a number down to the nearest integer.
# returns 4
exp(4.9)

sign(x)

The sign(x) function returns +1 if x is less than 0, -1 if x is greater than 0, and 0 if x is equal to 0.
# returns -1
sign(-42)
# returns 1
sign(42)
# returns 0
sign(0)

abs(x)

The abs(x) function returns the absolute value of a number.
# returns number 2
abs(-2)

random()

The random() function returns a semi random number between 0 and 1.
x = random()
randrange(a, b)
The randrange(a,b) function returns a semi random number between a and b.
x = randrange(10, 100)

factorial(x)

The factorial(x) function calculates the product of all positive integers less than or equal to the given positive integer.
# x! = x*(x-1)*(x-2)....(1)
# returns 120
factorial(5)

combin(n, r)

The combin(x,y) function calculates the number of ways to choose a sample of r elements from a set of n distinct objects where order does not matter and replacements are not allowed.
# x!/(y!*(x-y)!)
# returns 10
combin(5,2)

Trigonometric Functions

The following functions all use radians as the angle measurement. You can use pi to represent PI.

sin(angle)

The sin(angle) function is used to evaluate the trigonometric sine value for a given input angle. The input angle must be provided in radians.
# returns number 1
sin(pi/2)

cos(angle)

The cos(angle) function is used to evaluate the trigonometric cosine value for a given input angle. The input angle must be provided in radians.
# returns number 1
cos(0)

tan(angle)

The tan(angle) function is used to evaluate the trigonometric tangent value for a given input angle. The input angle must be provided in radians.
# returns number 1
tan(pi/4)

asin(value)

The asin(value) function is used to evaluate the trigonometric arcsine value (inverse sine) for a given input. The output angle is given in radians.
# returns number 1.57
asin(1)

acos(value)

The acos(value) function is used to evaluate the trigonometric arccosine value (inverse cosine) for a given input. The output angle is given in radians.
# returns number 0
acos(1)

atan2(x, y)

The atan2(value) function is used to evaluate the trigonometric arctangent value (inverse tangent) for a given x and y input. The output angle is given in radians.
# returns number -0.785
atan2(-1, 1)
# returns 2.36
atan2(1, -1)
radians(angle)
The input is an angle measurement in degrees and the output is the angle measurement in radians.
# returns number .785
radians(45)
degrees(angle)
The input is an angle measurement in radians and the output is the angle measurement in degrees.
# returns number 180
degrees(pi)

Vector and Vector Functions

The following functions provide operations for creating and using vectors in calculations.

vec(x, y), or vec(x,y,z)

Creates a Vector object representing a 3d vector with components identified as x, y, and z.
# Create a vector with components x=2, y=2, z=0
v1 = vec(2,2)
# Create a vector with components x=2, y=2, z=2
v2 = vec(2,2,2)
# Change a component of the vector:
v1.x = 3

Adding and Subtracting Vectors

Vectors can be added or subtracted from one another using standard +, - operators:
v1 = vec(2,2)
v2 = vec(1,1)
# Add the two vectors together:
v3 = v1 + v2 # Returns vec(3,3,0)

Multiplying and Dividing By A Scalar

Vectors can also by scaled using the standard *, / operators:
v1 = vec(2,2)
# Multiply by a scalar
v2 = 2 * v1 # Returns vec(4,4,0)
# Divide by a scalar
v3 = v1 / 2 # Returns vec(1,1,0)

dot(v1, v2)

Calculates the dot product of two Vector objects.
# Work is the dot product of Force (F) and displacement (r)
F = vec(2, 2)
r = vec(3, 3)
# returns 12
Work = dot(F, r)

cross(v1, v2)

Calculates the cross product for two vectors in three dimensional space.
# Torque is the cross product of Force and moment arm (r)
F = vec(2, 0, 0)
r = vec(0, 2, 0)
# returns Vector vec(0, 0, 4)
cross(F, r)

norm(vec)

Same as hat. See below.

hat(vec)

This function returns a unit vector representation of the given input vector. Its magnitude is 1 and the direction matches the direction of the input vector. This can be useful if you need just the direction of a vector, but not its magnitude.
  • vec - any two or three dimensional vector.
Example:
u = hat(vec(3, 4)) # returns vec(0.6, 0.8, 0)
mag(u) # returns 1

mag(vec)

This function returns the scaler magnitude of any given vector. This is helpful when you want to know the length of a vector, for example, if you want the magnitude of a vector, but not the direction.
  • vec - any two or three dimensional vector.
Example:
mag(vec(3, 4)) # returns 5
mag2(vec)
This function returns the scaler squared magnitude of any given vector.
  • vec - any two or three dimensional vector.
Example:
mag2(vec(3, 4)) # returns 25

diff_angle(vec1, vec2)

This function returns the angle between two vectors. The angle is returned in radians.
  • vec1 - any two or three dimensional vector.
  • vec2 - any two or three dimensional vector.
Example:
diff_angle(vec(4, 4), vec(4, 0)) # returns 0.7853981633974484

diff_angle(vec1, vec2)

This function returns the angle between two vectors. The angle value is returned in radians.
  • vec1 - any two or three dimensional vector.
  • vec2 - any two or three dimensional vector.
Example:
diff_angle(vec(4, 4), vec(4, 0)) # returns 0.7853981633974484

proj(vec1, vec2)

This function returns the vector projection of vec1 along vec2.
  • vec1 - any two or three dimensional vector.
  • vec2 - any two or three dimensional vector.

comp(vec1, vec2)

This function returns the scalar projection of vec1 along vec2.
  • vec1 - any two or three dimensional vector.
  • vec2 - any two or three dimensional vector.

distance(point1, point2)

This function returns a scalar quantity representing the distance between two points.
  • point1 - any two or three dimensional vector representing a point in space.
  • point2 - any two or three dimensional vector representing a point in space.
Example:
distance(vec(5,5), vec(3,3)) # returns 2.8284271247461903

direction(vec)

This function returns a scalar angle measurement. This is helpful when you want to know the direction of a vector, like the direction of a velocity vector, or the direction of a force vector. The returned angle is given in radians, measured from the positive x axis.
  • vec - any two or three dimensional vector.
Example:
direction(vec(4, 4)) # returns 0.7853981633974483

polar(radius, angle)

This function returns a two-dimensional vector representing the cartesian components of a polar coordinate corresponding to the magnitude of the radius and the radial angle.
  • radius - scalar quantity representing the scalar distance of the radius of the
  • angle - scalar quantity representing the angle measurement of the polar coordinate.
Example:
polar(10, pi/4) # returns vec(7.07, 7.07, 0)

Other Useful Functions

Some other useful functions..._

rgb(r, g, b)

This function returns a hexadecimal value for the red, green, and blue input values. This can be useful for setting the color of an object based on the RGB color model.
  • r - a value between 0 and 255 representing the RED of RGB.
  • g - a value between 0 and 255 representing the GREEN of RGB.
  • b- a value between 0 and 255 representing the BLUE of RGB.
Example:
color = rgb(100, 100, 0) # returns '#646400'

stop()

This function interrupts the simulation and stops it at the current frame.
stop() -> stops the simulation.
Example:
if buggy.pos.x > 100:
stop() # simulation stops

Collision Functions

Currently only circle objects and rectangle objects work with any of the collision functions. We are working to add other objects soon.
The following functions are meant to help users model collisions more easily. These functions could be used for other purposes rather than modeling collisions as Tychos is not a physics engine. These functions could be thought of as "overlap" functions. They return information about the overlap of objects.

has_collided(source, target)

This function returns a boolean true/false value when two objects are given as the source and target. It returns false if the two objects are not determined to be overlapping.
  • source - circle or rectangle object
  • target - circle or rectangle object
Example:
# Initial State
p1 = circle(pos=vec(15, 0), radius=10, color=rgb(0, 200, 200), opacity=.6)
b1 = rectangle(pos=vec(0, 0), size=vec(15, 15), color=rgb(200, 0, 200), opacity=.6)
b1.rotate(radians(45))
p3 = circle(pos=vec(-15, 0), radius=10, color=rgb(0, 0, 200), opacity=.6)
# Calculations
has_collided(p1, b1) # returns True
has_collided(b1, p3) # returns True
has_collided(p1, p3) # returns False

get_mtv(source target)

This function returns a two dimensional vector representing the minimum translation vector (MTV) that would be needed to separate two objects when they overlap. This can be used to simulate collision forces or to move objects apart based on the magnitude and direction of the MTV.
  • source - circle or rectangle object
  • target - circle or rectangle object
Example:
# Initial State
p1 = circle(pos=vec(0, 0), radius=10, color=rgb(200, 200, 200), opacity=.6)
p2 = circle(pos=vec(12, 10), radius=10, color=rgb(0, 200, 0), opacity=.6)
b1 = rectangle(pos=vec(-12, 0), size=vec(15, 15), color=rgb(200, 0, 0), opacity=.6)
mtv1 = arrow(pos=p1.pos, color="green")
mtv2 = arrow(pos=p1.pos, color="red")
# Calculations
mtv1.size = get_mtv(p1, p2)
mtv2.size = get_mtv(p1, b1)

Logical Operators

The following operators are used to execute logical AND and OR comparisons for the use of evaluating logical statements.

and

This is used for performing a logical AND conjunction. For example, "A and B" is true only if A is true and B is true. "A and B" is false if either A or B is false.
A = true
B = true
A and B # returns true
B = false
A and B # returns false
You can use the and operator to test if two comparisons are both true:
x = 2
(x < 3) and (x == 2) # returns true
(x < 3) and (x != 2) # returns false

or

This is used for performing a logical OR conjunction. For example, "A or B" is true if A or B is true. "A or B" is false only if A and B are both false.
A = true
B = false
A or B # returns true
A = false
A or B # returns false
You can use the or operator to test if one of two comparisons are true:
x = 2
smaller(x, 3) or equal(x, 3) # one is true, so returns true
(x < 1) or (x == 3) # both are false, so returns false

Built-in Classes

Tychos has only a few classes that are used to create the basic simulated objects in the Tychos universe as well as a few tools for analyzing those objects in the simulated world. The graphical objects in Tychos that can be used are the circle, the rectangle, the arrow, the line, the label, the spring and the poly_line.

circle

A circle is drawn as a colored circle in the World View.
Could not load image
Below is the constructor for the circle class that shows its default values:
circle( pos=vec(0,0), radius=10, color=default_color, border_size=0, border_color=default_color, border_style="none" image="", opacity=1, visible=True, motion_map=False, label_text="" label_color=default_color )
  • pos — The initial position of your circle as a vector with x,y,z coordinates.
  • radius — The radius of the circle.
  • color — The circle will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • border_size — The border thickness in pixels.
  • border_color — The border will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • border_style — Sets the border as either solid (default = "none") or "dash" for a dashed border, or "dot" for a dotted border.
  • 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_text - The text of an attached label.
  • label_color - The color of the attached label text.
These attributes may also be modified on the circle after it is created. In particular, one will usually change the pos attribute of a circle in the Calculations editor to show movement. e.g.
# Initial State editor
c = circle()
c_big = circle(pos:vec(50, 0), radius=25)
c_big.color = "red"
c_green = circle(pos=vec(100, 0), color="green", opacity=.5)
# Calculations editor
c.pos = c.pos + vec(1, 0.25)

circle.rotate()

circle objects can be rotated.
circle.rotate(angle=0, axis=vec(0, 0)) — Rotates the circle object by a given angle value in radian units. You can also provide an optional vector that identifies the axis of rotation. This method should only be called from the Calculations code editor.

circle.image

circle objects can also be represented with an image by setting the image attribute of the object. The text must be a URI link to a graphic file that can be a PNG, SVG, GIF, or JPEG image.
rocket = circle(pos=vec(0, 0), radius=10)
rocket.image = "https://upload.wikimedia.org/wikipedia/commons/3/3d/Spacecraft.png"
A Circle that looks like a rocket.
The above image also demonstrates the use of the direction function as well as the rotate method:
rocket.rotate(direction(rocket.v))

Ellipse

A ellipse is drawn as a colored ellipse in the World View.
e1 = ellipse(pos=vec(0, 0), size=vec(10,5), color="green", opacity=.6)
e2 = ellipse(pos=vec(10, 0), size=vec(5,10), color="blue", opacity=.6)
e3 = ellipse(pos=vec(0, -10), size=vec(5,5), color="red", opacity=.6)
Three ellipses with different sizes
Below is the constructor for the ellipse class that shows its default values:
ellipse( pos=vec(0,0), size=vec(10,5), color=default_color, border_size=0, border_color=default_color, border_style="none" image="", opacity=1, visible=True, motion_map=False, label_text="" label_color=default_color )
  • pos — The initial position of your ellipse as a vector with x,y,z coordinates.
  • size — The size of the ellipse given as a vector with x,y,z coordinates.
  • color — The ellipse will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • border_size — The border thickness in pixels.
  • border_color — The border will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • border_style — Sets the border as either solid (default = "none") or "dash" for a dashed border, or "dot" for a dotted border.
  • image — A URL that identifies a JPEG, GIF, SVG or PNG image.
  • opacity — The ellipse will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
  • visible — The ellipse 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_text - The text of an attached label.
  • label_color - The color of the attached label text.
These attributes may also be modified on the ellipse after it is created, just like the circle.

ellipse.rotate()

ellipse objects can be rotated, just like the circle.
ellipse.rotate(angle=0, axis=vec(0, 0)) — Rotates the ellipse object by a given angle value in radian units. You can also provide an optional vector that identifies the axis of rotation. This method should only be called from the Calculations code editor.

ellipse.image

ellipse objects can also be represented with an image by setting the image attribute of the object. The text must be a URI link to a graphic file that can be a PNG, SVG, GIF, or JPEG image. See the circle above for more detail

rectangle

A rectangle is very similar to a circle but it is represented as a colored rectangle in the World View. Just as with the circle, Tychos only uses the width and height attributes for display. You can define how these attributes change given the rules of the simulation that you define.
Could not load image
Below is the constructor for the Rectangle class that shows its default values:
rectangle( pos=vec(0,0), size=vec(10,5), color=default_color, border_size=0, border_color=default_color, border_style="none" image="", opacity=1, visible=True, motion_map=False, label_text="" label_color=default_color )
  • pos — The initial position of your rectangle as a vector with x,y,z coordinates.
  • size — The size of the rectangle given as a vector with x,y,z coordinates.
  • color — The rectangle will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • border_size — The border thickness in pixels.
  • border_color — The border will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • border_style — Sets the border as either solid (default = "none") or "dash" for a dashed border, or "dot" for a dotted border.
  • 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_text - The text of an attached label.
  • label_color - The color of the attached label text.
These attributes may also be modified on the rectangle object after it is created. In particular, one will usually change the pos attribute in the Calculations editor to show movement. e.g.
# Initial State editor
r1 = rectangle(pos=vec(0, 0), size=vec(10, 10), color="green")
r2 = rectangle(pos=vec(20, 0), size=vec(10, 20), color="blue")
r3 = rectangle(pos=vec(40, 0), size=vec(20, 10), color="orange")
# Calculations editor
r1.pos = r1.pos + vec(1, 0.25)

rectangle.rotate

You can also rotate a rectangle object in order to simulate rotational behavior.
rectangle.rotate(angle=0, axis=vec(0, 0)) — Rotates the rectangle object by a given angle value in radian units. You can also provide an optional matrix that identifies the center of rotation. This method should only be called from the Calculations code editor.
Three different Rectangle objects rotated at different angles
Example:
# Calculations editor
r1.rotate(-pi/4)
r2.rotate(radians(90))
r3.rotate(radians(45))
rectangle.image
Just as with circle objects, rectangle objects can also be represented with an image by setting the image attribute of the object.
r.image = "https://some.image.url.jpg"

Arrow

The arrow class represents a graphical arrow and is commonly used to illustrate vectors, but can be used for representing anything in your simulations.
Below is the constructor for the arrow class that shows its default values:
arrow( pos=vec(0,0), size=vec(0,1), color=default_color, stroke=0, style="none" opacity=1, show_components=False, visible=True, motion_map=False, )
  • pos — The initial position of your arrow tail as a vector with x,y,z coordinates.
  • size — The size of the arrow given as a vector with x,y,z coordinates.
  • color — The arrow will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".
  • stroke — The arrow thickness in pixels.
  • style — An arrow can have a "dash" style, or a "dot" style, or "none" which is the default.
  • opacity — The arrow will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
  • show_components — This flag tells Tychos to attach additional x and y component arrows.
  • visible — The arrow 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.
Could not load image
Arrow without components
Could not load image
Arrow with components
Example — The illustrations above were drawn using these commands:
# Initial State editor
c = circle(pos=vec(0,0), color="green")
a = arrow(pos=c.pos, size=vec(20, 20), color="purple")
a.show_components = True

Line

The line class draws a line and is commonly used to illustrate some connecting member like a string or cable, but could really represent anything you like.
Below is the constructor for the line class that shows its default values:
line( pos=vec(0,0), pos2=vec(1,0), color=default_color, stroke=1, style="none", opacity=1, visible=True, motion_map=False )
  • pos — coordinates for the starting point of the line as vector
  • pos2 — coordinates of the termination point of the line as a vector.
  • color — HTML color value for your line, e.g. "red" or "#ff0000".
  • stroke — Stroke value that determines the visual thickness of the line. This is measured in pixels.
  • style — Sets the line as either solid (default = "none") or "dash" for a dashed line, or "dot" for a dotted line.
  • opacity — The line will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
  • visible — The line 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.
Example:
myLine = line(pos=vec(0, 0), pos2=vec(20, 20), color="purple", stroke=2)
anotherLine = line(pos=vec(0, 0), pos2=vec(10, 20), color="green", stroke=10)

poly_line

The poly_line class draws a series of connected lines between a given set of points. This object can be used to represent a complex path or a shape other than a circle or rectangle.
Below is the constructor for the poly_line class that shows its default values:
poly_line( pos=vec(0,0), color=default_color, stroke=1, style="none" fill=False opacity=1, visible=True, motion_map=False } )
  • pos — coordinates for the starting point of the poly_line as a vector.
  • color — HTML color value for your poly_line, e.g. "red" or "#ff0000".
  • stroke — Stroke value that determines the visual thickness of the poly_line. This is measured in pixels.
  • style — Can be "none" for solid segments, "dash" for dashed line segments or "dot" for dotted line segments.
  • fill — Boolean value (True or False) for displaying the poly_line object as a filled in solid.
  • opacity — The poly_line will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
  • visible — The poly_line 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.
poly_line objects have a number of methods for manipulating the poly_line points, like adding new points, changing existing points, removing points, etc:
  • set_points: (points) -> Set the points for the poly_line given a List of points.
  • translate: (delta_position) -> Move all the points according to a vector.
  • rotate: (angle, axis) -> Transform the points a certain angle measurement about an axis. This axis is relative to the first point in the poly_line.
  • npoints(): -> Returns the number of points in the poly_line.
  • append: (*points) -> Add a point (or many points) to the end of the poly_line.
  • unshift: (*points) -> Add a point (or many points) at the beginning of the poly_line.
  • shift: () -> Remove the first point in the poly_line object.
  • splice: (start, how_many, *points) -> Insert a point or several points anywhere at the specific index position. You can also identify how many points you would like to remove from the poly_line as well.
  • pop: (n) -> Get the vector for point number n and remove it. Can use negative values, where -1 is the last point, -2 the next to the last, etc.
  • clear: () -> Remove all the points in the poly_line.
  • modify: (n, point) -> Modify the point at the specified n position, replacing it with the given vector.
  • slice: (start, end) -> Returns the set of points (but does not remove them) from the poly_line object beginning at the start value and ending at the end index position.
Three poly_line objects showing different styles and fills.

spring

A spring is a visual representation of a common elastic connector that displays a given number of coils that dynamically change shape once the dimensions of the spring are changed in the Calculations Pane.
Below is the constructor for the spring class that shows its default values:
spring( pos=vec(0,0), pos2=vec(1,0), color=default_color, coils=5, width=10, stroke=1, opacity=1, visible=True, motion_map=False )
  • pos — coordinates for the starting point of the spring as a 2D vector.
  • pos2 — coordinates of the termination point of the spring as 2D vector
  • color — HTML color value for your spring, e.g. "red" or "#ff0000".
  • coils — The number "coil" zig-zags.
  • width — The width of the "coil" zig-zags.
  • stroke — Stroke value that determines the visual thickness of the spring. This is measured in pixels.
  • opacity — The spring will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
  • visible — The spring 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.
Three different Spring objects
The code below shows the three different spring objects above that have different lengths, widths and coil numbers. The circle objects are shown just for reference.
# Initial State editor
c1 = circle(pos=vec(0, 0), radius=2, color="green")
spring1 = spring(pos=vec(0, 20), pos2=c1.pos, color="black", coils=20, width=2)
c2 = circle(pos=vec(10, 0), radius=2, color="green")
spring2 = spring(pos=vec(10, 20), pos2=c2.pos, color="black", coils=10, width=4)
c3 = circle(pos=vec(20, 0), radius=2, color="green")
spring3 = spring(pos=vec(20, 20), pos2=c3.pos, color="black", coils=20, width=2)
These attributes may also be modified after the spring is created.

Label

You can add text labels to any scenario using the label class.
Below is the constructor for the label class that shows its default values:
label( pos=vec(0,0), size=vec(10,10), text="", color=default_color, opacity=1, visible=True, motion_map=False )
  • pos — coordinates for the center point of the label as a 2D vector.
  • size — The width and height of the label as a 2D vector.
  • text — The text of the label as a string.
  • color — HTML color value for your label, e.g. "red" or "#ff0000".
  • opacity — The label will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.
  • visible — The label 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.
These attributes may also be modified after the label object is created. Here is an example of how to make several label objects:
# Initial State editor
label1 = label(pos=vec(0, 100), size=vec(50, 50), text="Cat", color="green")
label2 = label(pos=vec(0, 0), size=vec(150, 150), text="Dog", color="red")
label3 = label(pos=vec(0, -100), size=vec(50, 50), text="Mouse", color="blue")

label.rotate

You can rotate a label as shown above:
label3.rotate(pi/4)

Interface Widgets

Tychos also provides several "widgets" for displaying data in different ways as well as adding user interactivity so that your simulations can respond to input.

graph

A graph is a 2-dimensional chart of data that you specify in the Initial State editor. Each graph that is created will appear on the right side of the World View. Your program needs to add points to the graph with the plotcommand.
Here is the constructor for creating a graph object:
graph(title="Graph", y_axis="Y", x_axis="X") -> Returns a Graph
  • title = Optional text that will appear at the top of the graph.
  • y_axis = Optional text that will appear on the left side of the graph as the vertical axis label.
  • x_axis = Optional text that will appear at the bottom of the graph as the horizontal axis label.
g_x_t = graph(title="X Position vs Time", y_axis="X Position", x_axis="Time")
g_vx_t = graph(title="$v_x$ vs Time", y_axis="Velocity", x_axis="Time")
g_ax_t = graph(title="$a_x$ vs Time", y_axis="Acceleration", x_axis="Time"})
Examples of three graphs
Graph.plot
graph.plot(x, y, color=default_color) — Adds a data point to your graph.
# Calculations editor
# Graphing a particle projectile's position
g_pos.plot(t, particle.pos[X], "blue")
g_pos.plot(t, particle.pos[Y], "red")

bar_chart

A bar_chart is a chart of data that is displayed as a set of bars representing an array of data values. Each bar_chart that is created will appear on the right side of the World View.
  • title = Optional text that will appear at the top of the BarChart.
  • min = Optional minimum value whose default is 0. If any bar data value is less than this minimum, the bar will be truncated.
  • max = Optional maximum value whose default is 1. This sets the maximum viewable bar height. If any bar data value is greater than this maximum, the bar will be truncated.
bc_energy = bar_chart(title="Energy Bar Chart", min=0, max=500)

bar_chart.data

bar_chart.data — Sets the data values for the bars in the bar chart.
# Set the data for the bars
bc_energy.datat =[x,y,z,w] # Where x, y, z, and w are numerical data values

bar_chart.labels

bar_chart.labels — Defines a set of labels - one label corresponding to each data bar in the chart. If the number of labels does not match the number of bars set by the number of data values, then a numbered label will be assigned to the extra data value.
# Set the labels for the bars
bc_energy.labels = ["KE", "Ue", "Ug", "TE"]

bar_chart.colors

bar_chart.colors — List of colors, one color corresponding to each data bar in the chart. If the number of colors does not match the number of bars set by the number of data values, then any extra bars will be given the default HTML color of #aaa.
# Set the colors for the bars
bc_energy.colors = ["red", "blue", "green", "black"]

meter

A meter is a numeric display of data. Each meter that is created will appear on the left side of the World View. Your program needs to tell the meter what value it needs to display by using the display command.
Below is the constructor for the Meter widget and default values:
meter(title="Meter", color="black", suffix="")
  • title = Optional text that will appear at the top of the Gauge widget.
  • color — HTML color value for your meter, e.g. "red" or "#ff0000".
  • suffix — text added at the end of the data value display.

meter.display()

meter.display(value) — Displays the value on the meter.
  • value = Numeric value to be displayed.
Example:
# Initial State editor
mt = meter(title="Time", color="red", suffix="s")
# Calculations editor
mt.display(t)

gauge

A gauge is an analog display of data that is very similar to a meter that you specify in the Initial Sate pane. Each gauge that is created will appear on the left side of the World View. Gauges also need to to be set up with a specific minimum and maximum value for identifying the range of the gauge. Your scenario needs to tell the gauge what value it needs to display by using the display command.
Below is the constructor for the Gauge widget and default values:
gauge(title="Gauge", min=0, max=100, color="black")
  • title = Optional text that will appear at the top of the gauge widget.
  • min = The minimum value of the gauge
  • max = The maximum value of the gauge
  • color — HTML color value for your gauge, e.g. "red" or "#ff0000".

gauge.display()

gauge.display(value) — Displays the value in the Gauge.
Example:
# Initial State editor
g1 = gauge(title="Value 1", min=0, max=200, color="orange")
g2 = gauge(title="Value 2", min=-100, max=100, color="purple")
g3 = gauge(title="Value 3", min=0, max=100, color="blue")
# Calculations editor
val = 44
g1.display(val)
g2.display(val)
g3.display(val)

toggle

A toggle is an interactive widget that allows you to associate a boolean value (True or False) with the state of the toggle widget.
A toggle that is False
A toggle that is True
Below is the constructor for the toggle widget and default values:
toggle(title="Toggle")
  • title = Optional text that will appear at the top of the toggle widget.

Toggle.value

x = toggle.value — Returns the current value of the toggle. This is read/write.
Example:
# Initial State editor
t1 = toggle(title="Activate")
# Calculations editor
is_active = t1.value

slider

A slider is an interactive widget that allows you to link a value in your scenario to the current value of a horizontal slider.
A slider widget with a value of 0.
Below is the constructor for the Slider widget and default values:
slider(title="Slider", min=0, max=100, step=1})
  • title = Optional text that will appear at the top of the slider widget.
  • min = The minimum value of the slider
  • max = The maximum value of the slider
  • step = The step increment of the slider

slider.value

x = slider.value — Returns the current value of the Slider. This is read/write.
Example:
# Initial State editor
s1 = slider(title="I'm A Slider", min=0, max=100, step=2)