Created
February 2, 2017 09:21
-
-
Save oberhamsi/ba6fb3fa926c6202c6cb95e01012b210 to your computer and use it in GitHub Desktop.
This file contains 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
// erklärung 3d: http://blackpawn.com/texts/pointinpoly/ | |
// code unten kopiert von: http://stackoverflow.com/a/2049593 | |
// auf welcher seite der gerade bc ist punkt a? | |
// http://stackoverflow.com/questions/22668659/calculate-on-which-side-of-a-line-a-point-is | |
// cross product 2d: http://allenchou.net/2013/07/cross-product-of-2d-vectors/ | |
function sign(a, b, c) { | |
return (a[0] - c[0]) * (b[1] - c[1]) | |
- (b[0] - c[0]) * (a[1] - c[1]); | |
} | |
// auf welcher seite der drei linien des dreiecks (a,b,c) liegt point? | |
// | |
function pointInTriangle(point, a, b, c) { | |
let rightSideOfAb = sign(point, a, b) < 0; | |
let rightSideOfBc = sign(point, b, c) < 0; | |
let rightSideOfCa = sign(point, c, a) < 0; | |
return ((rightSideOfAb == rightSideOfBc) && (rightSideOfBc == rightSideOfCa)); | |
} | |
// einfaches dreieck | |
console.log('yes', pointInTriangle([1,1], [0,0], [2,0], [0,2]) ); | |
// gleiches dreieck aber andere punkt-reihenfolge | |
console.log('yes', pointInTriangle([1,1], [2,0], [0,2], [0,0]) ); | |
// punkt liegt auf dem rand | |
console.log('yes', pointInTriangle([2,0], [0,0], [2,0], [0, 2])); | |
// punkt außerhalb des dreieicks | |
console.log('no', pointInTriangle([-1,1], [0,0], [2,0], [0,2]) ); | |
console.log('no', pointInTriangle([5,5], [0,0], [2,0], [0,2]) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment