DaynaJuliana
DaynaJuliana

Reputation: 1164

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.

Upvotes: 3

Views: 5151

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 148880

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);
}

Upvotes: 1

MBo
MBo

Reputation: 80177

Yes, you may use inner_product (dot product) to take the result in very simple way.

Make vectors

V2 = P - P2
V3 = P - P3
V =  P3 - P2

Find signs of dot products D2 = Dot(V2,V) and D3 = Dot(V3,V)

Projection of Point P lies at S(P2, P3), if

D2 >=0 and
D3 <=0

Note - there is no need in normalizations, square roots etc. Just some subtractions, multiplications and additions.

(Explanation - angles P-P2-P3 and P-P3-P2 should be acute or right)

Upvotes: 1

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

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

Upvotes: 2

Related Questions