Difference between revisions of "Talk:LlVecDist"

From Second Life Wiki
Jump to: navigation, search
(example script fDistanceOfPointFromLine)
 
(added simpler solution)
 
Line 8: Line 8:
 
</lsl>
 
</lsl>
 
--[[User:MartinRJ Fayray|MartinRJ Fayray]] 11:49, 1 April 2013 (PDT)
 
--[[User:MartinRJ Fayray|MartinRJ Fayray]] 11:49, 1 April 2013 (PDT)
 +
 +
Here's a simpler solution using cross products:
 +
<lsl>
 +
float fDistanceOfPointFromLine(vector veca, vector vecb, vector vecc)
 +
{ //vecc is the point
 +
    float A = llVecMag((veca-vecc)%(vecb-vecc));
 +
    return A / llVecMag(vecb-veca);
 +
}
 +
</lsl>
 +
--[[User:MartinRJ Fayray|MartinRJ Fayray]] 11:58, 1 April 2013 (PDT)

Latest revision as of 11:58, 1 April 2013

Example script to calculate the distance of a vector-point from a 'line' (using Heron's formula)

<lsl> float fDistanceOfPointFromLine(vector veca, vector vecb, vector vecc) { //vecc is the point (veca and vecb are points on the line)

   float a = llVecDist(vecb, vecc); float b = llVecDist(veca, vecc); float c = llVecDist(veca, vecb); float s=(1.0/2.0)*(a+b+c);  float hc =(2.0/c)*llSqrt(s*(s-a)*(s-b)*(s-c));
   return hc;

} </lsl> --MartinRJ Fayray 11:49, 1 April 2013 (PDT)

Here's a simpler solution using cross products: <lsl> float fDistanceOfPointFromLine(vector veca, vector vecb, vector vecc) { //vecc is the point

   float A = llVecMag((veca-vecc)%(vecb-vecc));
   return A / llVecMag(vecb-veca);

} </lsl> --MartinRJ Fayray 11:58, 1 April 2013 (PDT)