Created
June 16, 2013 04:47
-
-
Save zhiyelee/5790807 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
/** | |
* @description check dot whether in a triangle | |
* @author zhiyelee | |
*/ | |
/** | |
* for tri like below: | |
* A | |
* * * | |
* B * * * C | |
*/ | |
// point is an object like A = [4,5] | |
// D is the point to check | |
function isInTriangle(A, B, C, D) { | |
return isLeftOfLine(A, B, D) && isLeftOfLine(B, C, D) && isLeftOfLine(C, A, D) | |
// check whether point `check` is on the left of vector `start-end` | |
function isLeftOfLine(start, end, check) { | |
return (end[0] - start[0]) * (check[1] - start[1]) - (end[1] - start[1]) * (check[0] - start[0]) > 0; | |
} | |
} | |
var A = [1, 3], | |
B = [0, 0], | |
C = [2, 0], | |
check1 = [2, 3], | |
check2 = [1, 1]; | |
console.info(isInTriangle(A, B, C, check1)); | |
console.info(isInTriangle(A, B, C, check2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment