distance from point to line

Solutions on MaxInterview for distance from point to line by the best coders in the world

showing results for - "distance from point to line"
Giada
09 Oct 2017
1// Get the normal distance from a point P to a line 
2// defined by two points L1 and L2:
3//
4// Formula:
5
6float normalDistanceToLine(vec2 P, vec2 L1, vec2 L2) {
7    float dividend = abs((L2.x - L1.x)*(L1.y - P.y) - (L1.x - P.x)*(L2.y - L1.y));
8    float divisor = sqrt(pow(L2.x - L1.x, 2) + pow(L2.y - L1.y, 2));
9  
10    if (divisor != 0.f) {
11        return dividend / divisor;
12    else		// Points L1 and L2 are the same, choose one
13      	return sqrt(pow(P.x - L1.x, 2) + pow(P.y - L1.y, 2));
14}
15  
16// With a distance function (maybe for a glsl shader)
17  
18float normalDistanceToLine(vec2 P, vec2 L1, vec2 L2) {
19  
20    float dist = distance(L1, L2);
21  
22    if (dist != 0.f) {
23        return abs((L2.x - L1.x)*(L1.y - P.y) - (L1.x - P.x)*(L2.y - L1.y)) / dist;
24    else		// Points L1 and L2 are the same, choose one
25      	return distance(P, L1);
26}
27
similar questions
std distance
queries leading to this page
distance between a line and pointformula distance between point and linefind distance between point and linehow to find distance between point and linepoint from line distancecalculate distance from line to pointsdistance from a point ot a linedistance between point and linedistance from point to a line when does it workhow to find the distance between a point and a linedistance line and pointdistance from point to line linear algebrahow to find distance from point to lineshortest distance point on line to linecalculate distance from point to linedistance from a point on a linedistance from point to line calculatorhow to calculate distance between line and pointfind point on line at distance from another pointpoint to line distance formulaline and distance of a point to a lineget distance between line and pointcalculate distance of a point from a linedistance to linefind distance from point to linedistance of a point from a line formulafind point on line at distanceaverage distance from a point to a linehow to find distance from a point to a linedistance from point to line definded by two pointsdistance between a line and a pointdistance from point to line calculutorpoint to line distancedistance from a point to a linehow to work out the distance between a point and a linedistance of a line from a pointdistance from point to line cvector calculatordistance of a point from linefind distance between a point and a linepoint line distance formuladistance from point to a lineformula for distance between a point and a lineget point from line with distancedistance between a point and a line formuladistance of a point from a linedistance from a point along a linedistance line to pointdistance from line to pointfind distance from point to a linedistance point to linecalculate distance between line and pointcalculate distance between point and linepoint line distanceline point distance formulafind distance between a point and a line codedistance of line from point formuladistance from point to linedistance between a point and a lineformula for distance from point to linehow to find point distance from a linedistance of point from line formulapoint and line distance calculatordistance line pointdistance between line and a pointdistance of line from pointfind distance from line to pointpoint distance from linedistance of point to linehow to calculate distance from a point to a linecalculate distance from a point to a lineanother point on a line at given distancedistance between point to line formuladistance of point form a linehow to find the distance from a point to a linedistance of a point to a linefind the distance from a point to a linecalculate point on line at distancedistance of point from linedistance point line formuladistance from line to point calculatordistance between line and pointdistance from point to a line formuladistance from point to line