overlap-area
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -7,3 +7,3 @@ /* | ||
repository: git+https://github.com/daybrush/overlap-area.git | ||
version: 0.3.0 | ||
version: 0.3.1 | ||
*/ | ||
@@ -336,4 +336,15 @@ 'use strict'; | ||
function getOverlapPoints(points1, points2) { | ||
var lines1 = convertLines(points1); | ||
var lines2 = convertLines(points2); | ||
var targetPoints1 = points1.slice(); | ||
var targetPoints2 = points2.slice(); | ||
if (utils.getShapeDirection(targetPoints1) === -1) { | ||
targetPoints1.reverse(); | ||
} | ||
if (utils.getShapeDirection(targetPoints2) === -1) { | ||
targetPoints2.reverse(); | ||
} | ||
var lines1 = convertLines(targetPoints1); | ||
var lines2 = convertLines(targetPoints2); | ||
var linearConstantss1 = lines1.map(function (line1) { | ||
@@ -345,28 +356,89 @@ return getLinearConstants(line1[0], line1[1]); | ||
}); | ||
var overlappingPoints = []; | ||
var points2Direction = utils.getShapeDirection(points2); | ||
lines2.forEach(function (line2) { | ||
if (isInside(line2[1], points1)) { | ||
overlappingPoints[points2Direction > 0 ? "push" : "unshift"](line2[1]); | ||
} | ||
}); | ||
var overlapInfos = []; | ||
linearConstantss1.forEach(function (linearConstants1, i) { | ||
var line1 = lines1[i]; | ||
var linePoints = []; | ||
var linePointInfos = []; | ||
linearConstantss2.forEach(function (linearConstants2, j) { | ||
var intersectionPoints = getIntersectionPointsByConstants(linearConstants1, linearConstants2); | ||
var points = getPointsOnLines(intersectionPoints, [line1, lines2[j]]); | ||
linePoints.push.apply(linePoints, points); | ||
linePointInfos.push.apply(linePointInfos, points.map(function (pos) { | ||
return { | ||
index1: i, | ||
index2: j, | ||
pos: pos | ||
}; | ||
})); | ||
}); | ||
linePoints.sort(function (a, b) { | ||
return utils.getDist(line1[0], a) - utils.getDist(line1[0], b); | ||
linePointInfos.sort(function (a, b) { | ||
return utils.getDist(line1[0], a.pos) - utils.getDist(line1[0], b.pos); | ||
}); | ||
overlappingPoints.push.apply(overlappingPoints, linePoints); | ||
overlapInfos.push.apply(overlapInfos, linePointInfos); | ||
if (isInside(line1[1], points2)) { | ||
overlappingPoints.push(line1[1]); | ||
if (isInside(line1[1], targetPoints2)) { | ||
overlapInfos.push({ | ||
index1: i, | ||
index2: -1, | ||
pos: line1[1] | ||
}); | ||
} | ||
}); | ||
lines2.forEach(function (line2, i) { | ||
if (isInside(line2[1], targetPoints1)) { | ||
var isNext_1 = false; | ||
var index = utils.findIndex(overlapInfos, function (_a) { | ||
var index2 = _a.index2; | ||
if (index2 === i) { | ||
isNext_1 = true; | ||
return false; | ||
} | ||
if (isNext_1) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (index === -1) { | ||
isNext_1 = false; | ||
index = utils.findIndex(overlapInfos, function (_a) { | ||
var index1 = _a.index1, | ||
index2 = _a.index2; | ||
if (index1 === -1 && index2 + 1 === i) { | ||
isNext_1 = true; | ||
return false; | ||
} | ||
if (isNext_1) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (index === -1) { | ||
overlapInfos.push({ | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1] | ||
}); | ||
} else { | ||
overlapInfos.splice(index, 0, { | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1] | ||
}); | ||
} | ||
} | ||
}); // console.log(overlapInfos); | ||
var overlapPoints = overlapInfos.map(function (_a) { | ||
var pos = _a.pos; | ||
return pos; | ||
}); | ||
var pointMap = {}; | ||
return overlappingPoints.filter(function (point) { | ||
return overlapPoints.filter(function (point) { | ||
var key = point[0] + "x" + point[1]; | ||
@@ -373,0 +445,0 @@ |
@@ -7,3 +7,3 @@ /* | ||
repository: git+https://github.com/daybrush/overlap-area.git | ||
version: 0.3.0 | ||
version: 0.3.1 | ||
*/ | ||
@@ -334,4 +334,15 @@ import { sum, findIndex, getShapeDirection, getDist } from '@daybrush/utils'; | ||
function getOverlapPoints(points1, points2) { | ||
var lines1 = convertLines(points1); | ||
var lines2 = convertLines(points2); | ||
var targetPoints1 = points1.slice(); | ||
var targetPoints2 = points2.slice(); | ||
if (getShapeDirection(targetPoints1) === -1) { | ||
targetPoints1.reverse(); | ||
} | ||
if (getShapeDirection(targetPoints2) === -1) { | ||
targetPoints2.reverse(); | ||
} | ||
var lines1 = convertLines(targetPoints1); | ||
var lines2 = convertLines(targetPoints2); | ||
var linearConstantss1 = lines1.map(function (line1) { | ||
@@ -343,28 +354,89 @@ return getLinearConstants(line1[0], line1[1]); | ||
}); | ||
var overlappingPoints = []; | ||
var points2Direction = getShapeDirection(points2); | ||
lines2.forEach(function (line2) { | ||
if (isInside(line2[1], points1)) { | ||
overlappingPoints[points2Direction > 0 ? "push" : "unshift"](line2[1]); | ||
} | ||
}); | ||
var overlapInfos = []; | ||
linearConstantss1.forEach(function (linearConstants1, i) { | ||
var line1 = lines1[i]; | ||
var linePoints = []; | ||
var linePointInfos = []; | ||
linearConstantss2.forEach(function (linearConstants2, j) { | ||
var intersectionPoints = getIntersectionPointsByConstants(linearConstants1, linearConstants2); | ||
var points = getPointsOnLines(intersectionPoints, [line1, lines2[j]]); | ||
linePoints.push.apply(linePoints, points); | ||
linePointInfos.push.apply(linePointInfos, points.map(function (pos) { | ||
return { | ||
index1: i, | ||
index2: j, | ||
pos: pos | ||
}; | ||
})); | ||
}); | ||
linePoints.sort(function (a, b) { | ||
return getDist(line1[0], a) - getDist(line1[0], b); | ||
linePointInfos.sort(function (a, b) { | ||
return getDist(line1[0], a.pos) - getDist(line1[0], b.pos); | ||
}); | ||
overlappingPoints.push.apply(overlappingPoints, linePoints); | ||
overlapInfos.push.apply(overlapInfos, linePointInfos); | ||
if (isInside(line1[1], points2)) { | ||
overlappingPoints.push(line1[1]); | ||
if (isInside(line1[1], targetPoints2)) { | ||
overlapInfos.push({ | ||
index1: i, | ||
index2: -1, | ||
pos: line1[1] | ||
}); | ||
} | ||
}); | ||
lines2.forEach(function (line2, i) { | ||
if (isInside(line2[1], targetPoints1)) { | ||
var isNext_1 = false; | ||
var index = findIndex(overlapInfos, function (_a) { | ||
var index2 = _a.index2; | ||
if (index2 === i) { | ||
isNext_1 = true; | ||
return false; | ||
} | ||
if (isNext_1) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (index === -1) { | ||
isNext_1 = false; | ||
index = findIndex(overlapInfos, function (_a) { | ||
var index1 = _a.index1, | ||
index2 = _a.index2; | ||
if (index1 === -1 && index2 + 1 === i) { | ||
isNext_1 = true; | ||
return false; | ||
} | ||
if (isNext_1) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (index === -1) { | ||
overlapInfos.push({ | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1] | ||
}); | ||
} else { | ||
overlapInfos.splice(index, 0, { | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1] | ||
}); | ||
} | ||
} | ||
}); // console.log(overlapInfos); | ||
var overlapPoints = overlapInfos.map(function (_a) { | ||
var pos = _a.pos; | ||
return pos; | ||
}); | ||
var pointMap = {}; | ||
return overlappingPoints.filter(function (point) { | ||
return overlapPoints.filter(function (point) { | ||
var key = point[0] + "x" + point[1]; | ||
@@ -371,0 +443,0 @@ |
@@ -7,3 +7,3 @@ /* | ||
repository: git+https://github.com/daybrush/overlap-area.git | ||
version: 0.3.0 | ||
version: 0.3.1 | ||
*/ | ||
@@ -453,4 +453,15 @@ (function (global, factory) { | ||
function getOverlapPoints(points1, points2) { | ||
var lines1 = convertLines(points1); | ||
var lines2 = convertLines(points2); | ||
var targetPoints1 = points1.slice(); | ||
var targetPoints2 = points2.slice(); | ||
if (getShapeDirection(targetPoints1) === -1) { | ||
targetPoints1.reverse(); | ||
} | ||
if (getShapeDirection(targetPoints2) === -1) { | ||
targetPoints2.reverse(); | ||
} | ||
var lines1 = convertLines(targetPoints1); | ||
var lines2 = convertLines(targetPoints2); | ||
var linearConstantss1 = lines1.map(function (line1) { | ||
@@ -462,28 +473,89 @@ return getLinearConstants(line1[0], line1[1]); | ||
}); | ||
var overlappingPoints = []; | ||
var points2Direction = getShapeDirection(points2); | ||
lines2.forEach(function (line2) { | ||
if (isInside(line2[1], points1)) { | ||
overlappingPoints[points2Direction > 0 ? "push" : "unshift"](line2[1]); | ||
} | ||
}); | ||
var overlapInfos = []; | ||
linearConstantss1.forEach(function (linearConstants1, i) { | ||
var line1 = lines1[i]; | ||
var linePoints = []; | ||
var linePointInfos = []; | ||
linearConstantss2.forEach(function (linearConstants2, j) { | ||
var intersectionPoints = getIntersectionPointsByConstants(linearConstants1, linearConstants2); | ||
var points = getPointsOnLines(intersectionPoints, [line1, lines2[j]]); | ||
linePoints.push.apply(linePoints, points); | ||
linePointInfos.push.apply(linePointInfos, points.map(function (pos) { | ||
return { | ||
index1: i, | ||
index2: j, | ||
pos: pos | ||
}; | ||
})); | ||
}); | ||
linePoints.sort(function (a, b) { | ||
return getDist(line1[0], a) - getDist(line1[0], b); | ||
linePointInfos.sort(function (a, b) { | ||
return getDist(line1[0], a.pos) - getDist(line1[0], b.pos); | ||
}); | ||
overlappingPoints.push.apply(overlappingPoints, linePoints); | ||
overlapInfos.push.apply(overlapInfos, linePointInfos); | ||
if (isInside(line1[1], points2)) { | ||
overlappingPoints.push(line1[1]); | ||
if (isInside(line1[1], targetPoints2)) { | ||
overlapInfos.push({ | ||
index1: i, | ||
index2: -1, | ||
pos: line1[1] | ||
}); | ||
} | ||
}); | ||
lines2.forEach(function (line2, i) { | ||
if (isInside(line2[1], targetPoints1)) { | ||
var isNext_1 = false; | ||
var index = findIndex(overlapInfos, function (_a) { | ||
var index2 = _a.index2; | ||
if (index2 === i) { | ||
isNext_1 = true; | ||
return false; | ||
} | ||
if (isNext_1) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (index === -1) { | ||
isNext_1 = false; | ||
index = findIndex(overlapInfos, function (_a) { | ||
var index1 = _a.index1, | ||
index2 = _a.index2; | ||
if (index1 === -1 && index2 + 1 === i) { | ||
isNext_1 = true; | ||
return false; | ||
} | ||
if (isNext_1) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (index === -1) { | ||
overlapInfos.push({ | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1] | ||
}); | ||
} else { | ||
overlapInfos.splice(index, 0, { | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1] | ||
}); | ||
} | ||
} | ||
}); // console.log(overlapInfos); | ||
var overlapPoints = overlapInfos.map(function (_a) { | ||
var pos = _a.pos; | ||
return pos; | ||
}); | ||
var pointMap = {}; | ||
return overlappingPoints.filter(function (point) { | ||
return overlapPoints.filter(function (point) { | ||
var key = point[0] + "x" + point[1]; | ||
@@ -490,0 +562,0 @@ |
@@ -7,5 +7,5 @@ /* | ||
repository: git+https://github.com/daybrush/overlap-area.git | ||
version: 0.3.0 | ||
version: 0.3.1 | ||
*/ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n=n||self).OverlapArea=t()}(this,function(){"use strict";function s(n,t,r){void 0===r&&(r=-1);for(var e=n.length,u=0;u<e;++u)if(t(n[u],u,n))return u;return r}function l(n,t){var r=t[0]-n[0],n=t[1]-n[1],r=Math.atan2(n,r);return 0<=r?r:r+2*Math.PI}function m(n){return[0,1].map(function(t){return function(n){for(var t=n.length,r=0,e=t-1;0<=e;--e)r+=n[e];return t?r/t:0}(n.map(function(n){return n[t]}))})}function M(n,t){return Math.sqrt(Math.pow((t?t[0]:0)-n[0],2)+Math.pow((t?t[1]:0)-n[1],2))}function r(r){return Math.abs(function(n){for(var t=0,r=n.length-1;0<=r;--r)t+=n[r];return t}(r.map(function(n,t){t=r[t+1]||r[0];return n[0]*t[1]-t[0]*n[1]})))/2}function v(n){var t=n.map(function(n){return n[0]}),n=n.map(function(n){return n[1]});return{minX:Math.min.apply(Math,t),minY:Math.min.apply(Math,n),maxX:Math.max.apply(Math,t),maxY:Math.max.apply(Math,n)}}function y(n,t,r){var u=n[0],a=n[1],e=v(t),i=e.minX,f=e.minY,n=e.maxX,e=e.maxY,o=[[i,a],[n,a]],p=[[u,f],[u,e]],c=g(o[0],o[1]),h=g(p[0],p[1]),t=P(t),l=[],m=[];return t.forEach(function(n){var t=g(n[0],n[1]),r=d(x(c,t),[o,n]),e=d(x(h,t),[p,n]);1===r.length&&n[0][1]===a||l.push.apply(l,r),1===e.length&&n[0][0]===u||m.push.apply(m,e),t[0]||l.push.apply(l,r),t[1]||m.push.apply(m,e)}),!r&&(-1<s(l,function(n){return n[0]===u})||-1<s(m,function(n){return n[1]===a}))||!!(l.filter(function(n){return n[0]>u}).length%2&&m.filter(function(n){return n[1]>a}).length%2)}function g(n,t){var r=n[0],e=n[1],n=t[0],t=t[1];if(r===n&&e===t)return[0,0,0];if(r===n)return[1,0,-r];if(e===t)return[0,1,-e];t=(n-r)/(e-t);return[1,t,-r-t*e]}function x(n,t){var r,e,u=n[0],a=n[1],i=n[2],f=t[0],o=t[1],p=t[2],n=0===u&&0===f,t=0===a&&0===o;if(n&&t)return[];if(n){n=-i/a;return n!=-p/o?[]:[[-1/0,n],[1/0,n]]}if(t){t=-i/u;return t!=-p/f?[]:[[t,-1/0],[t,1/0]]}return 0!==u?0!==f?0!==a?0!==o?[[r=(a*p-o*i)/(o*u-a*f),e=-(u*r+i)/a]]:[[r=-p/f,e=-(u*r+i)/a]]:[[r=-i/u,e=-(f*r+p)/o]]:[[r=-(a*(e=-p/o)+i)/u,e]]:[[r=-(o*(e=-i/a)+p)/f,e]]}function d(n,t){var r=t.map(function(t){return[0,1].map(function(n){return[Math.min(t[0][n],t[1][n]),Math.max(t[0][n],t[1][n])]})});if(2===n.length){var e=n[0],u=e[0],t=e[1];if(u===n[1][0]){var e=Math.max.apply(Math,r.map(function(n){return n[1][0]})),a=Math.min.apply(Math,r.map(function(n){return n[1][1]}));return a<e?[]:[[u,e],[u,a]]}if(t===n[1][1]){u=Math.max.apply(Math,r.map(function(n){return n[0][0]})),a=Math.min.apply(Math,r.map(function(n){return n[0][1]}));return a<u?[]:[[u,t],[a,t]]}}return n.filter(function(t){return r.every(function(n){return n[0][0]<=t[0]&&t[0]<=n[0][1]&&n[1][0]<=t[1]&&t[1]<=n[1][1]})})}function P(r){return function(){for(var n=0,t=0,r=arguments.length;t<r;t++)n+=arguments[t].length;for(var e=Array(n),u=0,t=0;t<r;t++)for(var a=arguments[t],i=0,f=a.length;i<f;i++,u++)e[u]=a[i];return e}(r.slice(1),[r[0]]).map(function(n,t){return[r[t],n]})}function e(t,a){var n,r,e,i=P(t),f=P(a),u=i.map(function(n){return g(n[0],n[1])}),o=f.map(function(n){return g(n[0],n[1])}),p=[],c=(r=m(n=a),e=l(r,n[0]),n=l(r,n[1]),e<n&&n-e<Math.PI||n<e&&n-e<-Math.PI?1:-1);f.forEach(function(n){y(n[1],t)&&p[0<c?"push":"unshift"](n[1])}),u.forEach(function(r,n){var e=i[n],u=[];o.forEach(function(n,t){t=d(x(r,n),[e,f[t]]);u.push.apply(u,t)}),u.sort(function(n,t){return M(e[0],n)-M(e[0],t)}),p.push.apply(p,u),y(e[1],a)&&p.push(e[1])});var h={};return p.filter(function(n){n=n[0]+"x"+n[1];return!h[n]&&(h[n]=!0)})}return{__proto__:null,getAreaSize:r,getMinMaxs:v,isInside:y,getLinearConstants:g,getIntersectionPointsByConstants:x,getIntersectionPoints:function(n,t,r){var e=x(g(n[0],n[1]),g(t[0],t[1]));return r?d(e,[n,t]):e},getPointsOnLines:d,convertLines:P,getOverlapPoints:e,getOverlapSize:function(n,t){return r(e(n,t))}}}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n=n||self).OverlapArea=t()}(this,function(){"use strict";function m(n,t,r){void 0===r&&(r=-1);for(var e=n.length,u=0;u<e;++u)if(t(n[u],u,n))return u;return r}function e(n,t){var r=t[0]-n[0],n=t[1]-n[1],r=Math.atan2(n,r);return 0<=r?r:r+2*Math.PI}function u(n){return[0,1].map(function(t){return function(n){for(var t=n.length,r=0,e=t-1;0<=e;--e)r+=n[e];return t?r/t:0}(n.map(function(n){return n[t]}))})}function i(n){var t=u(n),r=e(t,n[0]),n=e(t,n[1]);return r<n&&n-r<Math.PI||n<r&&n-r<-Math.PI?1:-1}function h(n,t){return Math.sqrt(Math.pow((t?t[0]:0)-n[0],2)+Math.pow((t?t[1]:0)-n[1],2))}function r(r){return Math.abs(function(n){for(var t=0,r=n.length-1;0<=r;--r)t+=n[r];return t}(r.map(function(n,t){t=r[t+1]||r[0];return n[0]*t[1]-t[0]*n[1]})))/2}function v(n){var t=n.map(function(n){return n[0]}),n=n.map(function(n){return n[1]});return{minX:Math.min.apply(Math,t),minY:Math.min.apply(Math,n),maxX:Math.max.apply(Math,t),maxY:Math.max.apply(Math,n)}}function s(n,t,r){var u=n[0],i=n[1],e=v(t),a=e.minX,o=e.minY,n=e.maxX,e=e.maxY,f=[[a,i],[n,i]],p=[[u,o],[u,e]],c=M(f[0],f[1]),h=M(p[0],p[1]),t=y(t),s=[],l=[];return t.forEach(function(n){var t=M(n[0],n[1]),r=d(x(c,t),[f,n]),e=d(x(h,t),[p,n]);1===r.length&&n[0][1]===i||s.push.apply(s,r),1===e.length&&n[0][0]===u||l.push.apply(l,e),t[0]||s.push.apply(s,r),t[1]||l.push.apply(l,e)}),!r&&(-1<m(s,function(n){return n[0]===u})||-1<m(l,function(n){return n[1]===i}))||!!(s.filter(function(n){return n[0]>u}).length%2&&l.filter(function(n){return n[1]>i}).length%2)}function M(n,t){var r=n[0],e=n[1],n=t[0],t=t[1];if(r===n&&e===t)return[0,0,0];if(r===n)return[1,0,-r];if(e===t)return[0,1,-e];t=(n-r)/(e-t);return[1,t,-r-t*e]}function x(n,t){var r,e,u=n[0],i=n[1],a=n[2],o=t[0],f=t[1],p=t[2],n=0===u&&0===o,t=0===i&&0===f;if(n&&t)return[];if(n){n=-a/i;return n!=-p/f?[]:[[-1/0,n],[1/0,n]]}if(t){t=-a/u;return t!=-p/o?[]:[[t,-1/0],[t,1/0]]}return 0!==u?0!==o?0!==i?0!==f?[[r=(i*p-f*a)/(f*u-i*o),e=-(u*r+a)/i]]:[[r=-p/o,e=-(u*r+a)/i]]:[[r=-a/u,e=-(o*r+p)/f]]:[[r=-(i*(e=-p/f)+a)/u,e]]:[[r=-(f*(e=-a/i)+p)/o,e]]}function d(n,t){var r=t.map(function(t){return[0,1].map(function(n){return[Math.min(t[0][n],t[1][n]),Math.max(t[0][n],t[1][n])]})});if(2===n.length){var e=n[0],u=e[0],t=e[1];if(u===n[1][0]){var e=Math.max.apply(Math,r.map(function(n){return n[1][0]})),i=Math.min.apply(Math,r.map(function(n){return n[1][1]}));return i<e?[]:[[u,e],[u,i]]}if(t===n[1][1]){u=Math.max.apply(Math,r.map(function(n){return n[0][0]})),i=Math.min.apply(Math,r.map(function(n){return n[0][1]}));return i<u?[]:[[u,t],[i,t]]}}return n.filter(function(t){return r.every(function(n){return n[0][0]<=t[0]&&t[0]<=n[0][1]&&n[1][0]<=t[1]&&t[1]<=n[1][1]})})}function y(r){return function(){for(var n=0,t=0,r=arguments.length;t<r;t++)n+=arguments[t].length;for(var e=Array(n),u=0,t=0;t<r;t++)for(var i=arguments[t],a=0,o=i.length;a<o;a++,u++)e[u]=i[a];return e}(r.slice(1),[r[0]]).map(function(n,t){return[r[t],n]})}function a(n,t){var u=n.slice(),a=t.slice();-1===i(u)&&u.reverse(),-1===i(a)&&a.reverse();var o=y(u),f=y(a),t=o.map(function(n){return M(n[0],n[1])}),p=f.map(function(n){return M(n[0],n[1])}),c=[];t.forEach(function(r,e){var u=o[e],i=[];p.forEach(function(n,t){n=d(x(r,n),[u,f[t]]);i.push.apply(i,n.map(function(n){return{index1:e,index2:t,pos:n}}))}),i.sort(function(n,t){return h(u[0],n.pos)-h(u[0],t.pos)}),c.push.apply(c,i),s(u[1],a)&&c.push({index1:e,index2:-1,pos:u[1]})}),f.forEach(function(n,r){var e,t;s(n[1],u)&&(e=!1,-1===(t=m(c,function(n){return n.index2!==r?!!e:void(e=!0)}))&&(e=!1,t=m(c,function(n){var t=n.index1,n=n.index2;if(-1!==t||n+1!==r)return!!e;e=!0})),-1===t?c.push({index1:-1,index2:r,pos:n[1]}):c.splice(t,0,{index1:-1,index2:r,pos:n[1]}))});var t=c.map(function(n){return n.pos}),r={};return t.filter(function(n){n=n[0]+"x"+n[1];return!r[n]&&(r[n]=!0)})}return{__proto__:null,getAreaSize:r,getMinMaxs:v,isInside:s,getLinearConstants:M,getIntersectionPointsByConstants:x,getIntersectionPoints:function(n,t,r){var e=x(M(n[0],n[1]),M(t[0],t[1]));return r?d(e,[n,t]):e},getPointsOnLines:d,convertLines:y,getOverlapPoints:a,getOverlapSize:function(n,t){return r(a(n,t))}}}); | ||
//# sourceMappingURL=overlap-area.min.js.map |
{ | ||
"name": "overlap-area", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Find the Overlap Area.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/overlap-area.cjs.js", |
import { sum, findIndex, getShapeDirection, getDist } from "@daybrush/utils"; | ||
import { PointInfo } from "./types"; | ||
@@ -299,19 +300,25 @@ /** | ||
export function getOverlapPoints(points1: number[][], points2: number[][]) { | ||
const lines1 = convertLines(points1); | ||
const lines2 = convertLines(points2); | ||
const targetPoints1 = points1.slice(); | ||
const targetPoints2 = points2.slice(); | ||
if (getShapeDirection(targetPoints1) === -1) { | ||
targetPoints1.reverse(); | ||
} | ||
if (getShapeDirection(targetPoints2) === -1) { | ||
targetPoints2.reverse(); | ||
} | ||
const lines1 = convertLines(targetPoints1); | ||
const lines2 = convertLines(targetPoints2); | ||
const linearConstantss1 = lines1.map(line1 => getLinearConstants(line1[0], line1[1])); | ||
const linearConstantss2 = lines2.map(line2 => getLinearConstants(line2[0], line2[1])); | ||
const overlappingPoints: number[][] = []; | ||
const points2Direction = getShapeDirection(points2); | ||
const overlapInfos: Array<{ | ||
index1: number; | ||
index2: number; | ||
pos: number[]; | ||
}> = []; | ||
lines2.forEach(line2 => { | ||
if (isInside(line2[1], points1)) { | ||
overlappingPoints[points2Direction > 0 ? "push" : "unshift"](line2[1]); | ||
} | ||
}); | ||
linearConstantss1.forEach((linearConstants1, i) => { | ||
const line1 = lines1[i]; | ||
const linePoints: number[][] = []; | ||
const linePointInfos: PointInfo[] = []; | ||
linearConstantss2.forEach((linearConstants2, j) => { | ||
@@ -321,15 +328,71 @@ const intersectionPoints = getIntersectionPointsByConstants(linearConstants1, linearConstants2); | ||
linePoints.push(...points); | ||
linePointInfos.push(...points.map(pos => ({ | ||
index1: i, | ||
index2: j, | ||
pos, | ||
}))); | ||
}); | ||
linePoints.sort((a, b) => { | ||
return getDist(line1[0], a) - getDist(line1[0], b); | ||
linePointInfos.sort((a, b) => { | ||
return getDist(line1[0], a.pos) - getDist(line1[0], b.pos); | ||
}); | ||
overlappingPoints.push(...linePoints); | ||
if (isInside(line1[1], points2)) { | ||
overlappingPoints.push(line1[1]); | ||
overlapInfos.push(...linePointInfos); | ||
if (isInside(line1[1], targetPoints2)) { | ||
overlapInfos.push({ | ||
index1: i, | ||
index2: -1, | ||
pos: line1[1], | ||
}); | ||
} | ||
}); | ||
lines2.forEach((line2, i) => { | ||
if (isInside(line2[1], targetPoints1)) { | ||
let isNext = false; | ||
let index = findIndex(overlapInfos, ({ index2 }) => { | ||
if (index2 === i) { | ||
isNext = true; | ||
return false; | ||
} | ||
if (isNext) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (index === -1) { | ||
isNext = false; | ||
index = findIndex(overlapInfos, ({ index1, index2 }) => { | ||
if (index1 === -1 && index2 + 1 === i) { | ||
isNext = true; | ||
return false; | ||
} | ||
if (isNext) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
if (index === -1) { | ||
overlapInfos.push({ | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1], | ||
}); | ||
} else { | ||
overlapInfos.splice(index, 0, { | ||
index1: -1, | ||
index2: i, | ||
pos: line2[1], | ||
}); | ||
} | ||
} | ||
}); | ||
// console.log(overlapInfos); | ||
const overlapPoints = overlapInfos.map(({ pos }) => pos); | ||
const pointMap: Record<string, boolean> = {}; | ||
return overlappingPoints.filter(point => { | ||
return overlapPoints.filter(point => { | ||
const key = `${point[0]}x${point[1]}`; | ||
@@ -336,0 +399,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
164728
25
1935