cleaned svgedit.math.transformBox(), wrote the according test

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2398 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Bruno Heridet
2013-02-15 19:42:04 +00:00
parent e906a0f6fd
commit 4c4fff6a0d
2 changed files with 89 additions and 67 deletions

View File

@@ -27,7 +27,7 @@ var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
// Function: svgedit.math.transformPoint
// A (hopefully) quicker function to transform a point by a matrix
// (this function avoids any DOM calls and just does the math)
//
//
// Parameters:
// x - Float representing the x coordinate
// y - Float representing the y coordinate
@@ -42,7 +42,7 @@ svgedit.math.transformPoint = function(x, y, m) {
// Helper function to check if the matrix performs no actual transform
// (i.e. exists for identity purposes)
//
// Parameters:
// Parameters:
// m - The matrix object to check
//
// Returns:
@@ -55,11 +55,11 @@ svgedit.math.isIdentity = function(m) {
// Function: svgedit.math.matrixMultiply
// This function tries to return a SVGMatrix that is the multiplication m1*m2.
// We also round to zero when it's near zero
//
//
// Parameters:
// >= 2 Matrix objects to multiply
//
// Returns:
// Returns:
// The matrix object resulting from the calculation
svgedit.math.matrixMultiply = function() {
var args = arguments, i = args.length, m = args[i-1];
@@ -81,10 +81,10 @@ svgedit.math.matrixMultiply = function() {
// Function: svgedit.math.hasMatrixTransform
// See if the given transformlist includes a non-indentity matrix transform
//
// Parameters:
// Parameters:
// tlist - The transformlist to check
//
// Returns:
// Returns:
// Boolean on whether or not a matrix transform was found
svgedit.math.hasMatrixTransform = function(tlist) {
if (!tlist) return false;
@@ -105,7 +105,7 @@ svgedit.math.hasMatrixTransform = function(tlist) {
// w - Float with the box width
// h - Float with the box height
// m - Matrix object to transform the box by
//
//
// Returns:
// An object with the following values:
// * tl - The top left coordinate (x,y object)
@@ -118,34 +118,30 @@ svgedit.math.hasMatrixTransform = function(tlist) {
// * Float with the axis-aligned width coordinate
// * Float with the axis-aligned height coordinate
svgedit.math.transformBox = function(l, t, w, h, m) {
var topleft = {x:l, y:t},
topright = {x:(l+w), y:t},
botright = {x:(l+w), y:(t+h)},
botleft = {x:l, y:(t+h)};
var transformPoint = svgedit.math.transformPoint;
topleft = transformPoint( topleft.x, topleft.y, m );
var minx = topleft.x,
maxx = topleft.x,
miny = topleft.y,
maxy = topleft.y;
topright = transformPoint( topright.x, topright.y, m );
minx = Math.min(minx, topright.x);
maxx = Math.max(maxx, topright.x);
miny = Math.min(miny, topright.y);
maxy = Math.max(maxy, topright.y);
botleft = transformPoint( botleft.x, botleft.y, m);
minx = Math.min(minx, botleft.x);
maxx = Math.max(maxx, botleft.x);
miny = Math.min(miny, botleft.y);
maxy = Math.max(maxy, botleft.y);
botright = transformPoint( botright.x, botright.y, m );
minx = Math.min(minx, botright.x);
maxx = Math.max(maxx, botright.x);
miny = Math.min(miny, botright.y);
maxy = Math.max(maxy, botright.y);
var transformPoint = svgedit.math.transformPoint,
return {tl:topleft, tr:topright, bl:botleft, br:botright,
aabox: {x:minx, y:miny, width:(maxx-minx), height:(maxy-miny)} };
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)
}
};
};
// Function: svgedit.math.transformListToTransform
@@ -153,7 +149,7 @@ svgedit.math.transformBox = function(l, t, w, h, m) {
// (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
//
//
// Parameters:
// tlist - The transformlist object
// min - Optional integer indicating start transform position
@@ -188,7 +184,7 @@ svgedit.math.transformListToTransform = function(tlist, min, max) {
//
// Parameters:
// elem - The DOM element to check
//
//
// Returns:
// The matrix object associated with the element's transformlist
svgedit.math.getMatrix = function(elem) {
@@ -200,14 +196,14 @@ svgedit.math.getMatrix = function(elem) {
// Function: svgedit.math.snapToAngle
// Returns a 45 degree angle coordinate associated with the two given
// coordinates
//
//
// Parameters:
// x1 - First coordinate's x value
// x2 - Second coordinate's x value
// y1 - First coordinate's y value
// y2 - Second coordinate's y value
//
// Returns:
// Returns:
// Object with the following values:
// x - The angle-snapped x value
// y - The angle-snapped y value
@@ -218,11 +214,13 @@ svgedit.math.snapToAngle = function(x1, y1, x2, y2) {
var dy = y2 - y1;
var angle = Math.atan2(dy, dx);
var dist = Math.sqrt(dx * dx + dy * dy);
var snapangle= Math.round(angle/snap)*snap;
var x = x1 + dist*Math.cos(snapangle);
var y = y1 + dist*Math.sin(snapangle);
//console.log(x1,y1,x2,y2,x,y,angle)
return {x:x, y:y, a:snapangle};
var snapangle = Math.round(angle/snap) * snap;
return {
x: x1 + dist * Math.cos(snapangle),
y: y1 + dist * Math.sin(snapangle),
a: snapangle
};
};
@@ -236,7 +234,7 @@ svgedit.math.snapToAngle = function(x1, y1, x2, y2) {
// Returns:
// Boolean that's true if rectangles intersect
svgedit.math.rectsIntersect = function(r1, r2) {
return r2.x < (r1.x+r1.width) &&
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;