Showing posts with label distance. Show all posts
Showing posts with label distance. Show all posts

Thursday, May 25, 2017

Euclidean distance between a point and a line segment

Function to find the shortest distance between a point and a line segment
Usage:


echo(point_to_line(   [150,50,0],   [0,0,0],   [100,0,0]  ));

function point_to_line(p,a,b)=
let(
pa = p - a, 
ba = b - a,
h = clamp( (pa*ba)/ (ba*ba)))
norm( pa - ba*h ) ;
function clamp(a, b = 0, c = 1) = min(max(a, b), c); 

Tuesday, May 16, 2017

Distance from a point to a plane

Usage:

my_dist=distance_point2plane([0,0,50],[100,0,0],[-1,0,0]);
point_projected_on_plane= point-my_dist*planes_normal;


function distance_point2plane(point, planes_origin, planes_normal) =
let (v = point - planes_origin)   
(
v.x * planes_normal.x + 
v.y * planes_normal.y + 
v.z * planes_normal.z
)); 

Friday, May 12, 2017

Euclidean distance

Euclidean distance of a vector in n dimensions
Usage:


echo( len3( [1,1,2] ) );
echo( len3( [1,1,2] - [0,2,0] ) );


function len3(v) =len(v)>1?
sqrt(addl([for(i=[0:len(v)-1])pow(v[i], 2)]))
:len(v)==1?v[0]:v; 
function addl(l,c=0)= len(l)-1 >c?l[c]+addl(l,c+1):l[c];