Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created January 10, 2020 20:11
Show Gist options
  • Save misterpoloy/321c0a6a2086a40804e1aaab072856bb to your computer and use it in GitHub Desktop.
Save misterpoloy/321c0a6a2086a40804e1aaab072856bb to your computer and use it in GitHub Desktop.
Direction of a point from a line segment
// https://www.youtube.com/watch?v=VMVuKpj_RQQ&list=PL2_aWCzGMAwLL-mEB4ef20f3iqWMGWa25&index=9
#include <iostream>
struct Point {
double x, y;
Point(double x, double y)
: x(x), y(y)
{
};
};
Point substract(Point N, Point A) {
int x = N.x - A.x;
int y = N.y - A.y;
Point result(x, y);
return result;
};
double crossProduct(Point A, Point B) {
return A.x * B.y - B.x * A.y;
};
const int LEFT = -1; const int RIGHT = +1, ON_THE_LINE = 0;
int getDirection(Point A, Point B, Point P) {
B = substract(B, A);
P = substract(P, A);
double result = crossProduct(B, P);
if (result > 0) return LEFT;
if (result < 0) return RIGHT;
if (result == 0) return ON_THE_LINE;
return 123; // I'll fixt it later.
};
int main() {
Point A(-30, 10);
Point B(29, -15);
Point P(15, 28);
std::cout << getDirection(A, B, P) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment