Author Topic: Distance between SimpleVectors  (Read 4388 times)

Offline bigfishcatcher

  • byte
  • *
  • Posts: 17
    • View Profile
Distance between SimpleVectors
« on: September 05, 2008, 01:57:23 am »
Is there a way to get a float value to represents the distance between SimpleVectors

Thanks

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Distance between SimpleVectors
« Reply #1 on: September 05, 2008, 02:22:44 am »
A method like this should work:

Code: [Select]
public float distanceBetweenVectors( SimpleVector a, SimpleVector b )
{
    return (float) Math.sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z) );
}

Offline bigfishcatcher

  • byte
  • *
  • Posts: 17
    • View Profile
Re: Distance between SimpleVectors
« Reply #2 on: September 05, 2008, 02:33:47 am »
Thanks, that really makes my life easier.


Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Distance between SimpleVectors
« Reply #3 on: September 05, 2008, 03:42:50 am »
No problem  ;D

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Distance between SimpleVectors
« Reply #4 on: September 05, 2008, 09:24:13 am »
Another way to do it:

Code: [Select]
s1.calcSub(s2).length();

Maybe i should add that method to SimpleVector...

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Distance between SimpleVectors
« Reply #5 on: September 06, 2008, 05:30:21 am »
While you're at it, why not a calculateAngle(SimpleVector origin, SimpleVector p1, SimpleVector p2)?