
-----------------------------------
Tony
Thu Jun 03, 2004 11:52 am

[Tutorial] Math.Distance
-----------------------------------
Math.Distance is a new function found in 
Math.Distance(x1,y1,  x2, y2 :real) :real


The math behind the function goes back to grade 10 math when we used to find the distances between points (remember that?). We would construct a triangle where the difference between the X values is our base and difference betwee the Y values is the height and we then would find the hypotinuce (distance between two points) using c^2 = a^2 + b^2

Now I realize that in face only few people have the latest release of Turing at this point (blame test patch, it works fine), but it is very easy to put together your own distance function:


function distance (x1, y1, x2, y2 : real) : real
    result sqrt ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
end distance


There're many instances when you would want to find the distance between two given points. [url=http://www.compsci.ca/v2/viewtopic.php?t=75#144]collision detection comes to mind.

Another useful application would be scoring in any type of shooting game where points are expressed as a function in relation to the distance between hit and bull's eye.

-----------------------------------
Danyo
Thu Jun 03, 2004 11:23 pm


-----------------------------------
Would you possibly know the function that was used for the other Math.Distance that calcuates one point to a line?
I would have used the actual function, but our school uses 4.0.4 and it doesn't have it.

-----------------------------------
Leny
Fri Jun 04, 2004 9:05 am


-----------------------------------
yeah alright i'm trying this math.distance on 4.0.4 with your function but i'm having some troubles ok so what i'm doing is i'm shooting curling rocks with curl right so everytime i shoot a rock i ask for four numbers line weight curl and direction and i do some wierd math to get it to curl the right way 

put "Enter your line(1-500)"
    get line (i)
    put "Enter your weight(1-10)"
    get weight
    put "Enter your curl(1-5)"
    get curl1
    put "curl Direction(L=0/R=1)"
    get dir
    for curl : 1 .. weight
        curlnum -= (curl1 * 5 - 1)
    end for
    w (i) := 50 * weight + curl1 * 5 - 1
    drift := curlnum * 100 div w (i)
    get dummy : 0


then it makes the rock come into the house which i have a function to draw 

if i mod 2 = 0 then
            c (i) := 39
        else
            c (i) := 9
        end if
        if crashx = 0 and crashy = 0 then
            if dir = 0 then
                drawfilloval ((line (i) - ((drift * (drawrock)) div 100)), drawrock, 25, 25, 15)
                drawfilloval ((line (i) - ((drift * (drawrock)) div 100)), drawrock, 20, 20, c (i))
            else
                drawfilloval ((line (i) + ((drift * drawrock) div 100)), drawrock, 25, 25, 15)
                drawfilloval ((line (i) + ((drift * drawrock) div 100)), drawrock, 20, 20, c (i))
            end if
        elsif crashx not= 0 then
            drawfilloval ((line (i) - ((drift * (drawrock) - 50) div 100)), (drawrock - 50), 25, 25, 15)
            drawfilloval ((line (i) - ((drift * (drawrock) - 50) div 100)), (drawrock - 50), 20, 20, c (i))

        elsif crashy not= 0 then
            drawfilloval ((line (i) + ((drift * (drawrock) + 50) div 100)), (drawrock - 50), 25, 25, 15)
            drawfilloval ((line (i) + ((drift * (drawrock) + 50) div 100)), (drawrock - 50), 20, 20, c (i))

        end if

there is also some colision detection in there but it's a little messed up too 

this is the whole code with errors though


WHAT AM I DONG WRONG????




setscreen ("graphics:500;500")
setscreen ("offscreenonly")
setscreen ("echo")
%a curling game
%******************* Mod edit: That's plenty right there. -Mazer
%variables for making the rock move
var dummy, winner : string
var curl1, dir, weight, drawtest, num, crashx, crashy, temp, catch, scoreBlue, scoreRed, centerx, centery, rockposx, rockposy : int := 0
var drift : real
var curlnum : int := 250
var line, w, c, NewLine, NewW : array 1 .. 16 of int

function drawhouse : int %function that draws the house
    drawfillbox (0, 0, 500, 500, 0) %White backgroud
    drawfilloval (250, 250, 200, 200, 1) %blue 8 foot circle
    drawfilloval (250, 250, 150, 150, 0) %white 6 foot circle
    drawfilloval (250, 250, 85, 85, red) %red 4 foot circle
    drawline (35, 250, 465, 250, 7) %T-line
    drawline (35, 0, 35, 450, 7) %east sideline
    drawline (465, 0, 465, 450, 7) %west sideline
    drawline (250, 450, 250, 0, 7) %centerline
    drawline (35, 450, 465, 450, 7) %backline
    drawfilloval (250, 250, 30, 30, 0) %white button
    drawfilloval (250, 250, 1, 1, 7) %center of the house
    result 1
end drawhouse
%******************* Mod edit: That's plenty right there. -Mazer
function collision (Crashx : int, Weight : int, I : int) : int %function for collision detection
    View.Update %updates the view if i colision is detected
    for b : 1 .. Weight * 30
        drawtest := drawhouse
        for j : 1 .. I
            if j = Crashx then
                if dir = 1 then
                    drawfilloval ((line (Crashx) - (b)), w (Crashx) + b, 25, 25, 15) %rock outline grey
                    drawfilloval ((line (Crashx) - (b)), w (Crashx) + b, 20, 20, c (j)) %Red/blue on rock
                elsif dir = 0 then
                    drawfilloval ((line (Crashx) + (b)), w (Crashx) + b, 25, 25, 15) %rock outline grey
                    drawfilloval ((line (Crashx) + (b)), w (Crashx) + b, 20, 20, c (j)) %Red/blue on rock
                end if
            else
                drawfilloval (line (j), w (j), 25, 25, 15)
                drawfilloval (line (j), w (j), 20, 20, c (j))
                temp := Crashx
            end if
        end for
        View.Update
    end for

    if dir = 0 then
        line (temp) := line (temp) + (Weight * 30)
        w (temp) := w (temp) + (Weight * 30)
    elsif dir = 1 then
        line (temp) := line (temp) - (Weight * 30)
        w (temp) := w (temp) + (Weight * 30)
    end if
    result line (Crashx) + Weight * 30
end collision
%******************* Mod edit: That's plenty right there. -Mazer
function distance (centerx, centery, rockposx, rockposy : int) : real
    
    result sqrt ((rockposx - centerx) ** 2 + (rockposy - centery) ** 2)
end distance
%******************* Mod edit: That's plenty right there. -Mazer
%******************* Mod edit: Two lines now?. -Mazer
for n : 1 .. 16
    winner := " "
end for
%******************* Mod edit: That's plenty right there. -Mazer
for i : 1 .. 16
    setscreen ("text")
    curlnum := 0
    centerx := 250
    centery := 250


    %******************* Mod edit: That's plenty right there. -Mazer

    put "Enter your line(1-500)"
    get line (i)
    put "Enter your weight(1-10)"
    get weight
    put "Enter your curl(1-5)"
    get curl1
    put "curl Direction(L=0/R=1)"
    get dir
    for curl : 1 .. weight
        curlnum -= (curl1 * 5 - 1)
    end for
    w (i) := 50 * weight + curl1 * 5 - 1
    drift := curlnum * 100 div w (i)
    get dummy : 0
    setscreen ("graphics:500;500")
    cls
    View.Update
    drawtest := drawhouse
    %View.Update
    %******************* Mod edit: That's plenty right there. -Mazer
    for drawrock : 1 .. w (i)
        drawtest := drawhouse
        for j : 1 .. i - 1
            if (line (i) - ((drift * w (i)) div 100)) - line (j) 