Tychos Language Reference

The following reference guide identifies the language syntax, built in variables, functions, and classes that are used to code your simulations in Tychos. Tychos uses the MathNotepad language. We have included documentation here for some of the helpful functions defined in the MathNotepad language. This is not a complete list of all functions available in MathNotepad, 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:

1

# This is a comment.

Copied!

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 declarations1

# Assigns a variable called x the value of 10

2

x = 10

3

# Assign a new variable y the value of the x

4

y = x

5

# Assign x the value of itself plus 1

6

x = x + 1

Copied!

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...`X`

,`Y`

,`Z`

— These are shortcuts for indexing the first two elements of 3-D matrices, e.g.`my_particle.pos[X]`

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:

The

`pow(base, power)`

function takes two arguments, raising the `base`

by the `power`

.1

# returns number 8

2

pow(2,3)

Copied!

The

`sqrt(positive_number)`

function takes a single non negative number and returns the real square root of the number.1

# returns number 2

2

sqrt(4)

Copied!

The

`abs(number)`

function returns the absolute value of a number.1

# returns number 2

2

abs(-2)

Copied!

Trigonometric Functions

The following functions all use radians as the angle measurement. You can use

`pi`

to represent PI.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.1

# returns number 1

2

sin(PI/2)

Copied!

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.1

# returns number 1

2

cos(0)

Copied!

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.1

# returns number 1

2

tan(PI/4)

Copied!

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.1

# returns number 1.57

2

asin(1)

Copied!

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.1

# returns number 0

2

acos(1)

Copied!

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.1

# returns number -0.785

2

atan2(-1, 1)

3

# returns 2.36

4

atan2(1, -1)

Copied!

See below.

The

`deg_to_rad(angle)`

function is not part of the MathNotepad language but is provided as a helper function to make the conversion from degree angles to radians easier. The input is an angle measurement in degrees and the output is the angle measurement in radians.1

# returns number .785

2

deg_to_rad(45)

Copied!

See below.

The

`degrees(angle)`

function is not part of the MathNotepad language but is provided as a helper function to make the conversion from radian angles to degrees easier. The input is an angle measurement in radians and the output is the angle measurement in degrees.1

# returns number 180

2

degrees(PI)

Copied!

Matrix Functions

The following functions provide operations for matrix calculations.

Calculates the dot product of two vectors. The dot product of

`x = [a1, a2, a3, ..., an]`

and `y = [b1, b2, b3, ..., bn]`

is defined as:`dot(x, y) = a1 * b1 + a2 * b2 + a3 * b3 + … + an * bn`

1

# Work is the dot product of Force (F) and displacement (r)

2

F = [2, 2]

3

r = [3, 3]

4

# returns 12

5

Work = dot(F, r)

Copied!

Calculates the cross product for two vectors in three dimensional space. The cross product of

`x = [a1, a2, a3]`

and `y = [b1, b2, b3]`

is defined as:`cross(x, y) = [ a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1 ]`

If one of the input vectors has a dimension greater than 1, the output vector will be a 1x3 (2-dimensional) matrix.

1

# Torque is the cross product of Force and moment arm (r)

2

F = [2, 0, 0]

3

r = [0, 2, 0]

4

# returns matrix [0, 0, 4]

5

cross(F, r)

Copied!

Other Useful Functions

Some other useful functions...

Return a random number larger or equal to min and smaller than max using a uniform distribution. If now min or max are given, then it returns a random value from 0 to 1. If just one value is given, then it returns a random number between 0 and the input value.

1

# returns a random number between 0 and 1

2

random()

3

# returns a random number between 0 and 100

4

random(100)

5

# returns a random number between 30 and 40

6

random(30, 40)

7

# returns a 2x3 matrix with random numbers between 0 and 1

8

random([2, 3])

Copied!

string(object)

Create a string or convert any object into a string. Elements of Arrays and Matrices are processed element wise.

format(value, precision)

Formats a value into a string. You have several options for how this value will be formatted:

1

# returns '0.333'

2

format(1/3, 3)

3

# returns '21000'

4

format(21385, 2)

5

# returns '1200000000'

6

format(12e8, {notation: 'fixed'})

7

# returns '2.3000'

8

format(2.3, {notation: 'fixed', precision: 4})

Copied!

Concatenate two or more matrices. This function can also be used to concatenate strings together.

`dim: number`

is a zero-based dimension over which to concatenate the matrices. By default the last dimension of the matrices.1

