Created
August 14, 2013 14:38
-
-
Save k0t0vich/6231683 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
package ru.k0t0vich.utils.math { | |
import flash.geom.Point; | |
public class IntersectUtil { | |
public static function getLinesIntersectionPoint(a1:Point, a2:Point, b1:Point, b2:Point):Point { | |
var d:Number = (a1.x - a2.x) * (b2.y - b1.y) - (a1.y - a2.y) * (b2.x - b1.x); | |
var da:Number = (a1.x - b1.x) * (b2.y - b1.y) - (a1.y - b1.y) * (b2.x - b1.x); | |
var db:Number = (a1.x - a2.x) * (a1.y - b1.y) - (a1.y - a2.y) * (a1.x - b1.x); | |
if (Math.abs(d) < 0.000001) { | |
return null; | |
} else { | |
var ta:Number = da / d; | |
var tb:Number = db / d; | |
if ((0 <= ta) && (ta <= 1) && (0 <= tb) && (tb <= 1)) { | |
var x:Number = a1.x + ta * (a2.x - a1.x); | |
var y:Number = a1.y + ta * (a2.y - a1.y); | |
return new Point(x, y); | |
} | |
} | |
return null; | |
} | |
public static function getPoligonsIntersectionPoints(polygone1:Array, | |
poligone2:Array):Array { | |
var len1:uint = polygone1.length; | |
var len2:uint = poligone2.length; | |
var result:Array = []; | |
for (var i:int = 0; i < len1; i++) { | |
var p1:Point = Point(polygone1[i]); | |
var p2:Point = Point(polygone1[(i + 1) % len1]); | |
for (var j:int = 0; j < len2; j++) { | |
var p3:Point = Point(poligone2[j]); | |
var p4:Point = Point(poligone2[(j + 1) % len2]); | |
var intersection:Point = getLinesIntersectionPoint(p1, p2, p3, p4); | |
if (intersection) { | |
result.push(intersection); | |
} | |
} | |
} | |
return result.length > 0 ? result : null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment