LT-123: Add missing files
-Replace mathUtils.js functions with existed functions in cornerstoneMath.js -Add rectToPoints, doesIntersect, getIntersectionRect methods in cornerstoneMath.rect, -Add intersectLine method in cornerstoneMath.lineSegment
This commit is contained in:
1 parent
3812def9cc
commit
12a326c7fe
6 files changed
+342
-146
No files matched your search
@@ -976,10 +976,95 @@ var cornerstoneMath = (function (cornerstoneMath) {
|
||||
return Math.sqrt(distanceToPointSquared(lineSegment, point));
|
||||
}
|
||||
|
||||
// Returns intersection points of two lines
|
||||
function intersectLine(lineSegment1, lineSegment2) {
|
||||
var intersectionPoint = {};
|
||||
|
||||
var x1 = lineSegment1.start.x, y1 = lineSegment1.start.y, x2 = lineSegment1.end.x, y2 = lineSegment1.end.y,
|
||||
x3 = lineSegment2.start.x, y3 = lineSegment2.start.y, x4 = lineSegment2.end.x, y4 = lineSegment2.end.y;
|
||||
|
||||
var a1, a2, b1, b2, c1, c2; // Coefficients of line equations
|
||||
var r1, r2, r3, r4; // Sign values
|
||||
|
||||
var denom, offset, num; //Intermediate values
|
||||
|
||||
// Compute a1, b1, c1, where line joining points 1 and 2 is "a1 x + b1 y + c1 = 0"
|
||||
a1 = y2 - y1;
|
||||
b1 = x1 - x2;
|
||||
c1 = x2 * y1 - x1 * y2;
|
||||
|
||||
// Compute r3 and r4
|
||||
r3 = a1 * x3 + b1 * y3 + c1;
|
||||
r4 = a1 * x4 + b1 * y4 + c1;
|
||||
|
||||
/* Check signs of r3 and r4. If both point 3 and point 4 lie on
|
||||
* same side of line 1, the line segments do not intersect.
|
||||
*/
|
||||
|
||||
if (r3 !== 0 &&
|
||||
r4 !== 0 &&
|
||||
cornerstoneMath.sign(r3) === cornerstoneMath.sign(r4)) {
|
||||
intersectionPoint.x = 0;
|
||||
intersectionPoint.y = 0;
|
||||
intersectionPoint.intersected = false;
|
||||
return intersectionPoint;
|
||||
}
|
||||
|
||||
/* Compute a2, b2, c2 */
|
||||
|
||||
a2 = y4 - y3;
|
||||
b2 = x3 - x4;
|
||||
c2 = x4 * y3 - x3 * y4;
|
||||
|
||||
/* Compute r1 and r2 */
|
||||
|
||||
r1 = a2 * x1 + b2 * y1 + c2;
|
||||
r2 = a2 * x2 + b2 * y2 + c2;
|
||||
|
||||
/* Check signs of r1 and r2. If both point 1 and point 2 lie
|
||||
* on same side of second line segment, the line segments do
|
||||
* not intersect.
|
||||
*/
|
||||
|
||||
if (r1 !== 0 &&
|
||||
r2 !== 0 &&
|
||||
cornerstoneMath.sign(r1) === cornerstoneMath.sign(r2)) {
|
||||
intersectionPoint.x = 0;
|
||||
intersectionPoint.y = 0;
|
||||
intersectionPoint.intersected = false;
|
||||
return intersectionPoint;
|
||||
}
|
||||
|
||||
/* Line segments intersect: compute intersection point.
|
||||
*/
|
||||
|
||||
denom = (a1 * b2) - (a2 * b1);
|
||||
|
||||
offset = denom < 0 ? -denom / 2 : denom / 2;
|
||||
|
||||
/* The denom/2 is to get rounding instead of truncating. It
|
||||
* is added or subtracted to the numerator, depending upon the
|
||||
* sign of the numerator.
|
||||
*/
|
||||
|
||||
num = (b1 * c2) - (b2 * c1);
|
||||
var x = parseFloat(num / denom);
|
||||
|
||||
num = (a2 * c1) - (a1 * c2);
|
||||
var y = parseFloat(num / denom);
|
||||
|
||||
intersectionPoint.x = x;
|
||||
intersectionPoint.y = y;
|
||||
intersectionPoint.intersected = true;
|
||||
|
||||
return intersectionPoint;
|
||||
}
|
||||
|
||||
// module exports
|
||||
cornerstoneMath.lineSegment =
|
||||
{
|
||||
distanceToPoint : distanceToPoint
|
||||
distanceToPoint : distanceToPoint,
|
||||
intersectLine: intersectLine
|
||||
};
|
||||
|
||||
|
||||
@@ -1012,9 +1097,15 @@ var cornerstoneMath = (function (cornerstoneMath) {
|
||||
return radians * radianToDegreesFactor;
|
||||
}
|
||||
|
||||
// Returns sign of number
|
||||
function sign(x) {
|
||||
return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
|
||||
}
|
||||
|
||||
cornerstoneMath.clamp = clamp;
|
||||
cornerstoneMath.degToRad = degToRad;
|
||||
cornerstoneMath.radToDeg = radToDeg;
|
||||
cornerstoneMath.sign = sign;
|
||||
|
||||
return cornerstoneMath;
|
||||
}(cornerstoneMath));
|
||||
@@ -1754,6 +1845,7 @@ var cornerstoneMath = (function (cornerstoneMath) {
|
||||
|
||||
return (distance < maxDistance);
|
||||
}
|
||||
|
||||
function distanceToPoint(rect, point)
|
||||
{
|
||||
var minDistance = 655535;
|
||||
@@ -1767,10 +1859,109 @@ var cornerstoneMath = (function (cornerstoneMath) {
|
||||
return minDistance;
|
||||
}
|
||||
|
||||
// Returns rectangle points
|
||||
function rectToPoints (rect) {
|
||||
var rectPoints = {
|
||||
top: rect.top,
|
||||
left: rect.left,
|
||||
right: rect.left + rect.width,
|
||||
bottom: rect.top + rect.height
|
||||
};
|
||||
|
||||
return rectPoints;
|
||||
}
|
||||
|
||||
// Returns whether two rectangles are intersected
|
||||
function doesIntersect (rect1, rect2) {
|
||||
var intersectLeftRight;
|
||||
var intersectTopBottom;
|
||||
|
||||
var rect1Points = rectToPoints(rect1);
|
||||
var rect2Points = rectToPoints(rect2);
|
||||
|
||||
if (rect1.width >= 0) {
|
||||
if (rect2.width >= 0)
|
||||
intersectLeftRight = !((rect1Points.right <= rect2Points.left) || (rect2Points.right <= rect1Points.left));
|
||||
else
|
||||
intersectLeftRight = !((rect1Points.right <= rect2Points.right) || (rect2Points.left <= rect1Points.left));
|
||||
} else {
|
||||
if (rect2.width >= 0)
|
||||
intersectLeftRight = !((rect1Points.left <= rect2Points.left) || (rect2Points.right <= rect1Points.right));
|
||||
else
|
||||
intersectLeftRight = !((rect1Points.left <= rect2Points.right) || (rect2Points.left <= rect1Points.right));
|
||||
}
|
||||
|
||||
if (rect1.height >= 0) {
|
||||
if (rect2.height >= 0)
|
||||
intersectTopBottom = !((rect1Points.bottom <= rect2Points.top) || (rect2Points.bottom <= rect1Points.top));
|
||||
else
|
||||
intersectTopBottom = !((rect1Points.bottom <= rect2Points.bottom) || (rect2Points.top <= rect1Points.top));
|
||||
} else {
|
||||
if (rect2.height >= 0)
|
||||
intersectTopBottom = !((rect1Points.top <= rect2Points.top) || (rect2Points.bottom <= rect1Points.bottom));
|
||||
else
|
||||
intersectTopBottom = !((rect1Points.top <= rect2Points.bottom) || (rect2Points.top <= rect1Points.bottom));
|
||||
}
|
||||
|
||||
return intersectLeftRight && intersectTopBottom;
|
||||
}
|
||||
|
||||
// Returns intersection points of two rectangles
|
||||
function getIntersectionRect(rect1, rect2) {
|
||||
var intersectPoints = {};
|
||||
|
||||
if (!doesIntersect(rect1, rect2)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var rect1Points = rectToPoints(rect1);
|
||||
var rect2Points = rectToPoints(rect2);
|
||||
|
||||
if (rect1.width >= 0) {
|
||||
if (rect2.width >= 0) {
|
||||
intersectPoints.left = Math.max(rect1Points.left, rect2Points.left);
|
||||
intersectPoints.right = Math.min(rect1Points.right, rect2Points.right);
|
||||
} else {
|
||||
intersectPoints.left = Math.max(rect1Points.left, rect2Points.right);
|
||||
intersectPoints.right = Math.min(rect1Points.right, rect2Points.left);
|
||||
}
|
||||
} else {
|
||||
if (rect2.width >= 0) {
|
||||
intersectPoints.left = Math.min(rect1Points.left, rect2Points.right);
|
||||
intersectPoints.right = Math.max(rect1Points.right, rect2Points.left);
|
||||
} else {
|
||||
intersectPoints.left = Math.min(rect1Points.left, rect2Points.left);
|
||||
intersectPoints.right = Math.max(rect1Points.right, rect2Points.right);
|
||||
}
|
||||
}
|
||||
|
||||
if (rect1.height >= 0) {
|
||||
if (rect2.height >= 0) {
|
||||
intersectPoints.top = Math.max(rect1Points.top, rect2Points.top);
|
||||
intersectPoints.bottom = Math.min(rect1Points.bottom, rect2Points.bottom);
|
||||
} else {
|
||||
intersectPoints.top = Math.max(rect1Points.top, rect2Points.bottom);
|
||||
intersectPoints.bottom = Math.min(rect1Points.bottom, rect2Points.top);
|
||||
}
|
||||
} else {
|
||||
if (rect2.height >= 0) {
|
||||
intersectPoints.top = Math.min(rect1Points.top, rect2Points.bottom);
|
||||
intersectPoints.bottom = Math.max(rect1Points.bottom, rect2Points.top);
|
||||
} else {
|
||||
intersectPoints.top = Math.min(rect1Points.top, rect2Points.top);
|
||||
intersectPoints.bottom = Math.max(rect1Points.bottom, rect2Points.bottom);
|
||||
}
|
||||
}
|
||||
|
||||
return intersectPoints;
|
||||
|
||||
}
|
||||
|
||||
// module exports
|
||||
cornerstoneMath.rect =
|
||||
{
|
||||
distanceToPoint : distanceToPoint
|
||||
rectToLineSegments : distanceToPoint,
|
||||
getIntersectionRect : getIntersectionRect
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user