Direction of a Point from a Line Segment | C ++ Program to Determine Direction of Point from line segment ; structure for point in cartesian plane . ; constant integers for directions ; subtracting co - ordinates of point A from B and P , to make A as origin ; Determining cross Product ; return RIGHT if cross product is positive ; return LEFT if cross product is negative ; return ZERO if cross product is zero . ; Driver code ; A . y = 10 ; A ( - 30 , 10 ) ; B . y = - 15 ; B ( 29 , - 15 ) ; P . y = 28 ; P ( 15 , 28 )
#include <iostream> NEW_LINE using namespace std ; struct point { int x , y ; } ; const int RIGHT = 1 , LEFT = -1 , ZERO = 0 ; int directionOfPoint ( point A , point B , point P ) { B . x -= A . x ; B . y -= A . y ; P . x -= A . x ; P . y -= A . y ; int cross_product = B . x * P . y - B . y * P . x ; if ( cross_product > 0 ) return RIGHT ; if ( cross_product < 0 ) return LEFT ; return ZERO ; } int main ( ) { point A , B , P ; A . x = -30 ; B . x = 29 ; P . x = 15 ; int direction = directionOfPoint ( A , B , P ) ; if ( direction == 1 ) cout << " Right ▁ Direction " << endl ; else if ( direction == -1 ) cout << " Left ▁ Direction " << endl ; else cout << " Point ▁ is ▁ on ▁ the ▁ Line " << endl ; return 0 ; }