Re: Fw: Distance between 2 points

new topic     » goto parent     » topic index » view thread      » older message » newer message

Ralf Nieuwenhuijsen wrote:
>...to be dimension free, who knows how many dimensions
>we are gonna explore in the future ?
agreed, wholeheartedly...

>That is also the problem with your function here,
>you forget to substract.
>   function distance(object a, object b)
>      return sqrt( (a*a)+(b*b) )
>   end function
>I think you meant this:
actually, i was thinking of a pair of lists of
right triangle legs.... so i really shoulda
called it:
   function hyp(object opp, object adj)
        return sqrt( (opp*opp)+(adj*adj) )
   end function
and then you throw a list of pairs of legs at it...
i figured doing it this way would make the
angle calc easier (as carl showed how to do that)
not only that, the theorem states:
c^2=a^2 + b^2 which is what i thought was getting
thrown at it in the first place...

using carl's and mine together:
   function hyp2d(object opp, object adj)
        return sqrt( (opp*opp)+(adj*adj) )
   end function
   function hyp3d(object x, object y, object z)
        return sqrt( (x*x)+(y*y)+(z*z) )
   end function

function FindDistanceAngle(sequence coor1, sequence coor2)
--accepts a pair of coordinate lists, in the form:
--{{x,y},{x,y}...{x,y}} OR {{x,y,z},{x,y,z}...{x,y,z}}
--and calculates the distance between them and the relative angle
--returns {distance,theta} where distance and theta
--are sequences equal to length(coor1) and theta is in radians.
--now, theta bears special note, that if it refers to a 2d
--world then theta is a sequence of atoms.  if theta
--is to refer to a 3d world then theta is a sequence of
--sequences in the form {rotation,rise} where rotation
--is the angle along the x/y axis' and rise is the angle
--above or below that plane
integer len
sequence dist,theta
object opp,adj,x,y,z
  len = length(coor1)
  if len!=length(coor2) then
        Error("unequal argument length ")
  end if
  dist=coor2-coor1
  theta=repeat(0,len)
  temp=dist[1]
  if atom(temp) then
        Error("got a 2 sided triangle handy???")
  end if
  if  length(temp)=2 then
        for i=1 to len do
           temp=dist[i]
           adj=temp[1] opp=temp[2]
           --below is what i had in mind
           dist[i] =hyp2d(opp,adj)
           theta[i]=arctan(opp/adj)
        end for
  elsif length(temp)=3 then
        for i=1 to len do
           temp=dist[i]
           x=temp[1] y=temp[2] z=temp[3]
           dist[i] =hyp3d(x,y,z)
           temp    =arctan(y/x)
           theta[i]={temp,arctan(z/temp)}
        end for
  else  Error("trying to use the 4th dimension? ;)")
  end if
  return {dist,theta}
end function

>>   function distance(object a, object b)
>>      return power( (a*a)+(b*b),0.5 )
>>   end function
>It might be less precize, because the result will always
>be a float because you use .5
yeah i was worried about that...
a 3-4-5 triangle becomes a 3-4-4.999999 triangle...
*blech*

>I wonder when Robert is gonna make calculations with
>floats convert back to machine integers when possible.
that could be handy...

>>to see if power is faster than sqrt.
>Something I dont expect.
ditto... but it would be interesting to see, no? :)

>First of all, sqrt takes less arguments, and thus
>needs less conversion.
ah, but was there actually another thread written for that,
OR!, does sqrt actually call power??? makes ya wonder eh? :)

> Off course a simple benchmark would tell wouldn't it ?
'tis why i suggested it compadre...

>And now here's my fastest function I could come
>up with (allowing a number of dimension ranging
>from 0 to eternity)

> function distance (object a, object b)
>     if atom (a) and atom(b) then
>         if a > b then
>             return a-b
>         else
>             return b-a
>         end if
>     end if
--kill this below
>     a = power(a - b,2)
--these two lines, instead of power(a-b,2) may
--be a lot faster... EU is optimized for them
      a=a-b
      a=a*a
--note: using a=(a-b)*(a-b) calculates (a-b) TWICE...
>     b = a[1]
>     for index = 2 to length(a) do
>         b = b + a[index]
>     end for
>     return sqrt (b)
> end function

> Now, how does this benchmark ?
try both ways.. eh???

--Hawke'

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu