Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orthogonal Projection of Point onto Line

I'm referencing a mathematics paper, but the terminology is strange, and I'm unsure of how to code the following:

Return if the orthogonal projection of Point P exists on S(P2, P3).

I found std::inner_product but not sure if thats the correct method to use.

like image 317
DaynaJuliana Avatar asked Nov 24 '25 21:11

DaynaJuliana


2 Answers

The concept is that you project P onto S and then check whether that projection P' is between P2 and P3.

To make it a little easier you say that P2 is the support-vector of S and P3-P2 is the direction-vector. You then project P-P2 onto the normalized P3-P2 (you compute the scalar-product between them) which gives you the distance D of P' to P2. Now in your case you only want to know if P' is between P2 and P3. That is true if D is between 0 and 1.

enter image description here

like image 145
Bernd Elkemann Avatar answered Nov 26 '25 11:11

Bernd Elkemann


You want the orthogonal projection of P (on the line given by P2 and P3) to be inside the segment [P2,P3]. Mathematically, it writes simply (I'm noting vect(A, B) the vector AB, because I do not know how to use the arrow notation):

0 <= vect(P2, P) . vect (P2, P3) <= vect(P2, P3) . vect(P2, P3)

You can indeed use std::inner_product but if your points are something as simple as:

struct Point {
    double x;
    double y;
};

You could just use

double operator - (const Point& a, const Point& b) {
    return a.x - b.x + a.y - b.y;
}
double operator * (const Point& a, const Point& b) {
    return a.x * b.x + a.y * b.y;
}

And the mathematical formula just gives:

bool is_proj_inside(const Point& P, const Point& P2, const Point& P3) {
    double p_proj = (P - P2) * (P3 - P2);
    double p3_proj = (P3 - P2) * (P3 - P2);

    return (p_proj >= 0) && (p_proj <= p3_proj);
}
like image 32
Serge Ballesta Avatar answered Nov 26 '25 11:11

Serge Ballesta