A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Pythagoras Theorem

  1. #1
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    Danny, normally to work out distances between 2 objects I would use pythagoras theroem ie:

    Code:
    distance = Math.sqrt((mc2._x - mc1._x)*2) + (mc2._y - mc1._y)*2));
    is this the best way to do this? Ie is it the fastest if I were to use this inside a function and use it throughout my movie?

  2. #2
    F# A# oo Ian424's Avatar
    Join Date
    Jun 2001
    Posts
    1,070
    im not the mod or a math expert, but i think thats the quickest way.

    distance = square root ((x1 - x2)^2 + (y1 - y2)^2)) ?

    (though maybe the * in yours was meant to be ^ instead, or im forgetting math)

    ..in fact, you could probably just make a function w/ that inside and plug it in, as far as i know its the closest thing to the actual equation/theorem/whatever as you can get.

  3. #3
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    Originally posted by Ian424

    (though maybe the * in yours was meant to be ^ instead, or im forgetting math)
    yeah, you're dead right. I was meant to write the long version:

    Code:
    distance = Math.sqrt((mc2._x - mc1._x)*(mc2._x - mc1._x)) + ((mc2._y - mc1._y)*(mc2._y - mc1._y));
    but I was being lazy, I meant squared, cheers Ian424

  4. #4
    Just an addition, hittesting circles that way works pretty good. You know, like radius of the two circles is half the distance, if distance is less then hit... better then the normal sqaure hittesting

  5. #5
    F# A# oo Ian424's Avatar
    Join Date
    Jun 2001
    Posts
    1,070
    Originally posted by DonDanny
    Just an addition, hittesting circles that way works pretty good. You know, like radius of the two circles is half the distance, if distance is less then hit... better then the normal sqaure hittesting
    any idea if this takes up more processor power than doing hitTest w/ 2 squares ? if it doesn't, i have some new footers to make

  6. #6
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    as a special request, I am going to explain pythagoras theorem. Please correct me if I am wrong.

    Basically Pythagoras Theorem is to work out the hypotenuse (longest side opposite the right angle) of a right angled triangle.



    So for example we know that the angle between a and b is a right angle and so therefore 90 degrees. We also know the length of the sides a and b. What we need to work out is the length of c.

    Lets say for example a is 5cm and b is 5.5cm

    pythagoras theorem says that a squared (written as a^2) plus b squared equals c squared.

    so:

    a^2 = 5^2 = 5*5 = 25

    b^2 = 5.5^2 = 5.585.5 = 30.25

    and because we know that c = b^2 + a^2

    we can equate

    c = sqrt(25+30.25) = sqrt(55.25) = 7.43303437365925

    so the distance of c is 7.43 (rounded).

    What use is this to us though, why would we need this in flash. well, imagine on the image above that the two red dots represent the position of movieclips. you can work out their position on the stage so with some simple mathematics you can work out the exact distance between them.

    Hope this is helpful to some.

    Bruce

  7. #7
    Hope my job as Math and Physics mod will always be that easy. I coundn't explain pythagoras theorem better, so I'll just say jup exactly ...

  8. #8
    It'll take longer Ian, but if you just do it for some objects it shouldn't be that processor intensive.

  9. #9
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    Nice explanation.

    Though you might find it a little neater if a=3 and b=4
    or a=5 and b=12
    or even a=99 and b=4900
    ..all resulting in c being a whole number. :-)

    They're what's known as pythagorean triples, where a^2 + b^2 = c^2 and a, b & c are all whole numbers.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center