fabric.js/src/intersection.class.js

151 lines
No EOL
4.1 KiB
JavaScript

//= require 'point.class'
(function(global) {
"use strict";
/* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */
var fabric = global.fabric || (global.fabric = { });
if (fabric.Intersection) {
fabric.warn('fabric.Intersection is already defined');
return;
}
/**
* @class Intersection
* @memberOf fabric
*/
function Intersection(status) {
if (arguments.length > 0) {
this.init(status);
}
}
fabric.Intersection = Intersection;
fabric.Intersection.prototype = /** @scope fabric.Intersection.prototype */ {
/**
* @method init
* @param {String} status
*/
init: function (status) {
this.status = status;
this.points = [];
},
/**
* @method appendPoint
* @param {String} status
*/
appendPoint: function (point) {
this.points.push(point);
},
/**
* @method appendPoints
* @param {String} status
*/
appendPoints: function (points) {
this.points = this.points.concat(points);
}
};
/**
* @static
* @method intersectLineLine
*/
fabric.Intersection.intersectLineLine = function (a1, a2, b1, b2) {
var result,
ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x),
ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x),
u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if (u_b != 0) {
var ua = ua_t / u_b,
ub = ub_t / u_b;
if (0 <= ua && ua <= 1 && 0 <= ub && ub <= 1) {
result = new Intersection("Intersection");
result.points.push(new fabric.Point(a1.x + ua * (a2.x - a1.x), a1.y + ua * (a2.y - a1.y)));
}
else {
result = new Intersection("No Intersection");
}
}
else {
if (ua_t == 0 || ub_t == 0) {
result = new Intersection("Coincident");
}
else {
result = new Intersection("Parallel");
}
}
return result;
};
/**
* @method intersectLinePolygon
*/
fabric.Intersection.intersectLinePolygon = function(a1,a2,points){
var result = new Intersection("No Intersection"),
length = points.length;
for (var i = 0; i < length; i++) {
var b1 = points[i],
b2 = points[(i+1) % length],
inter = Intersection.intersectLineLine(a1, a2, b1, b2);
result.appendPoints(inter.points);
}
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
/**
* @method intersectPolygonPolygon
*/
fabric.Intersection.intersectPolygonPolygon = function (points1, points2) {
var result = new Intersection("No Intersection"),
length = points1.length;
for (var i = 0; i < length; i++) {
var a1 = points1[i],
a2 = points1[(i+1) % length],
inter = Intersection.intersectLinePolygon(a1, a2, points2);
result.appendPoints(inter.points);
}
if (result.points.length > 0) {
result.status = "Intersection";
}
return result;
};
/**
* @method intersectPolygonRectangle
*/
fabric.Intersection.intersectPolygonRectangle = function (points, r1, r2) {
var min = r1.min(r2),
max = r1.max(r2),
topRight = new fabric.Point(max.x, min.y),
bottomLeft = new fabric.Point(min.x, max.y),
inter1 = Intersection.intersectLinePolygon(min, topRight, points),
inter2 = Intersection.intersectLinePolygon(topRight, max, points),
inter3 = Intersection.intersectLinePolygon(max, bottomLeft, points),
inter4 = Intersection.intersectLinePolygon(bottomLeft, min, points),
result = new Intersection("No Intersection");
result.appendPoints(inter1.points);
result.appendPoints(inter2.points);
result.appendPoints(inter3.points);
result.appendPoints(inter4.points);
if (result.points.length > 0) {
result.status="Intersection";
}
return result;
};
})(this);