Point->Line-Segment Distance ?

Started by
11 comments, last by Endemoniada 20 years, 9 months ago
Thanks everyone,

Mastaba, how do I find E in your solution ? That''s the point on Line(AB) that goes through my point C at a right angle to AB.

Thanks.
Advertisement
I don't really like to give explicit code, as I'd rather see people learn the math behind the code. I'll clearly give you a hint however.



Good Luck!

[edited by - Mastaba on July 3, 2003 8:53:33 PM]
.
point PointOnSegment(point C, point A, point B){    point D = (C.x - A.x, C.y - A.y);          // D = C - A    point E = (B.x - A.x, B.y - A.y);          // E = B - A    float DdotE (D.x * E.x + D.y * E.y);       // DdotE = D . E    float e2 = E.x*E.x + E.y*E.y;              // e2 = E . E    if (DdotE < 0.0f)                                  return A;    if (DdotE > e2)        return B;        DdotE /= e2;                               // DdotE = (D . E) / (E . E)    point F = (E.x * DdotE, E.y * DdotE);      // F = E * (D . E) / (E . E)    return point(A.x + F.x, A.y + F.y);        // return A + F}

Everything is better with Metal.

This topic is closed to new replies.

Advertisement