Computer Science Canada

[source] string/chain

Author:  zylum [ Sat Jan 14, 2006 6:54 pm ]
Post subject:  [source] string/chain

was screwing around and came up with this Wink

code:
setscreen ("graphics:max;max,offscreenonly")

type Node :
    record
        x, y, vx, vy : real
    end record

const N := 100
const d := 8

var nodes : array 1 .. N of Node
for i : 1 .. N
    nodes (i).x := Rand.Int (1, maxx)
    nodes (i).y := Rand.Int (1, maxy)
    nodes (i).vx := 0
    nodes (i).vy := 0
end for

var F : real
var mx, my, md : int

function whatAngle (dx, dy : real) : real
    var ratio, angle : real
    if abs (dx) > 0.0000001 then
        ratio := dy / dx
    else
        ratio := dy / 0.000001
    end if
    angle := arctand (abs (ratio))
    if dx < 0 then
        angle := 180 - angle
    end if
    if dy < 0 then
        angle := 360 - angle
    end if
    result angle
end whatAngle


loop
    mousewhere (mx, my, md)

    for i : 1 .. N
        if i = 1 then
            F := - (Math.Distance (mx, my, nodes (i).x, nodes (i).y) - d)
            nodes (i).vx := cosd (whatAngle (nodes (i).x - mx, nodes (i).y - my)) * F
            nodes (i).vy := sind (whatAngle (nodes (i).x - mx, nodes (i).y - my)) * F
        else
            F := - (Math.Distance (nodes (i - 1).x, nodes (i - 1).y, nodes (i).x, nodes (i).y) - d)
            nodes (i).vx := cosd (whatAngle (nodes (i).x - nodes (i - 1).x, nodes (i).y - nodes (i - 1).y)) * F
            nodes (i).vy := sind (whatAngle (nodes (i).x - nodes (i - 1).x, nodes (i).y - nodes (i - 1).y)) * F
        end if

        nodes (i).x += nodes (i).vx
        nodes (i).y := max (0, nodes (i).y + nodes (i).vy)
        if i = 1 then
            %Draw.ThickLine (mx, my, nodes (i).x div 1, nodes (i).y div 1, 2, black)
        else
            %Draw.ThickLine (nodes (i).x div 1, nodes (i).y div 1, nodes (i - 1).x div 1, nodes (i - 1).y div 1, 2, black)
        end if
        drawoval (nodes (i).x div 1, nodes (i).y div 1, d div 2, d div 2, black)
    end for
    Time.DelaySinceLast (10)
    View.Update
    cls
end loop

Author:  masterfenix [ Sat Jan 14, 2006 7:09 pm ]
Post subject: 

what is the code for

Math.Distance

cuz it says its not in my export list.

Author:  Cervantes [ Sat Jan 14, 2006 7:26 pm ]
Post subject: 

It determines the distance between two points in a cartesian plane. You need Turing 4.0.5 for it to be in the export list of the Math module. You can make your own:

code:

function MathDistance (x1, y1, x2, y2 : real) : real
  result ((x2 - x1)**2 + (y2 - y1)**2)**0.5
end MathDistance

Author:  Delos [ Sat Jan 14, 2006 8:18 pm ]
Post subject: 

How's this as an edit...Very Happy

code:

setscreen ("graphics:max;max,offscreenonly;nobuttonbar")
colourback (black)

type Node :
    record
        x, y, vx, vy : real
        col : int
    end record

const N := 100
const d := 8

var nodes : array 1 .. N of Node
fcn calcCol (inI : int) : int
    result 16 + inI div (N div 12)
end calcCol
for i : 1 .. N
    nodes (i).x := Rand.Int (1, maxx)
    nodes (i).y := Rand.Int (1, maxy)
    nodes (i).vx := 0
    nodes (i).vy := 0
    nodes (i).col := 30 + (16 - calcCol (i))
end for

var F : real
type mouse :
    record
        mx, my, mb, mcue : int
        motion : string
    end record

var _m : mouse
_m.motion := "down"

function whatAngle (dx, dy : real) : real
    var ratio, angle : real
    if abs (dx) > 0.0000001 then
        ratio := dy / dx
    else
        ratio := dy / 0.000001
    end if
    angle := arctand (abs (ratio))
    if dx < 0 then
        angle := 180 - angle
    end if
    if dy < 0 then
        angle := 360 - angle
    end if
    result angle
end whatAngle


loop
    Mouse.Where (_m.mx, _m.my, _m.mb)
    if Mouse.ButtonMoved (_m.motion) then
    Mouse.ButtonWait (_m.motion, _m.mx, _m.my, _m.mb, _m.mcue)
        for i : 1 .. upper (nodes)
            nodes (i).x := Rand.Int (1, maxx)
            nodes (i).y := Rand.Int (1, maxy)
        end for
    end if

    for decreasing i : N .. 1
        if i = 1 then
            F := - (Math.Distance (_m.mx, _m.my, nodes (i).x, nodes (i).y) - d)
            nodes (i).vx := cosd (whatAngle (nodes (i).x - _m.mx, nodes (i).y - _m.my)) * F
            nodes (i).vy := sind (whatAngle (nodes (i).x - _m.mx, nodes (i).y - _m.my)) * F
        else
            F := - (Math.Distance (nodes (i - 1).x, nodes (i - 1).y, nodes (i).x, nodes (i).y) - d)
            nodes (i).vx := cosd (whatAngle (nodes (i).x - nodes (i - 1).x, nodes (i).y - nodes (i - 1).y)) * F
            nodes (i).vy := sind (whatAngle (nodes (i).x - nodes (i - 1).x, nodes (i).y - nodes (i - 1).y)) * F
        end if

        nodes (i).x += nodes (i).vx
        nodes (i).y := max (0, nodes (i).y + nodes (i).vy)
        if i = 1 then
            %Draw.ThickLine (mx, my, nodes (i).x div 1, nodes (i).y div 1, 2, black)
        else
            %Draw.ThickLine (nodes (i).x div 1, nodes (i).y div 1, nodes (i - 1).x div 1, nodes (i - 1).y div 1, 2, black)
        end if
        drawoval (nodes (i).x div 1, nodes (i).y div 1, d div 2, d div 2, nodes (i).col)
    end for
    Time.DelaySinceLast (10)
    View.Update
    cls

    exit when hasch
end loop


Edit: Update, 2nd time! Click!

Author:  zylum [ Sun Jan 15, 2006 8:37 am ]
Post subject: 

i intended the string to have rigid connections.. i initially started with spring connections, ie k and it was sort of similar to yours Wink

Author:  Clayton [ Mon Jan 16, 2006 2:36 am ]
Post subject: 

very cool i wish i could program like u can gj


: