polygon-offset
Advanced tools
+77
-12
@@ -561,3 +561,3 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Offset=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
| * @param {Object} next | ||
| * @return {[type]} [description] | ||
| * @cosntructor | ||
| */ | ||
@@ -687,3 +687,3 @@ function Edge(current, next) { | ||
| * | ||
| * @param {Array.<Object>} vertices | ||
| * @param {Array.<Object>=} vertices | ||
| * @param {Number=} arcSegments | ||
@@ -693,6 +693,2 @@ * @constructor | ||
| function Offset(vertices, arcSegments) { | ||
| var edges = []; | ||
| for (var i = 0, len = vertices.length; i < len; i++) { | ||
| edges.push(new Edge(vertices[i], vertices[(i + 1) % len])); | ||
| } | ||
@@ -702,3 +698,3 @@ /** | ||
| */ | ||
| this.vertices = vertices; | ||
| this.vertices = null; | ||
@@ -708,12 +704,64 @@ /** | ||
| */ | ||
| this.edges = edges; | ||
| this.edges = null; | ||
| /** | ||
| * @type {Boolean} | ||
| */ | ||
| this._closed = false; | ||
| if (vertices) { | ||
| this.data(vertices); | ||
| } | ||
| /** | ||
| * Segments in edge bounding arches | ||
| * @type {Number} | ||
| */ | ||
| this.arcSegments = arcSegments || 5; | ||
| this._arcSegments = arcSegments || 5; | ||
| }; | ||
| /** | ||
| * Change data set | ||
| * @param {Array.<Array>} vertices | ||
| * @return {Offset} | ||
| */ | ||
| Offset.prototype.data = function(vertices) { | ||
| vertices = this.validate(vertices); | ||
| var edges = []; | ||
| for (var i = 0, len = vertices.length; i < len; i++) { | ||
| edges.push(new Edge(vertices[i], vertices[(i + 1) % len])); | ||
| } | ||
| this.vertices = vertices; | ||
| this.edges = edges; | ||
| return this; | ||
| }; | ||
| /** | ||
| * @param {Number} arcSegments | ||
| * @return {Offset} | ||
| */ | ||
| Offset.prototype.arcSegments = function(arcSegments) { | ||
| this._arcSegments = arcSegments; | ||
| return this; | ||
| }; | ||
| /** | ||
| * Validates if the first and last points repeat | ||
| * TODO: check CCW | ||
| * | ||
| * @param {Array.<Object>} vertices | ||
| */ | ||
| Offset.prototype.validate = function(vertices) { | ||
| var len = vertices.length; | ||
| if (vertices[0].x === vertices[len - 1].x && | ||
| vertices[0].y === vertices[len - 1].y) { | ||
| vertices = vertices.slice(0, len - 1); | ||
| this._closed = true; | ||
| } | ||
| return vertices; | ||
| }; | ||
| /** | ||
| * Creates arch between two edges | ||
@@ -801,3 +849,3 @@ * | ||
| thisEdge.current, | ||
| this.arcSegments, | ||
| this._arcSegments, | ||
| false); | ||
@@ -808,2 +856,4 @@ } | ||
| vertices = union ? union[0] : vertices; | ||
| vertices = this.ensureLastPoint(vertices); | ||
| return vertices; | ||
@@ -848,3 +898,3 @@ }; | ||
| thisEdge.current, | ||
| this.arcSegments, | ||
| this._arcSegments, | ||
| true | ||
@@ -862,6 +912,21 @@ ); | ||
| vertices = this.ensureLastPoint(vertices); | ||
| return vertices; | ||
| } | ||
| }; | ||
| /** | ||
| * @param {Array.<Object>} vertices | ||
| * @return {Array.<Object>} | ||
| */ | ||
| Offset.prototype.ensureLastPoint = function(vertices) { | ||
| if (this._closed) { | ||
| vertices.push({ | ||
| x: vertices[0].x, | ||
| y: vertices[0].y | ||
| }); | ||
| } | ||
| return vertices; | ||
| }; | ||
| /** | ||
| * Decides by the sign if it's a padding or a margin | ||
@@ -868,0 +933,0 @@ * |
+13
-12
| /* | ||
| Licensed under MIT License | ||
| */ | ||
| function r(){return function q(k,d,g){function f(a,c){if(!d[a]){if(!k[a]){var b="function"==typeof require&&require;if(!c&&b)return b(a,!0);if(h)return h(a,!0);b=Error("Cannot find module '"+a+"'");throw b.code="MODULE_NOT_FOUND",b;}b=d[a]={exports:{}};k[a][0].call(b.exports,function(b){var c=k[a][1][b];return f(c?c:b)},b,b.exports,q,k,d,g)}return d[a].exports}for(var h="function"==typeof require&&require,n=0;n<g.length;n++)f(g[n]);return f}({1:[function(q,k,d){(function(g,f){"object"===typeof d? | ||
| k.exports=f():window.greinerHormann=f()})(this,function(){function g(a,c){this.first=null;this.k=0;this.m=null;this.o="undefined"===typeof c?Array.isArray(a[0]):c;for(var b=0,e=a.length;b<e;b++)this.h(new h(a[b]))}function f(a,c,b,e){this.j=this.toSource=this.y=this.x=0;var l=(e.y-b.y)*(c.x-a.x)-(e.x-b.x)*(c.y-a.y);0!==l&&(this.toSource=((e.x-b.x)*(a.y-b.y)-(e.y-b.y)*(a.x-b.x))/l,this.j=((c.x-a.x)*(a.y-b.y)-(c.y-a.y)*(a.x-b.x))/l,this.valid()&&(this.x=a.x+this.toSource*(c.x-a.x),this.y=a.y+this.toSource* | ||
| (c.y-a.y)))}function h(a,c){1===arguments.length&&(Array.isArray(a)?(c=a[1],a=a[0]):(c=a.y,a=a.x));this.x=a;this.y=c;this.d=this.c=this.next=null;this.l=0;this.g=!0;this.f=this.a=!1}function d(a,c,b,e){a=new g(a);c=new g(c);return a.clip(c,b,e)}Array.isArray=Array.isArray||function(a){return Boolean(a&&"[object Array]"===Object.prototype.toString.call(Object(a)))};h.t=function(a,c,b){a=new h(a,c);a.l=b;a.a=!0;a.g=!1;return a};h.prototype.A=function(){this.f=!0;null===this.d||this.d.f||this.d.A()}; | ||
| h.prototype.b=function(a){return this.x===a.x&&this.y===a.y};h.prototype.w=function(a){var c=!1,b=a.first,e=b.next,l=this.x,g=this.y;do(b.y<g&&e.y>=g||e.y<g&&b.y>=g)&&(b.x<=l||e.x<=l)&&(c^=b.x+(g-b.y)/(e.y-b.y)*(e.x-b.x)<l),b=b.next,e=b.next||a.first;while(!b.b(a.first));return c};f.prototype.valid=function(){return 0<this.toSource&&1>this.toSource&&0<this.j&&1>this.j};g.prototype.h=function(a){if(null==this.first)this.first=a,this.first.next=a,this.first.c=a;else{var c=this.first,b=c.c;c.c=a;a.next= | ||
| c;a.c=b;b.next=a}this.k++};g.prototype.u=function(a,c,b){for(;!c.b(b)&&c.l<a.l;)c=c.next;a.next=c;b=c.c;a.c=b;b.next=a;c.c=a;this.k++};g.prototype.i=function(a){for(;a.a;)a=a.next;return a};g.prototype.C=function(){var a=this.B||this.first;do{if(a.a&&!a.f)break;a=a.next}while(!a.b(this.first));return this.B=a};g.prototype.D=function(){var a=this.m||this.first;do{if(a.a&&!a.f)return this.m=a,!0;a=a.next}while(!a.b(this.first));this.m=null;return!1};g.prototype.n=function(){var a=[],c=this.first;if(this.o){do a.push([c.x, | ||
| c.y]),c=c.next;while(c!==this.first)}else{do a.push({x:c.x,y:c.y}),c=c.next;while(c!==this.first)}return a};g.prototype.clip=function(a,c,b){var e=this.first,l=a.first,d,m;do{if(!e.a){do l.a||(m=new f(e,this.i(e.next),l,a.i(l.next)),m.valid()&&(d=h.t(m.x,m.y,m.toSource),m=h.t(m.x,m.y,m.j),d.d=m,m.d=d,this.u(d,e,this.i(e.next)),a.u(m,l,a.i(l.next)))),l=l.next;while(!l.b(a.first))}e=e.next}while(!e.b(this.first));e=this.first;l=a.first;d=e.w(a);m=l.w(this);c^=d;b^=m;do e.a&&(e.g=c,c=!c),e=e.next;while(!e.b(this.first)); | ||
| do l.a&&(l.g=b,b=!b),l=l.next;while(!l.b(a.first));for(c=[];this.D();){b=this.C();e=new g([],this.o);e.h(new h(b.x,b.y));do{b.A();if(b.g){do b=b.next,e.h(new h(b.x,b.y));while(!b.a)}else{do b=b.c,e.h(new h(b.x,b.y));while(!b.a)}b=b.d}while(!b.f);c.push(e.n())}0===c.length&&(d&&c.push(this.n()),m&&c.push(a.n()),0===c.length&&(c=null));return c};return{union:function(a,c){return d(a,c,!1,!1)},intersection:function(a,c){return d(a,c,!0,!0)},diff:function(a,c){return d(a,c,!1,!0)},clip:d}})},{}],2:[function(q, | ||
| k){function d(d,f){this.current=d;this.next=f;this.p=this.v();this.q=this.F()}d.prototype.F=function(){var d=this.v();return{x:-d.x,y:-d.y}};d.prototype.v=function(){var d=this.next.x-this.current.x,f=this.next.y-this.current.y,h=Math.sqrt(d*d+f*f);return{x:-f/h,y:d/h}};d.prototype.offset=function(g,f){var h=this.current,n=this.next;return new d({x:h.x+g,y:h.y+f},{x:n.x+g,y:n.y+f})};k.exports=d},{}],3:[function(q,k){k.exports=function(d,g,f,h){var n=(h.y-f.y)*(g.x-d.x)-(h.x-f.x)*(g.y-d.y);if(0==n)return null; | ||
| h=((h.x-f.x)*(d.y-f.y)-(h.y-f.y)*(d.x-f.x))/n;f=((g.x-d.x)*(d.y-f.y)-(g.y-d.y)*(d.x-f.x))/n;return 0>h||0>f||1<h||1<f?null:{x:d.x+h*(g.x-d.x),y:d.y+h*(g.y-d.y)}}},{}],4:[function(q,k){function d(a,c){for(var b=[],e=0,d=a.length;e<d;e++)b.push(new f(a[e],a[(e+1)%d]));this.k=a;this.e=b;this.r=c||5}var g=q("greiner-hormann"),f=q("./edge"),h=q("./intersection");"use strict";var n=Math.atan2;d.prototype.s=function(a,c,b,e,d,g,h){var f=2*Math.PI,k=n(e.y-c.y,e.x-c.x),p=n(d.y-c.y,d.x-c.x);0===g%2&&--g;0> | ||
| k&&(k+=f);0>p&&(p+=f);p=k>p?k-p:k+f-p;h=(h?-p:f-p)/g;a.push(e);for(e=1;e<g;++e)p=k+h*e,a.push({x:c.x+Math.cos(p)*b,y:c.y+Math.sin(p)*b});a.push(d)};d.prototype.padding=function(a){var c=[],b=[],e,d;e=0;for(d=this.e.length;e<d;e++){var f=this.e[e];c.push(f.offset(f.q.x*a,f.q.y*a))}e=0;for(d=c.length;e<d;e++){var f=c[e],m=c[(e+d-1)%d],k=h(m.current,m.next,f.current,f.next);k?b.push(k):this.s(b,this.e[e].current,a,m.next,f.current,this.r,!1)}return b=(a=g.union(b,b))?a[0]:b};d.prototype.margin=function(a){var c= | ||
| [],b=[],e,d;e=0;for(d=this.e.length;e<d;e++){var f=this.e[e];c.push(f.offset(f.p.x*a,f.p.y*a))}e=0;for(d=c.length;e<d;e++){var f=c[e],k=c[(e+d-1)%d],n=h(k.current,k.next,f.current,f.next);n?b.push(n):this.s(b,this.e[e].current,a,k.next,f.current,this.r,!0)}if(a=g.union(b,b))a=a[0],b=a.slice(0,a.length/2);return b};d.prototype.offset=function(a){return 0===a?this.k:0<a?this.margin(a):this.padding(-a)};k.exports=d},{"./edge":2,"./intersection":3,"greiner-hormann":1}]},{},[4])(4)} | ||
| if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.G)define([],r);else{var s;"undefined"!=typeof window?s=window:"undefined"!=typeof global?s=global:"undefined"!=typeof self&&(s=self);s.Offset=r()}!0; | ||
| function s(){return function q(k,d,h){function f(a,c){if(!d[a]){if(!k[a]){var b="function"==typeof require&&require;if(!c&&b)return b(a,!0);if(g)return g(a,!0);b=Error("Cannot find module '"+a+"'");throw b.code="MODULE_NOT_FOUND",b;}b=d[a]={exports:{}};k[a][0].call(b.exports,function(b){var c=k[a][1][b];return f(c?c:b)},b,b.exports,q,k,d,h)}return d[a].exports}for(var g="function"==typeof require&&require,m=0;m<h.length;m++)f(h[m]);return f}({1:[function(q,k,d){(function(h,f){"object"===typeof d? | ||
| k.exports=f():window.greinerHormann=f()})(this,function(){function h(a,c){this.first=null;this.g=0;this.m=null;this.p="undefined"===typeof c?Array.isArray(a[0]):c;for(var b=0,e=a.length;b<e;b++)this.i(new g(a[b]))}function d(a,c,b,e){this.k=this.toSource=this.y=this.x=0;var l=(e.y-b.y)*(c.x-a.x)-(e.x-b.x)*(c.y-a.y);0!==l&&(this.toSource=((e.x-b.x)*(a.y-b.y)-(e.y-b.y)*(a.x-b.x))/l,this.k=((c.x-a.x)*(a.y-b.y)-(c.y-a.y)*(a.x-b.x))/l,this.valid()&&(this.x=a.x+this.toSource*(c.x-a.x),this.y=a.y+this.toSource* | ||
| (c.y-a.y)))}function g(a,c){1===arguments.length&&(Array.isArray(a)?(c=a[1],a=a[0]):(c=a.y,a=a.x));this.x=a;this.y=c;this.e=this.c=this.next=null;this.l=0;this.h=!0;this.f=this.a=!1}function m(a,c,b,e){a=new h(a);c=new h(c);return a.clip(c,b,e)}Array.isArray=Array.isArray||function(a){return Boolean(a&&"[object Array]"===Object.prototype.toString.call(Object(a)))};g.u=function(a,c,b){a=new g(a,c);a.l=b;a.a=!0;a.h=!1;return a};g.prototype.C=function(){this.f=!0;null===this.e||this.e.f||this.e.C()}; | ||
| g.prototype.b=function(a){return this.x===a.x&&this.y===a.y};g.prototype.B=function(a){var c=!1,b=a.first,e=b.next,l=this.x,d=this.y;do(b.y<d&&e.y>=d||e.y<d&&b.y>=d)&&(b.x<=l||e.x<=l)&&(c^=b.x+(d-b.y)/(e.y-b.y)*(e.x-b.x)<l),b=b.next,e=b.next||a.first;while(!b.b(a.first));return c};d.prototype.valid=function(){return 0<this.toSource&&1>this.toSource&&0<this.k&&1>this.k};h.prototype.i=function(a){if(null==this.first)this.first=a,this.first.next=a,this.first.c=a;else{var c=this.first,b=c.c;c.c=a;a.next= | ||
| c;a.c=b;b.next=a}this.g++};h.prototype.w=function(a,c,b){for(;!c.b(b)&&c.l<a.l;)c=c.next;a.next=c;b=c.c;a.c=b;b.next=a;c.c=a;this.g++};h.prototype.j=function(a){for(;a.a;)a=a.next;return a};h.prototype.F=function(){var a=this.D||this.first;do{if(a.a&&!a.f)break;a=a.next}while(!a.b(this.first));return this.D=a};h.prototype.G=function(){var a=this.m||this.first;do{if(a.a&&!a.f)return this.m=a,!0;a=a.next}while(!a.b(this.first));this.m=null;return!1};h.prototype.n=function(){var a=[],c=this.first;if(this.p){do a.push([c.x, | ||
| c.y]),c=c.next;while(c!==this.first)}else{do a.push({x:c.x,y:c.y}),c=c.next;while(c!==this.first)}return a};h.prototype.clip=function(a,c,b){var e=this.first,l=a.first,r,n;do{if(!e.a){do l.a||(n=new d(e,this.j(e.next),l,a.j(l.next)),n.valid()&&(r=g.u(n.x,n.y,n.toSource),n=g.u(n.x,n.y,n.k),r.e=n,n.e=r,this.w(r,e,this.j(e.next)),a.w(n,l,a.j(l.next)))),l=l.next;while(!l.b(a.first))}e=e.next}while(!e.b(this.first));e=this.first;l=a.first;r=e.B(a);n=l.B(this);c^=r;b^=n;do e.a&&(e.h=c,c=!c),e=e.next;while(!e.b(this.first)); | ||
| do l.a&&(l.h=b,b=!b),l=l.next;while(!l.b(a.first));for(c=[];this.G();){b=this.F();e=new h([],this.p);e.i(new g(b.x,b.y));do{b.C();if(b.h){do b=b.next,e.i(new g(b.x,b.y));while(!b.a)}else{do b=b.c,e.i(new g(b.x,b.y));while(!b.a)}b=b.e}while(!b.f);c.push(e.n())}0===c.length&&(r&&c.push(this.n()),n&&c.push(a.n()),0===c.length&&(c=null));return c};return{union:function(a,c){return m(a,c,!1,!1)},intersection:function(a,c){return m(a,c,!0,!0)},diff:function(a,c){return m(a,c,!1,!0)},clip:m}})},{}],2:[function(q, | ||
| k){function d(d,f){this.current=d;this.next=f;this.r=this.A();this.s=this.H()}d.prototype.H=function(){var d=this.A();return{x:-d.x,y:-d.y}};d.prototype.A=function(){var d=this.next.x-this.current.x,f=this.next.y-this.current.y,g=Math.sqrt(d*d+f*f);return{x:-f/g,y:d/g}};d.prototype.offset=function(h,f){var g=this.current,m=this.next;return new d({x:g.x+h,y:g.y+f},{x:m.x+h,y:m.y+f})};k.exports=d},{}],3:[function(q,k){k.exports=function(d,h,f,g){var m=(g.y-f.y)*(h.x-d.x)-(g.x-f.x)*(h.y-d.y);if(0==m)return null; | ||
| g=((g.x-f.x)*(d.y-f.y)-(g.y-f.y)*(d.x-f.x))/m;f=((h.x-d.x)*(d.y-f.y)-(h.y-d.y)*(d.x-f.x))/m;return 0>g||0>f||1<g||1<f?null:{x:d.x+g*(h.x-d.x),y:d.y+g*(h.y-d.y)}}},{}],4:[function(q,k){function d(a,c){this.d=this.g=null;this.q=!1;a&&this.data(a);this.o=c||5}var h=q("greiner-hormann"),f=q("./edge"),g=q("./intersection");"use strict";var m=Math.atan2;d.prototype.data=function(a){a=this.I(a);for(var c=[],b=0,e=a.length;b<e;b++)c.push(new f(a[b],a[(b+1)%e]));this.g=a;this.d=c;return this};d.prototype.I= | ||
| function(a){var c=a.length;a[0].x===a[c-1].x&&a[0].y===a[c-1].y&&(a=a.slice(0,c-1),this.q=!0);return a};d.prototype.t=function(a,c,b,e,d,g,f){var h=2*Math.PI,k=m(e.y-c.y,e.x-c.x),p=m(d.y-c.y,d.x-c.x);0===g%2&&--g;0>k&&(k+=h);0>p&&(p+=h);p=k>p?k-p:k+h-p;f=(f?-p:h-p)/g;a.push(e);for(e=1;e<g;++e)p=k+f*e,a.push({x:c.x+Math.cos(p)*b,y:c.y+Math.sin(p)*b});a.push(d)};d.prototype.padding=function(a){var c=[],b=[],e,d;e=0;for(d=this.d.length;e<d;e++){var f=this.d[e];c.push(f.offset(f.s.x*a,f.s.y*a))}e=0;for(d= | ||
| c.length;e<d;e++){var f=c[e],n=c[(e+d-1)%d],k=g(n.current,n.next,f.current,f.next);k?b.push(k):this.t(b,this.d[e].current,a,n.next,f.current,this.o,!1)}b=(a=h.union(b,b))?a[0]:b;return b=this.v(b)};d.prototype.margin=function(a){var c=[],b=[],e,d;e=0;for(d=this.d.length;e<d;e++){var f=this.d[e];c.push(f.offset(f.r.x*a,f.r.y*a))}e=0;for(d=c.length;e<d;e++){var f=c[e],k=c[(e+d-1)%d],m=g(k.current,k.next,f.current,f.next);m?b.push(m):this.t(b,this.d[e].current,a,k.next,f.current,this.o,!0)}if(a=h.union(b, | ||
| b))a=a[0],b=a.slice(0,a.length/2);return b=this.v(b)};d.prototype.v=function(a){this.q&&a.push({x:a[0].x,y:a[0].y});return a};d.prototype.offset=function(a){return 0===a?this.g:0<a?this.margin(a):this.padding(-a)};k.exports=d},{"./edge":2,"./intersection":3,"greiner-hormann":1}]},{},[4])(4)} | ||
| if("object"==typeof exports&&"undefined"!=typeof module)module.exports=s();else if("function"==typeof define&&define.J)define([],s);else{var t;"undefined"!=typeof window?t=window:"undefined"!=typeof global?t=global:"undefined"!=typeof self&&(t=self);t.Offset=s()}!0; |
+77
-12
@@ -9,3 +9,3 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Offset=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
| * @param {Object} next | ||
| * @return {[type]} [description] | ||
| * @cosntructor | ||
| */ | ||
@@ -135,3 +135,3 @@ function Edge(current, next) { | ||
| * | ||
| * @param {Array.<Object>} vertices | ||
| * @param {Array.<Object>=} vertices | ||
| * @param {Number=} arcSegments | ||
@@ -141,6 +141,2 @@ * @constructor | ||
| function Offset(vertices, arcSegments) { | ||
| var edges = []; | ||
| for (var i = 0, len = vertices.length; i < len; i++) { | ||
| edges.push(new Edge(vertices[i], vertices[(i + 1) % len])); | ||
| } | ||
@@ -150,3 +146,3 @@ /** | ||
| */ | ||
| this.vertices = vertices; | ||
| this.vertices = null; | ||
@@ -156,12 +152,64 @@ /** | ||
| */ | ||
| this.edges = edges; | ||
| this.edges = null; | ||
| /** | ||
| * @type {Boolean} | ||
| */ | ||
| this._closed = false; | ||
| if (vertices) { | ||
| this.data(vertices); | ||
| } | ||
| /** | ||
| * Segments in edge bounding arches | ||
| * @type {Number} | ||
| */ | ||
| this.arcSegments = arcSegments || 5; | ||
| this._arcSegments = arcSegments || 5; | ||
| }; | ||
| /** | ||
| * Change data set | ||
| * @param {Array.<Array>} vertices | ||
| * @return {Offset} | ||
| */ | ||
| Offset.prototype.data = function(vertices) { | ||
| vertices = this.validate(vertices); | ||
| var edges = []; | ||
| for (var i = 0, len = vertices.length; i < len; i++) { | ||
| edges.push(new Edge(vertices[i], vertices[(i + 1) % len])); | ||
| } | ||
| this.vertices = vertices; | ||
| this.edges = edges; | ||
| return this; | ||
| }; | ||
| /** | ||
| * @param {Number} arcSegments | ||
| * @return {Offset} | ||
| */ | ||
| Offset.prototype.arcSegments = function(arcSegments) { | ||
| this._arcSegments = arcSegments; | ||
| return this; | ||
| }; | ||
| /** | ||
| * Validates if the first and last points repeat | ||
| * TODO: check CCW | ||
| * | ||
| * @param {Array.<Object>} vertices | ||
| */ | ||
| Offset.prototype.validate = function(vertices) { | ||
| var len = vertices.length; | ||
| if (vertices[0].x === vertices[len - 1].x && | ||
| vertices[0].y === vertices[len - 1].y) { | ||
| vertices = vertices.slice(0, len - 1); | ||
| this._closed = true; | ||
| } | ||
| return vertices; | ||
| }; | ||
| /** | ||
| * Creates arch between two edges | ||
@@ -249,3 +297,3 @@ * | ||
| thisEdge.current, | ||
| this.arcSegments, | ||
| this._arcSegments, | ||
| false); | ||
@@ -256,2 +304,4 @@ } | ||
| vertices = union ? union[0] : vertices; | ||
| vertices = this.ensureLastPoint(vertices); | ||
| return vertices; | ||
@@ -296,3 +346,3 @@ }; | ||
| thisEdge.current, | ||
| this.arcSegments, | ||
| this._arcSegments, | ||
| true | ||
@@ -310,6 +360,21 @@ ); | ||
| vertices = this.ensureLastPoint(vertices); | ||
| return vertices; | ||
| } | ||
| }; | ||
| /** | ||
| * @param {Array.<Object>} vertices | ||
| * @return {Array.<Object>} | ||
| */ | ||
| Offset.prototype.ensureLastPoint = function(vertices) { | ||
| if (this._closed) { | ||
| vertices.push({ | ||
| x: vertices[0].x, | ||
| y: vertices[0].y | ||
| }); | ||
| } | ||
| return vertices; | ||
| }; | ||
| /** | ||
| * Decides by the sign if it's a padding or a margin | ||
@@ -316,0 +381,0 @@ * |
+1
-1
| { | ||
| "name": "polygon-offset", | ||
| "version": "0.0.3", | ||
| "version": "0.0.4", | ||
| "description": "Polygon offsetting algorithm, aimed for use with leaflet", | ||
@@ -5,0 +5,0 @@ "main": "src/offset.js", |
+8
-4
@@ -31,11 +31,15 @@ # Offset | ||
| ```js | ||
| // if the first point repeats(for instance GeoJSON) - | ||
| // the output will have it as well | ||
| var points = [ | ||
| {x:0, y: 0}, {x: 100, y:0}, | ||
| {x: 100, y:100}, {x: 0, y: 100}]; | ||
| {x: 100, y:100}, {x: 0, y: 100}, | ||
| {x:0, y:0}], x = -10; | ||
| var margined = new Offset(points).margin(10); | ||
| var padding = new Offset(points).padding(10); | ||
| var offset = new Offset(); | ||
| var margined = offset.data(points).margin(10); | ||
| var padding = offset.data(points).padding(10); | ||
| // decides from the sign of x: negative for padding | ||
| var unknown = new Offset(points).offset(x); | ||
| var unknown = offset.data(points).arcSegments(3).offset(x); | ||
@@ -42,0 +46,0 @@ ``` |
+1
-1
@@ -8,3 +8,3 @@ "use strict"; | ||
| * @param {Object} next | ||
| * @return {[type]} [description] | ||
| * @cosntructor | ||
| */ | ||
@@ -11,0 +11,0 @@ function Edge(current, next) { |
+76
-11
@@ -14,3 +14,3 @@ var GreinerHormann = require('greiner-hormann'); | ||
| * | ||
| * @param {Array.<Object>} vertices | ||
| * @param {Array.<Object>=} vertices | ||
| * @param {Number=} arcSegments | ||
@@ -20,6 +20,2 @@ * @constructor | ||
| function Offset(vertices, arcSegments) { | ||
| var edges = []; | ||
| for (var i = 0, len = vertices.length; i < len; i++) { | ||
| edges.push(new Edge(vertices[i], vertices[(i + 1) % len])); | ||
| } | ||
@@ -29,3 +25,3 @@ /** | ||
| */ | ||
| this.vertices = vertices; | ||
| this.vertices = null; | ||
@@ -35,12 +31,64 @@ /** | ||
| */ | ||
| this.edges = edges; | ||
| this.edges = null; | ||
| /** | ||
| * @type {Boolean} | ||
| */ | ||
| this._closed = false; | ||
| if (vertices) { | ||
| this.data(vertices); | ||
| } | ||
| /** | ||
| * Segments in edge bounding arches | ||
| * @type {Number} | ||
| */ | ||
| this.arcSegments = arcSegments || 5; | ||
| this._arcSegments = arcSegments || 5; | ||
| }; | ||
| /** | ||
| * Change data set | ||
| * @param {Array.<Array>} vertices | ||
| * @return {Offset} | ||
| */ | ||
| Offset.prototype.data = function(vertices) { | ||
| vertices = this.validate(vertices); | ||
| var edges = []; | ||
| for (var i = 0, len = vertices.length; i < len; i++) { | ||
| edges.push(new Edge(vertices[i], vertices[(i + 1) % len])); | ||
| } | ||
| this.vertices = vertices; | ||
| this.edges = edges; | ||
| return this; | ||
| }; | ||
| /** | ||
| * @param {Number} arcSegments | ||
| * @return {Offset} | ||
| */ | ||
| Offset.prototype.arcSegments = function(arcSegments) { | ||
| this._arcSegments = arcSegments; | ||
| return this; | ||
| }; | ||
| /** | ||
| * Validates if the first and last points repeat | ||
| * TODO: check CCW | ||
| * | ||
| * @param {Array.<Object>} vertices | ||
| */ | ||
| Offset.prototype.validate = function(vertices) { | ||
| var len = vertices.length; | ||
| if (vertices[0].x === vertices[len - 1].x && | ||
| vertices[0].y === vertices[len - 1].y) { | ||
| vertices = vertices.slice(0, len - 1); | ||
| this._closed = true; | ||
| } | ||
| return vertices; | ||
| }; | ||
| /** | ||
| * Creates arch between two edges | ||
@@ -128,3 +176,3 @@ * | ||
| thisEdge.current, | ||
| this.arcSegments, | ||
| this._arcSegments, | ||
| false); | ||
@@ -135,2 +183,4 @@ } | ||
| vertices = union ? union[0] : vertices; | ||
| vertices = this.ensureLastPoint(vertices); | ||
| return vertices; | ||
@@ -175,3 +225,3 @@ }; | ||
| thisEdge.current, | ||
| this.arcSegments, | ||
| this._arcSegments, | ||
| true | ||
@@ -189,6 +239,21 @@ ); | ||
| vertices = this.ensureLastPoint(vertices); | ||
| return vertices; | ||
| } | ||
| }; | ||
| /** | ||
| * @param {Array.<Object>} vertices | ||
| * @return {Array.<Object>} | ||
| */ | ||
| Offset.prototype.ensureLastPoint = function(vertices) { | ||
| if (this._closed) { | ||
| vertices.push({ | ||
| x: vertices[0].x, | ||
| y: vertices[0].y | ||
| }); | ||
| } | ||
| return vertices; | ||
| }; | ||
| /** | ||
| * Decides by the sign if it's a padding or a margin | ||
@@ -195,0 +260,0 @@ * |
+12
-7
| var Offset = require('../src/offset'); | ||
| // CCW | ||
| var points = [{ | ||
@@ -7,4 +8,4 @@ x: 0, | ||
| }, { | ||
| x: 100, | ||
| y: 0 | ||
| x: 0, | ||
| y: 100 | ||
| }, { | ||
@@ -14,13 +15,17 @@ x: 100, | ||
| }, { | ||
| x: 100, | ||
| y: 0 | ||
| }, { | ||
| x: 0, | ||
| y: 100 | ||
| y: 0 | ||
| }], | ||
| x = -10; | ||
| var margined = new Offset(points).margin(10); | ||
| var padding = new Offset(points).padding(10); | ||
| var offset = new Offset(); | ||
| var margined = offset.data(points).margin(10); | ||
| var padding = offset.data(points).padding(10); | ||
| // decides from the sign of x: negative for padding | ||
| var unknown = new Offset(points).offset(x); | ||
| var unknown = offset.data(points).arcSegments(3).offset(x); | ||
| console.log('margined', margined, '\n\npadded:', padding, '\n\nadaptive', unknown); | ||
| console.log('data:', points, '\n\nmargined', margined, '\n\npadded:', padding, '\n\nadaptive', unknown); |
697262
0.64%10723
1.66%98
4.26%