A = [1, 2]

2

B = [3, 4]

3

concat(A, B)

4

# returns [1, 2, 3, 4]

5

math.concat(A, B, 0)

6

# returns [[1, 2], [3, 4]]

7

math.concat('hello', ' ', 'world')

8

# returns 'hello world'

Copied!

This function has been deprecated and you should instead use the class

**Arrow**

to represent arrows in your simulations. See below for more details.The

`drawArrow`

function draws an arrow and is commonly used to illustrate vectors for a particle. drawArrow should be called in the Calculations editor because it only draws the arrow for the current frame. If you call drawArrow() in the Initial State editor, you will not see anything.`drawArrow(pos=[0,0], size=[1,0], color="black", components=false, thickness=1)`

-> returns and draws an Arrow object`pos`

— coordinates for the starting point of the arrow as an [X,Y] matrix.`size`

— the vector to illustrate, e.g. [10, 0] will draw an arrow 10 units to the right.`color`

— Optional HTML color value for your arrow, e.g. "red" or "#ff0000".`components`

— Optional flag that determines if vector components are drawn, a value of`true`

displays the components.`thickness`

— Optional stroke value that determines the visual thickness of the arrow.

This function has been deprecated and you should instead use the class to represent lines in your simulations. See below for more details.

`Line`

The

`drawLine`

function draws a line and is commonly used to illustrate some connecting member like a string or cable, but could really represent anything you like. `drawLine`

should be called in the Calculations editor because it only draws the line for the current frame. If you call `drawLine`

in the Initial State editor, you will not see anything.`drawLine(pos=[0,0], pos2=[10,0], color="black", thickess=1)`

-> returns and draws an Line object`pos`

— coordinates for the starting point of the line as an [X,Y] matrix.`pos2`

— the coordinates of the end point of the line as an [X,Y] matrix.`color`

— Optional HTML color value for the line, e.g. "red" or "#ff0000".`thickness`

— Optional stroke value that determines the visual thickness of the line.

Example — The illustration above was drawn using this command:

1

# Calculations editor

2

drawLine([0, 0], [20, 20], "purple", 2) # a line

3

drawLine([0, 0], [10, 20], "green", 10) # another line

Copied!

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.

`unit_vector(vec)`

-> returns a vector of length 1, and in same direction as `vec`

.`vec`

- any two dimensional vector as a [X, Y] matrix.

Example:

1

u = unit_vector([3, 4]) # returns [0.6, 0.8]

2

magnitude(u) # returns 1

Copied!

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 its direction.

`magnitude(vec)`

-> returns the scaler magnitude of the vector `vec`

.`vec`

- any two dimensional vector as a [X, Y] matrix.

Example:

1

magnitude([3, 4]) # returns 5

Copied!

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 default return angle is given in radians, but can also be expressed in degrees.

`direction(vec, units="rad")`

-> returns the scaler angle measurement of the vector `vec`

heading in radian form or in degree form.`vec`

- any two dimensional vector as a [X, Y] matrix.`units`

- optional`deg`

for degree measurement or the default of`rad`

for radians.

Example:

1

direction([4, 4]) # returns .785

2

direction([4, 4], "deg") # returns 45

Copied!

polar

This function returns a two-dimensional matrix representing the cartesian components of a polar coordinate corresponding to the magnitude of the radius and the radial angle.

`polar(radius, angle, units="rad")`

-> returns a two dimensional vector as a [X, Y] matrix.`radius`

- scalar quantity representing the scalar distance of the radius of the`angle`

- scalar quantity representing the angle measurement of the polar coordinate.`units`

- optional`deg`

for degree measurement or the default of`rad`

for radians.

Example:

1

polar(10, 45, "deg") # returns [7.07, 7.07]

2

polar(10, PI/4) # returns [7.07, 7.07]

Copied!

createPoints

This function creates an array of points based on a domain, a domain increment, and a function expression.

`createPoints(min, max, step, expression)`

-> returns an array of points whose domain is defined from the `min`

value to the `max`

value with an increment defined by `step`

and whose range is evaluated based on the function `expression`

.`min`

- the*inclusive*minimum value of the domain.`max`

- the*exclusive*maximum value of the domain.`step`

- the increment step value of the domain.`expression`

- a string representing a functional expression that can be evaluated in terms of "x".

Example:

1

points = createPoints(0, 4, 1, "x^2")

2

# [[0, 0], [1, 1], [2, 4], [3, 9]]

Copied!

stop

This function actually evaluates a boolean test and then stops the simulation once the boolean test succeeds. This can be useful if you want the simulation to stop when some condition has been met within your simulation.

`stop(test)`

-> returns a either false or true. If true is returned, the simulation stops.`test`

- a boolean statement that can be evaluated to`true`

or`false`

.

Example:

1

stop(t > 10) # simulation stops at 10 seconds

2

stop(buggy.pos[X] == 100) # simulation stops when X position of particle equals 100

Copied!

Collision Functions

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.

hasCollided

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.*

`hasCollided(source, target)`

-> returns a boolean `true`

or `false`

.`source`

-`Circle`

or`Rectangle`

object`target`

-`Circle`

or`Rectangle`

object

Example:

1

# Initial State

2

p1 = Circle({pos:[15, 0], radius:10, color:rgba(0, 200, 200, .6)})

3

b1 = Rectangle({pos:[0, 0], size:[15, 15], color:rgba(200, 0, 200, .6)})

4

b1.rotate(radians(45))

5

p3 = Circle({pos:[-15, 0], radius:10, color:rgba(0, 0, 200, .6)})

Copied!

1

# Calculations

2

hasCollided(p1, b1) # returns true

3

hasCollided(b1, p3) # returns true

4

hasCollided(p1, p3) # returns false

Copied!

getIntersect

This function returns a two dimensional matrix 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.

`getIntersect(source, target)`

-> returns a two dimensional matrix.`source`

-`Circle`

or`Rectangle`

object`target`

-`Circle`

or`Rectangle`

object

Example:

1

# Initial State

2

p1 = Circle({pos:[0, 0], radius:10, color:rgba(200, 200, 200, .6)})

3

p2 = Circle({pos:[12, 10], radius:10, color:rgba(0, 200, 0, .6)})

4

b1 = Rectangle({pos:[-12, 0], size:[15, 15], color:rgba(200, 0, 0, .6)})

5

mtv1 = Arrow({pos:[p1.pos], color:"green"})

6

mtv2 = Arrow({pos:[p1.pos], color:"red"})

Copied!

1

# Calculations

2

mtv1.size = getIntersect(p1, p2)

3

mtv2.size = getIntersect(p1, b1)

Copied!

Comparison Functions

The following functions are used to compare two values as being equal or unequal as well as testing if one value is larger or smaller than another. These are very helpful when writing goals for students.

`equal(a, b)`

`a == b`

The function tests if two values (x and y) are equal. It returns a boolean value of

`true`

or `false`

.1

2 + 2 == 3 # returns false

2

2 + 2 == 4 # returns true

3

t == 10 # returns true if t is 10, or false if it is not.

4

equal(2 + 2, 4) # same as 2 + 2 == 4

Copied!

This function is similar to

`equal`

, but it tests element wise whether two matrices are equal. It returns a boolean value of `true`

or `false`

. The code below demonstrates the difference between `equal`

and `deepEqual`

:1

p1 = Particle([10, 10])

2

p2 = Particle([10, 0])

3

deepEqual(p1.pos, p2.pos) # returns false

4

equal(p1.pos, p2.pos) # returns [true, false]

Copied!

`larger(a, b)`

or `a > b`

The function tests if one value (a) is larger than another (b). It returns a boolean value of

`true`

or `false`

.1

2 > 3 # returns false

2

3 > 2 # returns true

3

2 > 2 # returns false

4

larger(2, 2) # same as 2 > 2

Copied!

`smaller(a, b)`

or `a < b`

The function tests if one value (a) is smaller than another (b). It returns a boolean value of

`true`

or `false`

.1

2 < 3 # returns true

2

3 < 2 # returns false

3

2 < 2 # returns false

4

smaller(2, 2) # returns false

Copied!

`unequal(a, b)`

or `a != b`

The function tests if two values (a and b) are unequal. It returns a boolean value of

`true`

or `false`

.1

2 + 2 != 3 # returns true

2

unequal(2 + 2, 3) # true -- same as 2 + 2 != 3

3

2 + 2 != 4 # returns false

4

t != 10 # returns false if t is 10, or true if it is not.

Copied!

Comparison operators return

`true`

or `false`

but these also evaluate to 1 (true) or 0 (false). This can allow you to conditionally assign a value to a variable depending on the evaluation of the comparison. See the code below as an example:1

# If t is larger than 10, then the value of F is [10, 10], otherwise it is 0.

2

F = (t > 10) * [10, 10]

Copied!

`if(test, true_result, false_result)`

The

`if()`

function returns `true_result`

or `false_result`

depending on `test`

.1

if(true, 3, 44) # returns 3

2

if(false, 3, 44) # returns 44

3

if(1 > 2, 3, 44) # test is false; therefore returns 44

4

a = 1

5

b = 1

6

if(a == b, "YAY", "darn") # test is true; therefore returns "YAY"

Copied!

Logical Operators

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

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.

1

A = true

2

B = true

3

A and B # returns true

4

B = false

5

A and B # returns false

Copied!

You can use the

`and`

operator to test if two comparisons are both true:1

x = 2

2

(x < 3) and (x == 2) # returns true

3

(x < 3) and (x != 2) # returns false

Copied!

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.

1

A = true

2

B = false

3

A or B # returns true

4

A = false

5

A or B # returns false

Copied!

You can use the

`or`

operator to test if one of two comparisons are true:1

x = 2

2

smaller(x, 3) or equal(x, 3) # one is true, so returns true

3

(x < 1) or (x == 3) # both are false, so returns false

Copied!

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

`Cirlcle`

, the `Rectangle`

, the `Arrow`

, the `Line`

, the `Label`

, and the `Spring`

The tools that can be used for analyzing the behavior of your simulations are the `Graph`

, the `Gauge`

and the `Meter`

. There are also user input objects that can be added to your simulations to make them more interactive: the `Toggle`

, the `Slider`

, the `Input`

, and the `Menu`

controls.Circle

A

`Circle`

is drawn as a colored circle in the World View. A `Circle`

has a position, a radius, a color, an opacity, a flag for setting its visibility state, and a flag for determining if a motion map should be attached.Below is the constructor for the

`Circle`

class that shows its default values:```
Circle(
{pos:[0,0],
radius:10,
color:default_color,
image: "",
opacity: 1,
visible: true,
motion_map: false,
label: {text: "", color: default_color}
}
)
```

`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.

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.1

# Initial State editor

2

c = Circle()

3

c_big = Circle({pos:[50, 0], radisu:25})

4

c_big.color = "red"

5

c_green = Circle({pos:[100, 0], color: "green", opacity: .5})

Copied!

1

# Calculations editor

2

c.pos = c.pos + [1, 0.25]

Copied!

Circle.rotate()

`Circle`

objects can be rotated.`Circle.rotate(angle=0, axis=[0, 0])`

— Rotates the `Circle`

object by a given angle value in radian units. You can also provide an optional matrix that identifies the axis of rotation. This method should only be called from the 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.1

rocket = Circle({pos:[0, 0], radius:10})

2

rocket.image = "https://upload.wikimedia.org/wikipedia/commons/3/3d/Spacecraft.png"

Copied!

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))`

Circle.label

`Circle`

objects can also be given a text label. This is similar to the `Label`

object.`c.label = {text:"Hello", color:"green"}`

— This adds a text label to the `Circle`

object that scales to fit inside the circle.Rectangle

A

`Rectangle`

is very similar to a `Circle`

but it is represented as a colored rectangle in the World View. A `Rectangle`

has position, width, height, color, opacity, visibility, a motion map flag, as well as a label. 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.Below is the constructor for the

`Rectangle`

class that shows its default values:```
Rectangle(
{pos:[0,0],
size:[10,10],
color:default_color,
image: "",
opacity: 1,
visible: true,
motion_map: false,
label: {text: "", color: default_color}
}
)
```

`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.

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.1

# Initial State editor

2

r1 = Rectangle({pos:[0, 0], size:[10, 10], color:"green"})

3

r2 = Rectangle({pos:[20, 0], size:[10, 20], color:"blue"})

4

r3 = Rectangle({pos:[40, 0], size:[20, 10], color:"orange"})

Copied!

1

# Calculations editor

2

r1.pos = r1.pos + [1, 0.25]

Copied!

Rectangle.rotate

You can also rotate a

`Rectangle`

object in order to simulate rotational behavior.`Rectangle.rotate(angle=0, axis=[0, 0])`

— Rotates the `Rectangle`

object by a given angle value in Three different Rectangle objects rotated at different angles

Example:

1

# Calculations editor

2

r1.rotate(-PI/4)

3

r2.rotate(radians(90))

4

r3.rotate(radians(45))

Copied!

Just as with

`Circle`

objects, `Rectangle`

objects can also be represented with an image by setting the image attribute of the object.1

r.image = "https://some.image.url.jpg"

Copied!

Rectangle.label

`Rectangle`

objects can also be given a text label. This is similar to the `Label`

object.1

r.label = {text:"Hello", color:"green"}

Copied!

This adds a text label to the

`Rectangle`

object.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:[0,0],
size:[1,0],
color:default_color,
componets: false,
stroke: 1,
opacity: 1,
visible: true,
motion_map: false
}
)
```

`pos`

— coordinates for the starting point of the`Arrow`

as an`[X,Y]`

matrix.`size`

— the vector to illustrate, e.g.`[10, 0]`

will draw an`Arrow`

10 units to the right.`color`

— HTML color value for your`Arrow`

, e.g. "red" or "#ff0000".`components`

— A flag that determines if X and Y components are drawn, a value of`true`

displays the components.`stroke`

— Stroke value that determines the visual thickness of the`Arrow`

.`opacity`

— The`Arrow`

will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.`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.

Arrow without components

Arrow with components

Example — The illustrations above were drawn using these commands:

1

# Initial State editor

2

c = Circle({pos:[0,0], color:"green"})

3

a = Arrow({pos: c.pos, size: [20, 20], color:"purple"})

4

a.components = true

Copied!

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:[0,0],
pos2:[1,0],
color:default_color,
stroke: 1,
opacity: 1,
visible: true,
motion_map: false
}
)
```

`pos`

— coordinates for the starting point of the`Line`

as an`[X,Y]`

matrix.`pos2`

— coordinates of the termination point of the`Line`

as an`[X,Y]`

matrix`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.`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:

1

myLine = Line({pos:[0, 0], pos2: [20, 20], color: "purple", stroke:2})

2

anotherLine = Line({pos:[0, 0], pos2: [10, 20], color: "green", stroke:10})

Copied!

PolyLine

The

`PolyLine`

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

`PolyLine`

class that shows its default values:```
PolyLine(
{points:[],
color:default_color,
stroke: 1,
style: "none"
fill: false
opacity: 1,
visible: true,
motion_map: false
}
)
```

`points`

— an array of points given as`[X,Y]`

matrices.`color`

— HTML color value for your`PolyLine`

, e.g. "red" or "#ff0000".`stroke`

— Stroke value that determines the visual thickness of the`PolyLine`

. This is measured in pixels.`style`

— Can be`"none"`

for a solid segments,`"dash"`

for dashed line segments or`"dot"`

for dotted line segments.`fill`

— Boolean value (true or false) for displaying the PolyLine object as a filled in solid.`opacity`

— The`PolyLine`

will be drawn with an opacity between 1 and 0, representing 100% opaque to 100% transparent.`visible`

— The`PolyLine`

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.

PolyLine objects have a number of methods for manipulating the PolyLine points, like adding new points, changing existing points, removing points, etc:

`setPoints: (points)`

-> Set the points for the PolyLine given an array of points.`translate: (deltaPosition)`

-> Move all the points according to a matrix`[X, Y]`

.`rotate: (angle, axis)`

-> Transform the points a certain angle measurement about an axis. This axis is relative to the first point in the PolyLine.`npoints():`

-> Returns the number of points in the PolyLine.`append: (point)`

-> Add a point to the end of the PolyLine.`remove: (n)`

-> Remove a point at the given index. The first point is at index = 0.`unshift: (point)`

-> Add a point at the beginning of the PolyLine.`shift: ()`

-> Remove the first point in the PolyLine object.`splice: (point, n)`

-> Add a point into the PolyLine at the specific index position. The first point is at index = 0.`drop: (n)`

-> Remove all points from the start to the "n" index position from the PolyLine.`dropRight: (n)`

-> Remove all points from the end of the PolyLine to the "n" index position.`last: ()`

-> Returns the last point in the PolyLine.`first: ()`

-> Returns the first point in the PolyLine.`replace: (point, n)`

-> Replace the point at "n" index position with a new point.`clear: ()`

-> Remove all the points in the PolyLine.`point: (n)`

-> Returns the point at that "n" index position.`slice: (start, end)`

-> Returns the set of points (but does not remove them) from the PolyLine object beginning at the`start`

value and ending at the`end`

index position.

Three PolyLine objects showing different styles and fills.

Example:

1

star1 = PolyLine({stroke: 3, style:"dash", fill: true, opacity: .2, color: "blue"})

