Last active
August 29, 2015 14:19
-
-
Save Aleksey-Danchin/f20123b6c74c2478159a to your computer and use it in GitHub Desktop.
Функция определения факта пересечения двух отрезков на 2-х мерной декартовой системе координат.
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
function isIntersection (x1, y1, x2, y2, x3, y3, x4, y4) { | |
var point1 = {x: x1, y: y1}, | |
point2 = {x: x2, y: y2}, | |
point3 = {x: x3, y: y3}, | |
point4 = {x: x4, y: y4}; | |
var line1 = {}, | |
line2 = {}; | |
init(); | |
if (line1.k === line2.k) | |
return isBetween(line1, point3) | |
|| isBetween(line1, point4) | |
|| isBetween(line2, point1) | |
|| isBetween(line2, point2); | |
var alpha = (Math.atan(line1.k) + Math.atan(line2.k)) / 2; | |
point1 = turnCoordinates(point1, alpha); | |
point2 = turnCoordinates(point2, alpha); | |
point3 = turnCoordinates(point3, alpha); | |
point4 = turnCoordinates(point4, alpha); | |
init(); | |
var intersectionPoint = {}; | |
intersectionPoint.x = (line2.b - line1.b) / (line1.k - line2.k); | |
intersectionPoint.y = line1.k * intersectionPoint.x + line1.b; | |
return ( | |
isBetween(line1, intersectionPoint) | |
&& | |
isBetween(line2, intersectionPoint) | |
); | |
function init () { | |
line1.k = (point1.y - point2.y) / (point1.x - point2.x); | |
line1.b = point1.y - line1.k * point1.x; | |
line1.left = point1.x <= point2.x ? point1 : point2; | |
line1.right = line1.left === point1 ? point2 : point1; | |
line1.top = point1.y >= point2.y ? point1 : point2; | |
line1.bottom = line1.top === point1 ? point2 : point1; | |
line2.k = (point3.y - point4.y) / (point3.x - point4.x); | |
line2.b = point3.y - line2.k * point3.x; | |
line2.left = point3.x <= point4.x ? point3 : point4; | |
line2.right = line2.left === point3 ? point4 : point3; | |
line2.top = point3.y >= point4.y ? point3 : point4; | |
line2.bottom = line2.top === point3 ? point4 : point3; | |
if (Math.abs(line1.k) === Infinity) line1.k = Infinity; | |
if (Math.abs(line2.k) === Infinity) line2.k = Infinity; | |
} | |
function isBetween (line, point) { | |
return line.left.x <= point.x && point.x <= line.right.x && | |
line.bottom.y <= point.y && point.y <= line.top.y; | |
} | |
function turnCoordinates (point, angle) { | |
return { | |
x: point.x * Math.cos(angle) - point.y * Math.sin(angle), | |
y: point.x * Math.sin(angle) + point.y * Math.cos(angle) | |
}; | |
} | |
} |
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
function isIntersection(t,x,y,n,o,a,k,r){function i(){M.k=(b.y-f.y)/(b.x-f.x),M.b=b.y-M.k*b.x,M.left=b.x<=f.x?b:f,M.right=M.left===b?f:b,M.top=b.y>=f.y?b:f,M.bottom=M.top===b?f:b,c.k=(s.y-u.y)/(s.x-u.x),c.b=s.y-c.k*s.x,c.left=s.x<=u.x?s:u,c.right=c.left===s?u:s,c.top=s.y>=u.y?s:u,c.bottom=c.top===s?u:s,1/0===Math.abs(M.k)&&(M.k=1/0),1/0===Math.abs(c.k)&&(c.k=1/0)}function e(t,x){return t.left.x<=x.x&&x.x<=t.right.x&&t.bottom.y<=x.y&&x.y<=t.top.y}function h(t,x){return{x:t.x*Math.cos(x)-t.y*Math.sin(x),y:t.x*Math.sin(x)+t.y*Math.cos(x)}}var b={x:t,y:x},f={x:y,y:n},s={x:o,y:a},u={x:k,y:r},M={},c={};if(i(),M.k===c.k)return e(M,s)||e(M,u)||e(c,b)||e(c,f);var l=(Math.atan(M.k)+Math.atan(c.k))/2;b=h(b,l),f=h(f,l),s=h(s,l),u=h(u,l),i();var p={};return p.x=(c.b-M.b)/(M.k-c.k),p.y=M.k*p.x+M.b,e(M,p)&&e(c,p)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment