appunti_tychos

https://docs.tychos.org/docs/

# questo è un commento le unità di misura (implicite) 

# sono quelle del S.I. : kg, m, s, m/s, N, J, W, etc 

# assegnazioni =====================

massa= 12 # sottointeso kg

pos = [10,2]    # sottointeso metri (è un vettore!)

F=[2,4]         # sottointeso newton (è un vettore!)

# assegnazioni di variabili legate ad un oggetto

pallone.pos=[-2,3]

pallone.v=[5,4]

# assegnazioni modifica:

x=x+1

# uniche variabili di sistema: =====================

# t        secondi passati dall'inizio della simulazione

# dt        intervallo di tempo tra un frame e l'altro

# frame_count quanti frame sono già passati

# X, Y X vale 0 ed Y vale 1, servono per poter scrivere  a=pos[X] oppure b=pos[Y]

# operatori e funzioni MATEMATICHE =====================

# + Addition

# - Subtraction

# * Multiplication

# / Division

# ^ Exponent

# % Modulo

# pow(2,3) -> 8

# sqrt(9) -> 3

# abs(-2) -> 2

# GONIOMETRICHE =====================

# sin(PI/2) -> 1

# cos(0) -> 1

# tan(PI/4)  -> 1

#  asin(1) -> 1.57..

#  acos(-1) -> 3.14..

#  atan2(1, -1) -> 2.36..

#  deg_to_rad(180) -> 3.14..

#  rad_to_deg(PI) -> 180

# MATRICI E VETTORI =====================

F = [Fx, Fy] # : un vettore

r = [rx, ry] # : un vettore 

Work = dot(F, r)  # (PRODOTTO SCALARE) 

F = [2, 0, 0]

r = [0, 2, 0]

M = cross(F, r)  # (PRODOTTO VETTORIALE) 

# returns matrix [0, 0, 4]

# Useful Functions =====================

random() # returns a random number between 0 and 1

random(100) # returns a random number between 0 and 100

random(30, 40) # returns a random number between 30 and 40

random([2, 3]) # returns a 2x3 matrix with random numbers between 0 and 1

s=string(object)

format(1/3, 3)  # returns '0.333'

format(21385, 2) # returns '21000'

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

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

A = [1, 2]

B = [3, 4]

concat(A, B)                  

# returns [1, 2, 3, 4]

math.concat(A, B, 0)           

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

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

# returns 'hello world'

drawArrow(pos=[0,0], size=[1,0], color="black", components=false, thickness=1)

drawLine(pos=[0,0], pos2=[10,0], color="black", thickess=1)

unit_vector(vec) # returns a vector of length 1, and in same direction as vec.

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

magnitude(u)             # returns 1

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

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

polar(radius, angle, units="rad") # returns a two dimensional vector as a [X, Y] matrix.

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

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

stop(test) #-> returns a either false or true. If true is returned, the simulation stops.

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

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

hasCollided(source, target) # -> returns a boolean true or false.

# source - Particle or Block object 

# target - Particle or Block object

# PARTICELLE e BLOCCHi

# Initial State

p1 = Particle([15, 0], 10, rgba(0, 200, 200, .6))

b1 = Block([0, 0], [15, 15], rgba(200, 0, 200, .6))

b1.rotate(45)

p3 = Particle([-15, 0], 10, rgba(0, 0, 200, .6))

# Calculations

hasCollided(p1, b1)            # returns true

hasCollided(b1, p3)            # returns true

hasCollided(p1, p3)            # returns false

######## INTERSEZIONI ########

getIntersect(source, target) # returns a two dimensional matrix. (il minimo vettore per de-sovrapporli)

# Initial State

p1 = Particle([0, 0], 10, rgba(200, 200, 200, .6))

p2 = Particle([12, 10], 10, rgba(0, 200, 0, .6))

b1 = Block([-12, 0], [15, 15], rgba(200, 0, 0, .6))

# Calculations

mtv1 = getIntersect(p1, p2)

mtv2 = getIntersect(p1, b1)

drawArrow(p1.pos, mtv1, "green")

drawArrow(p1.pos, mtv2, "red")

###############################

2 + 2 == 3             # returns false

2 + 2 == 4             # returns true

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

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

p1 = Particle([10, 10])

p2 = Particle([10, 0])

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

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

2 > 3               # returns false

3 > 2               # returns true

2 > 2               # returns false

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

2 < 3                # returns true

3 < 2                # returns false

2 < 2                # returns false

smaller(2, 2)        # returns false

2 + 2 != 3              # returns true

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

2 + 2 != 4              # returns false

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

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

F = (t > 10) * [10, 10] # true vale 1, false vale 0

if(true, 3, 44)                 # returns 3

if(false, 3, 44)                # returns 44

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

a = 1

b = 1

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

x = 2

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

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

(x < 1) or (x == 2)  # returns true

x = 2

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

#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 two simulated objects in Tychos are the Particleand the Block. The tools that can be used for analyzing the behavior of your simulations are the Graph and the Meter.

PARTICLE: 

costruttore:

p=Particle(pos=[0,0], radius=10, color=default_color)

proprietà

.pos 

metodi

p.rotate(angle=0, center_of_rotation=[0, 0])

p.addLabel(text="Hello", color="green")

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

BLOCK: 

costruttore:

b=Block(pos=[0,0], size=[10, 10], color=default_color)

proprietà

.pos 

metodi

b.rotate(angle=0, center_of_rotation=[0, 0])

b.addLabel(text="Hello", color="green")

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

b.rotate(direction(b.v)) # ruota nella direzione nella quale viaggia

SPRING:

Spring(pos=[0,0], pos2=[100, 0], color=default_color, coils=5, width=10, thickness=1)

Particle(pos=[0,0], radius=10, color=default_color) # returns a Particle

Particle(pos=[0,0], radius=10, color="green") # returns a green Particle

Particle(pos=[0,0], radius=10, color="#FF0000") # returns a red Particle

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

LABEL

Label(pos=[0,0], size=[10, 10], text="Hello", color=default_color) -> returns a Label object.

# Initial State editor

label1 = Label([0, 100], [50, 50], "Cat", "green")

label2 = Label([0, 0], [150, 150], "Dog", "red")

label3 = Label([0, -100], [50, 50], "Mouse", "blue")

label3.rotate(PI/4)

Interface Widgets ===================================

GRAPH  graph_x_t = Graph("X Position vs Time", "X Position", "Time")

graph_vx_t = Graph("$v_x$", "Velocity", "Time")

graph_ax_t = Graph("$a_x", "Acceleration", "Time")

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

# Initial State editor

g = Graph("X Positions vs Time")

# Calculations editor

# Graphing a particle projectile's Y velocity

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

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

# Initial State editor

g = Graph("Y Velocity")

# Calculations editor

# Graphing a particle projectile's Y velocity

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

g.integrate("green")

METER

meter.display(value, units) — Displays the value on the Meter.

value = Numeric value to be displayed.

units = String representing the unit label to be displayed.

# Initial State editor

mt = Meter("Time")

# Calculations editor

mt.display(t, "s")

GAUGE

# Initial State editor

g1 = Gauge("Value 1", 200, 0, "orange")

g2 = Gauge("Value 2", 100, -100, "purple")

g3 = Gauge("Value 3", 100, 0, "blue")

# Calculations editorval = 44

g1.display(val)

g2.display(val)

g3.display(val)

INTERACTIVITY ===================================

TOGGLE

# Initial State editor

t1 = Toggle("My Toggle")

# Calculations editor

isActive = t1.value

SLIDER

# Initial State editor

s1 = Slider("I'm A Slider", 0, 100, 2)

# Calculations editor

x = s1.value

keyboard

The keyboard object represents your computers keyboard and has commands to see if any keys are pressed during a simulation.

keyboard.is_down

keyboard.is_down(key) -> boolean — Return 1/0 whether key is currently down

keyboard.last_pressed

keyboard.last_pressed(key) -> boolean — was key typed? i.e. key was pushed down then released.

mouse

The mouse object represents your computer's mouse.

mouse.pos

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

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

mouse.is_over(object) -> boolean — Returns whether the mouse is positioned over an object that is either a Particle or a Block. object — A simulation object.

DATA OUTPUT ========================================

# Initial State editor

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

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

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

----