Skip to content

Instantly share code, notes, and snippets.

@tmshv
Created August 6, 2012 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmshv/3274821 to your computer and use it in GitHub Desktop.
Save tmshv/3274821 to your computer and use it in GitHub Desktop.
Определение положения точки относительно направленного отрезка прямой линии
/**
* define type of point relativly line
* point: {x:[number], y:[number]}
* line:
{
first:{x:[number], y:[number]},
second:{x:[number], y:[number]}
}
*/
function classify(point, line) {
var p0 = line.first;
var p1 = line.second;
var p2 = point;
var a = {
x: p1.x - p0.x,
y: p1.y - p0.y
);
var b = {
x: p2.x - p0.x,
y: p2.y - p0.y
);
var sa = a.x * b.y - b.x * a.y;
if (sa > 0.0) return "left";
if (sa < 0.0) return "right";
if ((a.x * b.x < 0.0) || (a.y * b.y < 0.0)) return "behind";
if (a.length < b.length) return "beyond";
if (p0.equals(p2))return "origin";
if (p1 == p2)return "destination";
return "between";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment