Skip to content

Instantly share code, notes, and snippets.

@zhiyelee
Created June 16, 2013 04:47
Show Gist options
  • Save zhiyelee/5790807 to your computer and use it in GitHub Desktop.
Save zhiyelee/5790807 to your computer and use it in GitHub Desktop.
/**
* @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