[Tutorial] Math.Distance
Author 
Message 
Tony

Posted: Thu Jun 03, 2004 11:52 am Post subject: [Tutorial] Math.Distance 


Math.Distance is a new function found in v4.0.5 of Turing. It is used to calculate the distance between any two sets of points on a 2D plane.
Quote:
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 HoltSoft for taking down their test patch, it works fine), but it is very easy to put together your own distance function:
code: 
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. 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. 
Tony's programming blog. DWITE  a programming contest. 




Sponsor Sponsor



Danyo

Posted: Thu Jun 03, 2004 11:23 pm Post subject: (No subject) 


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

Posted: Fri Jun 04, 2004 9:05 am Post subject: (No subject) 


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(1500)"
get line (i)
put "Enter your weight(110)"
get weight
put "Enter your curl(15)"
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) %Tline
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(1500)"
get line (i)
put "Enter your weight(110)"
get weight
put "Enter your curl(15)"
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) <= 50 and (w (i)  w (j)) <= 50 and dir = 0 then
crashx := j
end if
if (line (i) + ((drift * w (i)) div 100))  line (j) <= 50 and (w (i)  w (j)) <= 50 and dir = 1 then
crashy := j
end if
drawfilloval (line (j), w (j), 25, 25, 15) %rock outline grey
drawfilloval (line (j), w (j), 20, 20, c (j)) %Red/blue on rock
end for
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
View.Update
cls
end for
put sqrt ((rockposx  centerx) ** 2 + (rockposy  centery) ** 2)
%******************* Mod edit: That's plenty right there. Mazer
% if crashx not= 0 then
% catch := collision (crashx, weight, i)
%
% end if
% if crashy not= 0 then
% catch := collision (crashy, weight, i)
%
% end if
if dir = 0 then
line (i) := line (i)  ((drift * w (i)) div 100)
elsif dir = 1 then
line (i) := line (i) + ((drift * w (i)) div 100)
end if
%******************* Mod edit: That's plenty right there. Mazer
for score1 : 1 .. 1
if
end if
end for
end for
get dummy : 0
crashx := 0
crashy := 0
rockposx := (line (i)  ((drift * (drawrock)) div 100))
rockposy := drawrock
end for
%******************* Mod edit: That's plenty right there. Mazer
%******************* Mod edit: That's plenty right there. Mazer
if scoreBlue >= scoreRed then
put "Blue wins with ", scoreBlue, " points."
elsif scoreRed >= scoreBlue then
put "Red wins with ", scoreRed, " points."
else
put "you have tied."
end if 





Martin

Posted: Tue Jul 27, 2004 11:37 pm Post subject: (No subject) 


What you are doing wrong is not putting code brackets on your code. 





Tony

Posted: Tue Jul 27, 2004 11:43 pm Post subject: (No subject) 


what's wrong is posting 5 screens worth of code not relating to the tutorial what so ever 
Tony's programming blog. DWITE  a programming contest. 




Mazer

Posted: Wed Jul 28, 2004 7:52 am Post subject: (No subject) 


No, no. The real problem is having a really long line. Doesn't anybody take the horizontal scroll bar into consideration these days? We shouldn't be forced to use it!
EDIT: I think the funniest part was that they weren't even the same length. You could've found and fixed whatever your problem was in less time than it took you to type out all of those lines. 