2

star1.setPoints([[0,75-200], [379-350,161-200],[469-350,161-200],[397-350,215-200], [423-350,301-200], [350-350,250-200], [277-350,301-200], [303-350,215-200], [231-350,161-200], [321-350,161-200], [0,75-200]])

3

star2 = PolyLine({stroke: 2, style:"none", color: "green"})

4

star2.setPoints([[0,75-200], [379-350,161-200],[469-350,161-200],[397-350,215-200], [423-350,301-200], [350-350,250-200], [277-350,301-200], [303-350,215-200], [231-350,161-200], [321-350,161-200], [0,75-200]])

5

star2.translate([100, 100])

6

star3 = PolyLine({stroke: 3, style:"dot", color: "red"})

7

star3.setPoints([[0,75-200], [379-350,161-200],[469-350,161-200],[397-350,215-200], [423-350,301-200], [350-350,250-200], [277-350,301-200], [303-350,215-200], [231-350,161-200], [321-350,161-200], [0,75-200]])

8

star3.translate([-100, 100])

Copied!

Spring

A **Calculations Pane**.

`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 Below is the constructor for the

`Spring`

class that shows its default values:```
Spring(
{pos:[0,0],
pos2:[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 an`[X,Y]`

matrix.`pos2`

— coordinates of the termination point of the`Spring`

as an`[X,Y]`

matrix`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.1

# Initial State editor

2

c1 = Circle({pos:[0, 0], radius:2, color:"green"})

3

spring1 = Spring({pos:[0, 20], pos2:c1.pos, color:"black", coils:20, width:2})

4

c2 = Circle({pos:[10, 0], radius:2, color:"green"})

5

spring2 = Spring({pos:[10, 20], pos2:c2.pos, color:"black", coils:10, width:4})

6

c3 = Circle({pos:[20, 0], radius:2, color:"green"})

7

spring3 = Spring({pos:[20, 20], pos2:c3.pos, color:"black", coils:20, width:2})

Copied!

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:[0,0],
size:[10,10],
text: "",
color:default_color,
opacity: 1,
visible: true,
motion_map: false
}
)
```

`pos`

— coordinates for the center point of the`Label`

as an`[X,Y]`

matrix.`size`

— The width and height of the`Label`

as an`[X,Y]`

matrix`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:1

# Initial State editor

2

label1 = Label({pos:[0, 100], size:[50, 50], text:"Cat", color:"green"})

3

label2 = Label({pos:[0, 0], size:[150, 150], text:"Dog", color:"red"})

4

label3 = Label({pos:[0, -100], size:[50, 50], text:"Mouse", color:"blue"})

Copied!

Label.rotate

Just as with a

`Block`

object or a `Particle`

object, you can rotate a label as shown above:1

label3.rotate(PI/4)

Copied!

This class has been deprecated. Past scenarios that used this class will still work, but we will no longer maintain this class as part of the Tychos language and we suggest that you switch to the

`Circle`

class.A

`Particle`

represents a spherical particle in the simulated world and is drawn as a colored circle in the World View. A particle has position, radius and color.`Particle(pos=[0,0], radius=10, color=default_color)`

-> returns a Particle`pos`

— The initial position of your particle in [X,Y] coordinates. If you don't specify a position, the default value of [0,0] is used.`radius`

— The radius of the circle that is drawn in the World View to represent this particle.`color`

— The particle will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".

These attributes may also be modified on the particle after it is created. In particular, one will usually change the

`pos`

attribute of a particle in the Calculations editor to show a particle's movement. E.g.1

# Initial State editor

2

p = Particle()

3

p_big = Particle([50, 0], 25)

4

p_big.color = "red"

5

p_green = Particle([100, 0], 10, "green")

6

7

# Calculations editor

8

p.pos = p.pos + [1, 0.25]

Copied!

Particle.rotate()

`Particle`

objects can be rotated, but it will have no noticeable affect on the object unless you are using an image as a representation of the `Particle`

.`particle.rotate(angle=0, center_of_rotation=[0, 0])`

— Rotates the `Particle`

object by a given angle value in degree units. You can also provide an optional matrix that identifies the center of rotation. This method should only be called from the Particle.addLabel()

`Particle`

objects can also be given a text label. This is similar to the Label object.`particle.addLabel(text="Hello", color="green")`

— This adds a text label to the `Particle`

object.This class has been deprecated. Past scenarios that used this class will still work, but we will no longer maintain this class as part of the Tychos language and we suggest that you switch to the

`Rectanlge`

class.Another particle representation in Tychos is the

`Block`

. A `Block`

is very similar to a `Partcle`

but it is represented as a colored rectangle in the World View. A block has position, width, height and color. Just as with Particle's, 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.`Block(pos=[0,0], size=[10, 10], color=default_color)`

-> returns a Block`pos`

— The inital position of your block in [X,Y] coordinates. If you don't specify a position, the default value of [0,0] is used.`size`

— The width and height of the block that is drawn in the World View to represent the block.`color`

— The block will be drawn in this color. Use HTML colors e.g. "#ff3300", "blue".

These attributes may also be modified on the block after it is created. In particular, one will usually change the

`pos`

attribute of a block in the Calculations editor to show a block's movement. E.g.1

# Initial State editor

2

b1 = Block([0, 0], [10, 10], "green")

3

b2 = Block([20, 0], [10, 20], "blue")

4

b3 = Block([40, 0], [20, 10], "orange")

5

6

# Calculations editor

7

b1.pos = b1.pos + [1, 0.25]

Copied!

Block.rotate

You can also rotate a

`Block`

object in order to simulate rotational behavior.`Block.rotate(angle=0, center_of_rotation=[0, 0])`

— Rotates the `Block`

object by a given angle value in degree units. You can also provide an optional matrix that identifies the center of rotation. This method should only be called from the Example:

1

# Initial State editor

2

b1 = Block([-20, 0], [20, 10], "green")

3

b2 = Block([0, 0], [20, 10], "blue")

4

b3 = Block([20, 0], [20, 10], "orange")

5

6

# Calculations editor

7

b1.rotate(rad_to_deg(-PI/4))

8

b2.rotate(90)

9

b3.rotate(45)

Copied!

Just as with

`Particle`

objects, `Block`

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.1

rocket = Block([0, 0], [10, 10], "purple")

2

rocket.image = "https://upload.wikimedia.org/wikipedia/commons/3/3d/Spacecraft.png"

Copied!

Block.addLabel()

`Block`

objects can also be given a text label. This is similar to the `Label`

object.`block.addLabel(text="Hello", color="green")`

— This adds a text label to the `Block`

object.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 Calculations 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 `plot`

command.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.

1

g_x_t = Graph({title:"X Position vs Time", y_axis:"X Position", x_axis:"Time"})

2

g_vx_t = Graph({title:"$v_x$ vs Time", y_axis:"Velocity", x_axis:"Time"})

3

g_ax_t = Graph({title:"$a_x$ vs Time", y_axis:"Acceleration", x_axis:"Time"})

Copied!

Examples of three graphs.

`graph.plot(x, y, color=default_color)`

— Adds a data point to your graph.1

# Calculations editor

2

# Graphing a particle projectile's position

3

g_pos.plot(t, particle.pos[X], "blue")

4

g_pos.plot(t, particle.pos[Y], "red")

Copied!

Graph.integrate

`graph.integrate(color=default_color)`

— When you call the `plot`

function of a graph, you create a new plot set. You can then integrate this plot set. This is done by referencing the plot set's color. This returns the calculated area based on the1

# Calculations editor

2

# Graphing a particle projectile's Y velocity

3

g_vel.plot(t, particle.v[Y], "green")

4

g_vel.integrate("green")

Copied!

Meter

A

`Meter`

is a numeric display of data that you specify in the Calculations editor. 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:default_color})`

`title`

= Optional text that will appear at the top of the`Gauge`

widget.`color`

— HTML color value for your`Gauge`

, e.g. "red" or "#ff0000".

`meter.display(value, units)`

— Displays the value on the Meter.`value`

= Numeric value to be displayed.`units`

= Optional string representing the unit label to be displayed.

Example:

1

# Initial State editor

2

mt = Meter({title:"Time", color:"red"})

Copied!

1

# Calculations editor

2

mt.display(t, "s")

Copied!

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.Three different gauges

Below is the constructor for the

`Gauge`

widget and default values:`Gauge({title:"Gauge", min:0, max:100, color:default_color})`

`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(value)`

— Displays the value in the Gauge.Example:

1

# Initial State editor

2

g1 = Gauge({title:"Value 1", min:0, max:200, color:"orange"})

3

g2 = Gauge({title:"Value 2", min:-100, max:100, color:"purple"})

4

g3 = Gauge({title:"Value 3", min:0, max:100, color:"blue"})

Copied!

1

# Calculations editor

2

val = 44

3

g1.display(val)

4

g2.display(val)

5

g3.display(val)

Copied!

Toggle

A

`Toggle`

is an interactive widget that allows you 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.

`x = toggle.value`

— Returns the current value of the Toggle. This is read/write.Example:

1

# Initial State editor

2

t1 = Toggle({title:"Activate"})

Copied!

1

# Calculations editor

2

isActive = t1.value

Copied!

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:"Input", 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`

`x = slider.value`

— Returns the current value of the Slider. This is read/write.Example:

1

# Initial State editor

2

s1 = Slider({title:"I'm A Slider", min:0, max:100, step:2})

Copied!

1

# Calculations editor

2

x = s1.value

Copied!

Input

An

`Input`

is an interactive widget that allows you to link a value in your scenario to the current value of a text box. The input values are limited to numerical inputs.An Input widget

Below is the constructor for the

`Input`

widget:`Input({title:"Input"})`

`title`

— The text title of the`Input`

`min`

= The minimum value that the user can enter in the`Input`

`max`

= The maximum value that the user can enter in the`Input`

`step`

= The step increment of the`Input`

`x = input.value`

— Returns the current value of the `Input`

.Example:

1

# Initial State editor

2

in = Input({title:"Size", max:10, min:-10, step:.1})

Copied!

1

# Calculations editor

2

x = in.value

Copied!

Menu

A

`Menu`

is an interactive widget that allows you to link a value in your scenario to the current value selected from a drop-down menu.A Menu widget

Below is the constructor for the

`Menu`

widget:`Menu({title:"Menu", choices:[], values:[]})`

`title`

— The text title of the`Input`

`choices`

— An array of menu choices.`values`

— An optional array of corresponding menu values for each choice. If no values are given, the values are assumed to be the choices.

`x = menu.value`

— Returns the current value of the `Menu`

.Example:

1

# Initial State editor

2

menu = Menu({title:"Pick One", choices:["A","B","C"], values:[1,2,3]})

Copied!

1

# Calculations editor

2

x = menu.value

Copied!

Interactivity

The following section describes additional interactivity that you can add through the use of

`keyboard`

and `mouse`

objects.keyboard

The

`keyboard`

object represents your computers keyboard and has commands to see if any keys are pressed during a simulation.`keyboard.is_down(key)`

-> `boolean`

— Return 1/0 whether `key`

is currently down`keyboard.last_pressed(key)`

-> `boolean`

— was `key`

typed? i.e. key was pushed down then released.mouse

The **'**s mouse.

`mouse`

object represents your computer`mouse.pos`

-> `vec`

— Returns two dimensional vector as a [X, Y] matrix representing the position of the mouse in the simulation space.`mouse.is_down(button_num=0)`

-> `boolean`

— Returns whether the mouse button is pressed. `button_num`

— Which button to check? 0 = primary button, 1 = secondary, etc.`mouse.is_over(object)`

-> `boolean`

— Returns whether the mouse is positioned over an object that is either a `Circle`

or a `Rectangle`

. `object`

— A simulation object.Data Output

You can create a representation of variable values, displayed in the **Data Output** tab, by using various API calls on the built in

`table`

object.table.setColumns

`table.setColumns(columns=[])`

- You must first define the column headings for the table in the Initial State pane. The arguments are simply an array of string values representing the column headings of the table.table.addRow

`table.addRow(row_data=[])`

- Once the table columns have been defined, you then need to define the row data to be added to the table. This is an array of any data type values.Example:

1

# Initial State editor

2

table.setColumns(["time", "x value", "y value"])

Copied!

1

# Calculations editorx = t * 2y = t / 2

2

table.addRow([t, x, y])

Copied!

The code above would generate this table:

The table output in the Data Output hack panel.

Last modified 9d ago

Copy link

Contents

Comments

Variables

Built-in Scenario Variables

Common Math Operators and Functions

Mathematical Operators

Basic Math Functions

Trigonometric Functions

Matrix Functions

Other Useful Functions

Collision Functions

Comparison Functions

Logical Operators

Built-in Classes

Circle

Rectangle

Arrow

Line

PolyLine

Spring

Label

Particle (deprecated)

Block (deprecated)

Interface Widgets

Graph

Meter

Gauge

Toggle

Slider

Input

Menu

Interactivity

keyboard

mouse

Data Output