2018-05-16 08:32:44 +00:00
|
|
|
/* eslint-disable no-var */
|
2018-05-16 00:53:27 +00:00
|
|
|
/* globals svgedit */
|
2010-10-28 16:47:39 +00:00
|
|
|
/**
|
2010-11-05 17:00:32 +00:00
|
|
|
* Package: svedit.math
|
2010-10-28 16:47:39 +00:00
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-10-28 16:47:39 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies:
|
|
|
|
// None.
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* @typedef AngleCoord45
|
2018-05-17 18:57:28 +00:00
|
|
|
* @type {Object}
|
2014-03-15 08:53:45 +00:00
|
|
|
* @property {number} x - The angle-snapped x value
|
|
|
|
* @property {number} y - The angle-snapped y value
|
|
|
|
* @property {number} a - The angle at which to snap
|
|
|
|
*/
|
2010-10-28 16:47:39 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2010-10-28 16:47:39 +00:00
|
|
|
|
|
|
|
if (!svgedit.math) {
|
|
|
|
svgedit.math = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
var NEAR_ZERO = 1e-14;
|
|
|
|
|
|
|
|
// Throw away SVGSVGElement used for creating matrices/transforms.
|
2013-02-16 15:02:26 +00:00
|
|
|
var svg = document.createElementNS(svgedit.NS.SVG, 'svg');
|
2010-10-28 16:47:39 +00:00
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* A (hopefully) quicker function to transform a point by a matrix
|
|
|
|
* (this function avoids any DOM calls and just does the math)
|
|
|
|
* @param {number} x - Float representing the x coordinate
|
|
|
|
* @param {number} y - Float representing the y coordinate
|
|
|
|
* @param {SVGMatrix} m - Matrix object to transform the point with
|
2018-05-17 18:57:28 +00:00
|
|
|
* @returns {Object} An x, y object representing the transformed point
|
2014-03-15 08:53:45 +00:00
|
|
|
*/
|
|
|
|
svgedit.math.transformPoint = function (x, y, m) {
|
2018-05-16 00:53:27 +00:00
|
|
|
return {x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f};
|
2010-10-28 16:47:39 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
2018-05-16 00:53:27 +00:00
|
|
|
* Helper function to check if the matrix performs no actual transform
|
2014-03-15 08:53:45 +00:00
|
|
|
* (i.e. exists for identity purposes)
|
|
|
|
* @param {SVGMatrix} m - The matrix object to check
|
|
|
|
* @returns {boolean} Indicates whether or not the matrix is 1,0,0,1,0,0
|
|
|
|
*/
|
|
|
|
svgedit.math.isIdentity = function (m) {
|
2010-10-28 16:47:39 +00:00
|
|
|
return (m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.e === 0 && m.f === 0);
|
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* This function tries to return a SVGMatrix that is the multiplication m1*m2.
|
|
|
|
* We also round to zero when it's near zero
|
|
|
|
* @param {...SVGMatrix} matr - Two or more matrix objects to multiply
|
|
|
|
* @returns {SVGMatrix} The matrix object resulting from the calculation
|
|
|
|
*/
|
|
|
|
svgedit.math.matrixMultiply = function (matr) {
|
2018-05-16 00:53:27 +00:00
|
|
|
var args = arguments, i = args.length, m = args[i - 1];
|
2013-02-14 13:29:02 +00:00
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
while (i-- > 1) {
|
2018-05-16 00:53:27 +00:00
|
|
|
var m1 = args[i - 1];
|
2010-10-28 16:47:39 +00:00
|
|
|
m = m1.multiply(m);
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
if (Math.abs(m.a) < NEAR_ZERO) { m.a = 0; }
|
|
|
|
if (Math.abs(m.b) < NEAR_ZERO) { m.b = 0; }
|
|
|
|
if (Math.abs(m.c) < NEAR_ZERO) { m.c = 0; }
|
|
|
|
if (Math.abs(m.d) < NEAR_ZERO) { m.d = 0; }
|
|
|
|
if (Math.abs(m.e) < NEAR_ZERO) { m.e = 0; }
|
|
|
|
if (Math.abs(m.f) < NEAR_ZERO) { m.f = 0; }
|
2013-02-14 13:29:02 +00:00
|
|
|
|
2010-10-28 16:47:39 +00:00
|
|
|
return m;
|
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* See if the given transformlist includes a non-indentity matrix transform
|
2018-05-17 18:57:28 +00:00
|
|
|
* @param {Object} [tlist] - The transformlist to check
|
2014-03-15 08:53:45 +00:00
|
|
|
* @returns {boolean} Whether or not a matrix transform was found
|
|
|
|
*/
|
|
|
|
svgedit.math.hasMatrixTransform = function (tlist) {
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!tlist) { return false; }
|
2010-10-28 16:47:39 +00:00
|
|
|
var num = tlist.numberOfItems;
|
|
|
|
while (num--) {
|
|
|
|
var xform = tlist.getItem(num);
|
2018-05-16 08:32:44 +00:00
|
|
|
if (xform.type === 1 && !svgedit.math.isIdentity(xform.matrix)) { return true; }
|
2010-10-28 16:47:39 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* Transforms a rectangle based on the given matrix
|
|
|
|
* @param {number} l - Float with the box's left coordinate
|
|
|
|
* @param {number} t - Float with the box's top coordinate
|
|
|
|
* @param {number} w - Float with the box width
|
|
|
|
* @param {number} h - Float with the box height
|
|
|
|
* @param {SVGMatrix} m - Matrix object to transform the box by
|
2018-05-17 18:57:28 +00:00
|
|
|
* @returns {Object} An object with the following values:
|
2014-03-15 08:53:45 +00:00
|
|
|
* tl - The top left coordinate (x,y object)
|
|
|
|
* tr - The top right coordinate (x,y object)
|
|
|
|
* bl - The bottom left coordinate (x,y object)
|
|
|
|
* br - The bottom right coordinate (x,y object)
|
|
|
|
* aabox - Object with the following values:
|
|
|
|
* x - Float with the axis-aligned x coordinate
|
|
|
|
* y - Float with the axis-aligned y coordinate
|
|
|
|
* width - Float with the axis-aligned width coordinate
|
|
|
|
* height - Float with the axis-aligned height coordinate
|
|
|
|
*/
|
|
|
|
svgedit.math.transformBox = function (l, t, w, h, m) {
|
2013-02-15 19:42:04 +00:00
|
|
|
var transformPoint = svgedit.math.transformPoint,
|
|
|
|
|
|
|
|
tl = transformPoint(l, t, m),
|
|
|
|
tr = transformPoint((l + w), t, m),
|
|
|
|
bl = transformPoint(l, (t + h), m),
|
|
|
|
br = transformPoint((l + w), (t + h), m),
|
|
|
|
|
|
|
|
minx = Math.min(tl.x, tr.x, bl.x, br.x),
|
|
|
|
maxx = Math.max(tl.x, tr.x, bl.x, br.x),
|
|
|
|
miny = Math.min(tl.y, tr.y, bl.y, br.y),
|
|
|
|
maxy = Math.max(tl.y, tr.y, bl.y, br.y);
|
|
|
|
|
|
|
|
return {
|
|
|
|
tl: tl,
|
|
|
|
tr: tr,
|
|
|
|
bl: bl,
|
|
|
|
br: br,
|
|
|
|
aabox: {
|
|
|
|
x: minx,
|
|
|
|
y: miny,
|
|
|
|
width: (maxx - minx),
|
|
|
|
height: (maxy - miny)
|
|
|
|
}
|
|
|
|
};
|
2010-10-28 16:47:39 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* This returns a single matrix Transform for a given Transform List
|
|
|
|
* (this is the equivalent of SVGTransformList.consolidate() but unlike
|
|
|
|
* that method, this one does not modify the actual SVGTransformList)
|
|
|
|
* This function is very liberal with its min, max arguments
|
2018-05-17 18:57:28 +00:00
|
|
|
* @param {Object} tlist - The transformlist object
|
2014-03-15 08:53:45 +00:00
|
|
|
* @param {integer} [min=0] - Optional integer indicating start transform position
|
|
|
|
* @param {integer} [max] - Optional integer indicating end transform position;
|
|
|
|
* defaults to one less than the tlist's numberOfItems
|
2018-05-17 18:57:28 +00:00
|
|
|
* @returns {Object} A single matrix transform object
|
2014-03-15 08:53:45 +00:00
|
|
|
*/
|
|
|
|
svgedit.math.transformListToTransform = function (tlist, min, max) {
|
2013-02-15 16:51:48 +00:00
|
|
|
if (tlist == null) {
|
2010-10-28 16:47:39 +00:00
|
|
|
// Or should tlist = null have been prevented before this?
|
|
|
|
return svg.createSVGTransformFromMatrix(svg.createSVGMatrix());
|
|
|
|
}
|
2013-02-14 13:29:02 +00:00
|
|
|
min = min || 0;
|
|
|
|
max = max || (tlist.numberOfItems - 1);
|
|
|
|
min = parseInt(min, 10);
|
|
|
|
max = parseInt(max, 10);
|
2010-10-28 16:47:39 +00:00
|
|
|
if (min > max) { var temp = max; max = min; min = temp; }
|
|
|
|
var m = svg.createSVGMatrix();
|
2014-01-31 02:13:37 +00:00
|
|
|
var i;
|
|
|
|
for (i = min; i <= max; ++i) {
|
2010-10-28 16:47:39 +00:00
|
|
|
// if our indices are out of range, just use a harmless identity matrix
|
2018-05-16 00:53:27 +00:00
|
|
|
var mtom = (i >= 0 && i < tlist.numberOfItems
|
|
|
|
? tlist.getItem(i).matrix
|
|
|
|
: svg.createSVGMatrix());
|
2010-10-28 16:47:39 +00:00
|
|
|
m = svgedit.math.matrixMultiply(m, mtom);
|
|
|
|
}
|
|
|
|
return svg.createSVGTransformFromMatrix(m);
|
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* Get the matrix object for a given element
|
|
|
|
* @param {Element} elem - The DOM element to check
|
|
|
|
* @returns {SVGMatrix} The matrix object associated with the element's transformlist
|
|
|
|
*/
|
|
|
|
svgedit.math.getMatrix = function (elem) {
|
2011-02-09 06:14:47 +00:00
|
|
|
var tlist = svgedit.transformlist.getTransformList(elem);
|
|
|
|
return svgedit.math.transformListToTransform(tlist).matrix;
|
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* Returns a 45 degree angle coordinate associated with the two given
|
|
|
|
* coordinates
|
|
|
|
* @param {number} x1 - First coordinate's x value
|
|
|
|
* @param {number} x2 - Second coordinate's x value
|
|
|
|
* @param {number} y1 - First coordinate's y value
|
|
|
|
* @param {number} y2 - Second coordinate's y value
|
|
|
|
* @returns {AngleCoord45}
|
|
|
|
*/
|
|
|
|
svgedit.math.snapToAngle = function (x1, y1, x2, y2) {
|
|
|
|
var snap = Math.PI / 4; // 45 degrees
|
2010-10-29 05:09:39 +00:00
|
|
|
var dx = x2 - x1;
|
|
|
|
var dy = y2 - y1;
|
2013-02-15 16:51:48 +00:00
|
|
|
var angle = Math.atan2(dy, dx);
|
2010-10-29 05:09:39 +00:00
|
|
|
var dist = Math.sqrt(dx * dx + dy * dy);
|
2014-03-15 08:53:45 +00:00
|
|
|
var snapangle = Math.round(angle / snap) * snap;
|
2013-02-15 19:42:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
x: x1 + dist * Math.cos(snapangle),
|
|
|
|
y: y1 + dist * Math.sin(snapangle),
|
|
|
|
a: snapangle
|
|
|
|
};
|
2010-10-29 05:09:39 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 08:53:45 +00:00
|
|
|
/**
|
|
|
|
* Check if two rectangles (BBoxes objects) intersect each other
|
|
|
|
* @param {SVGRect} r1 - The first BBox-like object
|
|
|
|
* @param {SVGRect} r2 - The second BBox-like object
|
|
|
|
* @returns {boolean} True if rectangles intersect
|
|
|
|
*/
|
|
|
|
svgedit.math.rectsIntersect = function (r1, r2) {
|
|
|
|
return r2.x < (r1.x + r1.width) &&
|
|
|
|
(r2.x + r2.width) > r1.x &&
|
|
|
|
r2.y < (r1.y + r1.height) &&
|
|
|
|
(r2.y + r2.height) > r1.y;
|
2010-11-08 08:42:46 +00:00
|
|
|
};
|
2014-03-15 08:53:45 +00:00
|
|
|
}());
|