Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function closestEdge(x,y,w,h) {
- var topEdgeDist = distMetric(x,y,w/2,0);
- var bottomEdgeDist = distMetric(x,y,w/2,h);
- var leftEdgeDist = distMetric(x,y,0,h/2);
- var rightEdgeDist = distMetric(x,y,w,h/2);
- var min = Math.min(topEdgeDist,bottomEdgeDist,leftEdgeDist,rightEdgeDist);
- switch (min) {
- case leftEdgeDist:
- return "left";
- case rightEdgeDist:
- return "right";
- case topEdgeDist:
- return "top";
- case bottomEdgeDist:
- return "bottom";
- }
- }
- function distMetric(x,y,x2,y2) {
- var xDiff = x - x2;
- var yDiff = y - y2;
- return (xDiff * xDiff) + (yDiff * yDiff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement