Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Math.Distance [Tutorial]
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
upthescale




PostPosted: Mon May 29, 2006 7:21 pm   Post subject: Math.Distance [Tutorial]

Ok, lets get a ready for a tutorial that I don't think has yet come in Comp.Sci, and it is obviosuly Math.Distance. Math.Distance, along with many other ways, is a way of creating a 'Hit Detection'...Usually with boxes, you may right in the coordinates liek so:
code:

if x > 100 and x < 200 and y > 100 and y< 200 then
%Code
end if

...Hmm, yes that was the Math way...Well, what about circles? there are ways to do it with circles also...
code:

if whatdotcolor (x,y) = 7 then
%Code
end if

There are other ways also, but I will explain Math.Distance...

First and foremost, you must create the variables...
code:

var ballx,bally,radius:int
ballx:=100
baly:=200
radius:=30


So now we have a variable for the whole ball, the radius can be whatever number you like, along with the ballx and bally. In this case, the ball will be the size of 30...

So let's assume that when the mouse hits the circle, the hit detection will occur, well we haven't created the x and y varialbes for the mouse have we?...

code:

var mx,my,b:int


Now we have...Ok, so now we will input the Math.Distance function, I will do it one line by one, and explain each line...Only one line hehe.

code:

if Math.Distance (mx,my,ballx,bally) < radius * 2 then
put"We Have A Detection!"
end if


Ok, a little confusing but not really. You see the brackets with the 4 variablesi n it, recognize them? Yes they are indeed the variables we declared earlier in the tutorial, mad flashbacks! So we have the mx, and my, ballx and bally all in one package. So when all these meet, and the mouse is les than the radius, then we have a detection. WHAT THE... WHAT THE HECK IS THE *2? well that is easy, because since the radius is in the middle of the circle, we want the detection to happen when we touch the outside of the circle. So by adding the * 2, the program will multiply all around the ball, bringing the detection to tyhe outside of the ball

Now we have that covered, but what about wh a circle hits a line? Well, for this, instead of Math.Distance, we will put:

code:
Math.DistancePointLine

An example would be:

code:

    if Math.DistancePointLine (linex, liney, 600, 200, 600, 300) < radius then
        Input.Pause
    end if


Now, it is the same thing, in this case you aren't using mousewhere, except you have a line (you can use mousewhere if you want, it would still work)...So you have linex,and liney, and the coordinates for the line all in the same package. What you can do is just copy and paste the line into the brackets beside your to other variables. (Don,t use the color, or there will be to many parameters)

Pretty Easy? I think so!

Here is a little program I put together, not the best coding, but it works:
code:

setscreen ("offscreenonly;nobuttonbar;title:Math.Distance Tutorial;nocursor")
var x, y, radius : int
var x2, y2 : int
var xchange, ychange : int := 1
var xchange2, ychange2 : int := 1
var answer : string
radius := 20
x := 100
y := 20
x2 := 680
y2 := 20
loop
    colorback (7)
    cls
    color (0)
    drawfilloval (x, y, radius, radius, red)
    drawoval (x, y, radius - 1, radius - 1, 0)
    x += xchange
    drawfilloval (x2, y2, radius, radius, red)
    drawoval (x2, y2, radius - 1, radius - 1, 0)
    x2 -= xchange2
    if x > maxx - 20 or x < 20 then
        xchange := -xchange
    end if
    if x2 > maxx - 20 or x2 < 20 then
        xchange2 := -xchange2
    end if
    %The Detection
    if Math.Distance (x, y, x2, y2) < radius * 2 then
        Input.Pause
        color (0)
        locate (22, 18)
        put "The Balls Have Collided All Because Of Math.Distance!"
    end if
    %The Detection
    drawfilloval (x, y, radius, radius, red)
    drawline (600, 0, 600, maxy, 12)
    x += xchange
    if x > maxx - 20 or x < 20 then
        xchange := -xchange
    end if
    if Math.DistancePointLine (x, y, 600, 0, 600, maxy) < radius then
        Input.Pause
        color (0)
        locate (22, 18)
        put "The Balls Have Collided All Because Of Math.Distance!"
    end if
    delay (5)
    View.Update
end loop


So I guess that is it for this tutorial on Math.Distance, Happy Programming
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Mon May 29, 2006 8:02 pm   Post subject: (No subject)

Er, not too bad, but the main problem I see here is that a lot of people don't have the Math module in their version of Turing...

Also, you're supposed to get these things approved by mods before you post them...
I know i'm a hypocrite, I forgot that when I was posting mine.

Anyways, instead of using Turings pre-built Math Modules, why not just create your own, that way you actually know whats going on in your code? For this tutorial, you should have at least posted what these functions actually do. For example, post the distance formula.

Now, on to the actual tutorial.

First and foremost,
Quote:
if Math.Distance (mx,my,ballx,bally) < radius * 2 then
put"We Have A Detection!"
end if


radius * 2 is not what you want. You want (circleRadiusOne + CircleRadiusTwo), because if the two circles are of a different size, then radius * 2 wont work. You failed to give this description in your explanation.

Also, rather than saying (mx,my,ballx,bally), use what Turing Help gives you,

Quote:

Math.Distance (x1, y1, x2, y2 : real) : real


In your code, it would be assumed that you can only use integers for Math.Distance, but this is not so!

Same thing applies to your Math.DistancePointLine code (Though that one was just wrong anyways...)

Quote:

Math.DistancePointLine (xp, yp, x1, y1, x2, y2 : real) : real


xp,yp is the point. (x1,y1) (x2,y2) are the ordered pairs of the line.

You said:
Quote:
Math.DistancePointLine (linex, liney, 600, 200, 600, 300)


I havn't checked over your program yet, but i'll look it over and post again when i'm done with that.
Cervantes




PostPosted: Mon May 29, 2006 8:37 pm   Post subject: (No subject)

It should be noted that Math.Distance and Math.DistancePointLine can be added to your Turing library. Instructions are here.

Is it just me and Delos or are these kinds of tutorials aggrivating? For example, is it a good tutorial if the author writes,
upthescale wrote:
Now we have...Ok, so now we will input the Math.Distance function

It sounds like you really don't know what you're talking about and you are making it up as you go, refusing to use the backspace key.

Though once V3 comes out, I guess I'd promote these kinds of submissions is since they won't clog down the 'official' tutorials as they will have their own place. And really, I'll bet upthescale learned a lot while making this tutorial. Smile
Delos




PostPosted: Mon May 29, 2006 10:39 pm   Post subject: (No subject)

Learning is good, to be sure. I will put forth the suggestion I usually do in cases like these, if upthescale would like to improve his tutorial - simply PM either Cervantes or myself and we'll send you the necassary BBCode. Of course, you will have to do a hefty job in making this worthy of some of the other high-class tuts around here.
Windsurfer




PostPosted: Mon Jun 05, 2006 10:12 pm   Post subject: (No subject)

People, people! Math.Distance is slow! i don't know why, but i tested it comparing the timing of a function I made:

code:


function Pt_Near (h1, v1, h2, v2, dist : real) : boolean
    %which method is faster?
    %result sqrt ((h2 - h1) *(h2 - h1) + (v2 - v1) *(v2 - v1)) < dist
    result sqrt ((h2 - h1) ** 2 + (v2 - v1) ** 2) < dist
end Pt_Near



Using a program as follows, i found that my algorythm is faster:

code:


var last_time : real
function Pt_Near (h1, v1, h2, v2, dist : real) : boolean
    result sqrt ((h2 - h1) ** 2 + (v2 - v1) ** 2) < dist
end Pt_Near

var test1, test2 : string
put "Running... time is in miliseconds..."

%These values are completly arbitrary
loop
    last_time := Time.Elapsed
    for lp : 1 .. 100000
        % const this := lp
        if Math.Distance (1, 1, 34, 23) < 54 then
        end if
    end for
    test1 := "using turing's built-in: " + realstr (Time.Elapsed - last_time, 15)

    last_time := Time.Elapsed
    for lp : 1 .. 100000
        if Pt_Near (1, 1, 34, 23, 54) then
        end if
    end for
    test2 := "using my more efficient calculation: " + realstr (Time.Elapsed - last_time, 3)
    cls
    put test1
    put test2
end loop



so yeah... i just felt like sharing that. Would anyone be able to explain Turing's slowness? I'd really like to know, because I've been fighting with it's extremely poor preformance (especially when dealing with graphix... why can't there be some sort of DirectDraw plugin or something for hardware acceleration?)
Delos




PostPosted: Tue Jun 06, 2006 9:11 am   Post subject: (No subject)

Yes Turing is slow with graphics. There is not way around that. It's an interpreted language so that is to be expected. I've heard, overever, that 4.1 is a little better w/ graphics. But then methinks you already use 4.1 so that is of no help in any case.
As for DX - not going to happen. If you're at the point where you need to use DX (note: need not want) then it's time for a new language!
Windsurfer




PostPosted: Tue Jun 06, 2006 11:31 am   Post subject: (No subject)

Delos wrote:
If you're at the point where you need to use DX (note: need not want) then it's time for a new language!


Ahaha, how right you are! However, since my final project must be written in turing... i have no choice. And, yes, need is the right word. Take a look at my FP Smile http://www.compsci.ca/v2/viewtopic.php?t=12468
evildaddy911




PostPosted: Sun Oct 16, 2011 2:54 pm   Post subject: RE:Math.Distance [Tutorial]

so what are each of the parameters in Math.DistancePointLine?
EDIT sorry, i didnt see that post
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: