UNPKG

greiner-hormann

Version:

Greiner-Hormann clipping algorithm

11 lines (10 loc) 4.93 kB
/** * greiner-hormann v1.4.2 * Greiner-Hormann clipping algorithm * * @author Alexander Milevski <info@w8r.name> * @license MIT * @preserve */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.greinerHormann={})}(this,function(t){"use strict";var e=function(t,e){1===arguments.length&&(Array.isArray(t)?(e=t[1],t=t[0]):(e=t.y,t=t.x)),this.x=t,this.y=e,this.next=null,this.prev=null,this._corresponding=null,this._distance=0,this._isEntry=!0,this._isIntersection=!1,this._visited=!1};e.createIntersection=function(t,i,s){var n=new e(t,i);return n._distance=s,n._isIntersection=!0,n._isEntry=!1,n},e.prototype.visit=function(){this._visited=!0,null===this._corresponding||this._corresponding._visited||this._corresponding.visit()},e.prototype.equals=function(t){return this.x===t.x&&this.y===t.y},e.prototype.isInside=function(t){var e=!1,i=t.first,s=i.next,n=this.x,r=this.y;do{(i.y<r&&s.y>=r||s.y<r&&i.y>=r)&&(i.x<=n||s.x<=n)&&(e^=i.x+(r-i.y)/(s.y-i.y)*(s.x-i.x)<n),s=(i=i.next).next||t.first}while(!i.equals(t.first));return e};var i=function(t,e,i,s){this.x=0,this.y=0,this.toSource=0,this.toClip=0;var n=(s.y-i.y)*(e.x-t.x)-(s.x-i.x)*(e.y-t.y);0!==n&&(this.toSource=((s.x-i.x)*(t.y-i.y)-(s.y-i.y)*(t.x-i.x))/n,this.toClip=((e.x-t.x)*(t.y-i.y)-(e.y-t.y)*(t.x-i.x))/n,this.valid()&&(this.x=t.x+this.toSource*(e.x-t.x),this.y=t.y+this.toSource*(e.y-t.y)))};i.prototype.valid=function(){return 0<this.toSource&&this.toSource<1&&0<this.toClip&&this.toClip<1};var s=function(t,i){this.first=null,this.vertices=0,this._lastUnprocessed=null,this._arrayVertices=void 0===i?Array.isArray(t[0]):i;for(var s=0,n=t.length;s<n;s++)this.addVertex(new e(t[s]))};function n(t){var e=t;if(e){e[0][0]===e[e.length-1][0]&&e[0][1]===e[e.length-1][1]&&(e=e.slice(0,e.length-1));for(var i=0,s=e.length;i<s;i++)e[i]=[e[i][1],e[i][0]];return e}return null}s.prototype.addVertex=function(t){if(null===this.first)this.first=t,this.first.next=t,this.first.prev=t;else{var e=this.first,i=e.prev;e.prev=t,t.next=e,t.prev=i,i.next=t}this.vertices++},s.prototype.insertVertex=function(t,e,i){for(var s,n=e;!n.equals(i)&&n._distance<t._distance;)n=n.next;t.next=n,s=n.prev,t.prev=s,s.next=t,n.prev=t,this.vertices++},s.prototype.getNext=function(t){for(var e=t;e._isIntersection;)e=e.next;return e},s.prototype.getFirstIntersect=function(){var t=this._firstIntersect||this.first;do{if(t._isIntersection&&!t._visited)break;t=t.next}while(!t.equals(this.first));return this._firstIntersect=t,t},s.prototype.hasUnprocessed=function(){var t=this._lastUnprocessed||this.first;do{if(t._isIntersection&&!t._visited)return this._lastUnprocessed=t,!0;t=t.next}while(!t.equals(this.first));return this._lastUnprocessed=null,!1},s.prototype.getPoints=function(){var t=[],e=this.first;if(this._arrayVertices)do{t.push([e.x,e.y]),e=e.next}while(e!==this.first);else do{t.push({x:e.x,y:e.y}),e=e.next}while(e!==this.first);return t},s.prototype.clip=function(t,n,r){var o,h,l=this.first,u=t.first,c=!n&&!r,f=n&&r;do{if(!l._isIntersection)do{if(!u._isIntersection){var x=new i(l,this.getNext(l.next),u,t.getNext(u.next));if(x.valid()){var a=e.createIntersection(x.x,x.y,x.toSource),p=e.createIntersection(x.x,x.y,x.toClip);a._corresponding=p,p._corresponding=a,this.insertVertex(a,l,this.getNext(l.next)),t.insertVertex(p,u,t.getNext(u.next))}}u=u.next}while(!u.equals(t.first));l=l.next}while(!l.equals(this.first));l=this.first,u=t.first,n^=o=l.isInside(t),r^=h=u.isInside(this);do{l._isIntersection&&(l._isEntry=n,n=!n),l=l.next}while(!l.equals(this.first));do{u._isIntersection&&(u._isEntry=r,r=!r),u=u.next}while(!u.equals(t.first));for(var y=[];this.hasUnprocessed();){var d=this.getFirstIntersect(),v=new s([],this._arrayVertices);v.addVertex(new e(d.x,d.y));do{if(d.visit(),d._isEntry)do{d=d.next,v.addVertex(new e(d.x,d.y))}while(!d._isIntersection);else do{d=d.prev,v.addVertex(new e(d.x,d.y))}while(!d._isIntersection);d=d._corresponding}while(!d._visited);y.push(v.getPoints())}return 0===y.length&&(c?o?y.push(t.getPoints()):h?y.push(this.getPoints()):y.push(this.getPoints(),t.getPoints()):f?o?y.push(this.getPoints()):h&&y.push(t.getPoints()):o?y.push(t.getPoints(),this.getPoints()):h?y.push(this.getPoints(),t.getPoints()):y.push(this.getPoints()),0===y.length&&(y=null)),y};var r=function(t,e,i,r){for(var o=[],h=[],l=t._latlngs[0],u=0,c=l.length;u<c;u++)o.push([l[u].lng,l[u].lat]);for(var f=0,x=(l=e._latlngs[0]).length;f<x;f++)h.push([l[f].lng,l[f].lat]);o=new s(o),h=new s(h);var a=o.clip(h,i,r);if(a&&a.length>0){for(var p=0,y=a.length;p<y;p++)a[p]=n(a[p]);return a?1===a.length?a[0]:a:null}return null};t.union=function(t,e){return r(t,e,!1,!1)},t.intersection=function(t,e){return r(t,e,!0,!0)},t.diff=function(t,e){return r(t,e,!1,!0)},t.clip=r,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=greiner-hormann.leaflet.min.js.map