Created
August 6, 2012 14:30
-
-
Save tmshv/3274821 to your computer and use it in GitHub Desktop.
Определение положения точки относительно направленного отрезка прямой линии
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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