made with requirebin
Last active
August 29, 2015 14:21
-
-
Save tmpvar/b73e86cbdfe7558271a7 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isects = require('2d-polygon-self-intersections'); | |
var poly = [ | |
[0, 0], | |
[10, 0], | |
[0, 10], | |
[10, 10] | |
]; | |
var r = isects(poly); | |
console.log(r); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=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){"use strict";module.exports=exactIntersect;var twoProduct=require("two-product");var robustSum=require("robust-sum");var robustScale=require("robust-scale");var compress=require("robust-compress");function exactIntersect(a,b,c,d){var x1=robustSum([c[1]],[-d[1]]);var y1=robustSum([-c[0]],[d[0]]);var denom=robustSum(robustSum(robustScale(y1,a[1]),robustScale(y1,-b[1])),robustSum(robustScale(x1,a[0]),robustScale(x1,-b[0])));if(denom[0]===0){return[[0],[0],[0]]}var w0=robustSum(twoProduct(-a[0],b[1]),twoProduct(a[1],b[0]));var w1=robustSum(twoProduct(-c[0],d[1]),twoProduct(c[1],d[0]));var nX=robustSum(robustSum(robustScale(w1,a[0]),robustScale(w1,-b[0])),robustSum(robustScale(w0,-c[0]),robustScale(w0,d[0])));var nY=robustSum(robustSum(robustScale(w1,a[1]),robustScale(w1,-b[1])),robustSum(robustScale(w0,-c[1]),robustScale(w0,d[1])));return[compress(nX),compress(nY),compress(denom)]}},{"robust-compress":2,"robust-scale":4,"robust-sum":5,"two-product":6}],2:[function(require,module,exports){"use strict";module.exports=compressExpansion;function compressExpansion(e){var m=e.length;var Q=e[e.length-1];var bottom=m;for(var i=m-2;i>=0;--i){var a=Q;var b=e[i];Q=a+b;var bv=Q-a;var q=b-bv;if(q){e[--bottom]=Q;Q=q}}var top=0;for(var i=bottom;i<m;++i){var a=e[i];var b=Q;Q=a+b;var bv=Q-a;var q=b-bv;if(q){e[top++]=q}}e[top++]=Q;e.length=top;return e}},{}],3:[function(require,module,exports){"use strict";module.exports=fastTwoSum;function fastTwoSum(a,b,result){var x=a+b;var bv=x-a;var av=x-bv;var br=b-bv;var ar=a-av;if(result){result[0]=ar+br;result[1]=x;return result}return[ar+br,x]}},{}],4:[function(require,module,exports){"use strict";var twoProduct=require("two-product");var twoSum=require("two-sum");module.exports=scaleLinearExpansion;function scaleLinearExpansion(e,scale){var n=e.length;if(n===1){var ts=twoProduct(e[0],scale);if(ts[0]){return ts}return[ts[1]]}var g=new Array(2*n);var q=[.1,.1];var t=[.1,.1];var count=0;twoProduct(e[0],scale,q);if(q[0]){g[count++]=q[0]}for(var i=1;i<n;++i){twoProduct(e[i],scale,t);var pq=q[1];twoSum(pq,t[0],q);if(q[0]){g[count++]=q[0]}var a=t[1];var b=q[1];var x=a+b;var bv=x-a;var y=b-bv;q[1]=x;if(y){g[count++]=y}}if(q[1]){g[count++]=q[1]}if(count===0){g[count++]=0}g.length=count;return g}},{"two-product":6,"two-sum":3}],5:[function(require,module,exports){"use strict";module.exports=linearExpansionSum;function scalarScalar(a,b){var x=a+b;var bv=x-a;var av=x-bv;var br=b-bv;var ar=a-av;var y=ar+br;if(y){return[y,x]}return[x]}function linearExpansionSum(e,f){var ne=e.length|0;var nf=f.length|0;if(ne===1&&nf===1){return scalarScalar(e[0],f[0])}var n=ne+nf;var g=new Array(n);var count=0;var eptr=0;var fptr=0;var abs=Math.abs;var ei=e[eptr];var ea=abs(ei);var fi=f[fptr];var fa=abs(fi);var a,b;if(ea<fa){b=ei;eptr+=1;if(eptr<ne){ei=e[eptr];ea=abs(ei)}}else{b=fi;fptr+=1;if(fptr<nf){fi=f[fptr];fa=abs(fi)}}if(eptr<ne&&ea<fa||fptr>=nf){a=ei;eptr+=1;if(eptr<ne){ei=e[eptr];ea=abs(ei)}}else{a=fi;fptr+=1;if(fptr<nf){fi=f[fptr];fa=abs(fi)}}var x=a+b;var bv=x-a;var y=b-bv;var q0=y;var q1=x;var _x,_bv,_av,_br,_ar;while(eptr<ne&&fptr<nf){if(ea<fa){a=ei;eptr+=1;if(eptr<ne){ei=e[eptr];ea=abs(ei)}}else{a=fi;fptr+=1;if(fptr<nf){fi=f[fptr];fa=abs(fi)}}b=q0;x=a+b;bv=x-a;y=b-bv;if(y){g[count++]=y}_x=q1+x;_bv=_x-q1;_av=_x-_bv;_br=x-_bv;_ar=q1-_av;q0=_ar+_br;q1=_x}while(eptr<ne){a=ei;b=q0;x=a+b;bv=x-a;y=b-bv;if(y){g[count++]=y}_x=q1+x;_bv=_x-q1;_av=_x-_bv;_br=x-_bv;_ar=q1-_av;q0=_ar+_br;q1=_x;eptr+=1;if(eptr<ne){ei=e[eptr]}}while(fptr<nf){a=fi;b=q0;x=a+b;bv=x-a;y=b-bv;if(y){g[count++]=y}_x=q1+x;_bv=_x-q1;_av=_x-_bv;_br=x-_bv;_ar=q1-_av;q0=_ar+_br;q1=_x;fptr+=1;if(fptr<nf){fi=f[fptr]}}if(q0){g[count++]=q0}if(q1){g[count++]=q1}if(!count){g[count++]=0}g.length=count;return g}},{}],6:[function(require,module,exports){"use strict";module.exports=twoProduct;var SPLITTER=+(Math.pow(2,27)+1);function twoProduct(a,b,result){var x=a*b;var c=SPLITTER*a;var abig=c-a;var ahi=c-abig;var alo=a-ahi;var d=SPLITTER*b;var bbig=d-b;var bhi=d-bbig;var blo=b-bhi;var err1=x-ahi*bhi;var err2=err1-alo*bhi;var err3=err2-ahi*blo;var y=alo*blo-err3;if(result){result[0]=y;result[1]=x;return result}return[y,x]}},{}],7:[function(require,module,exports){module.exports=estimateFloat;function estimateFloat(predicate){var r=0,l=predicate.length;switch(l){case 1:r=predicate[0];break;case 2:r=predicate[0]+predicate[1];break;case 3:r=predicate[0]+predicate[1]+predicate[2];break;case 4:r=predicate[0]+predicate[1]+predicate[2]+predicate[3];break;default:for(var i=0;i<l;i++){r+=predicate[i]}}return r}},{}],"2d-polygon-self-intersections":[function(require,module,exports){var isect=require("exact-segment-intersect");var float=require("robust-estimate-float");module.exports=selfIntersections;function cmp(a,b){return a[0]===b[0]&&a[1]===b[1]}function valid(a){if(a[2][0]===0){return false}return true}function selfIntersections(poly){var seen={};var l=poly.length;var isects=[];for(var o=0;o<l;o++){var oc=poly[o];var on=poly[(o+1)%l];for(var p=0;p<l;p++){var pc=poly[p];var pn=poly[(p+1)%l];if(cmp(pc,oc)||cmp(pc,on)||cmp(pn,oc)||cmp(pn,on)){continue}var r=isect(oc,on,pc,pn);var wraw=r[2];if(wraw.length===1&&!wraw[0]){continue}var w=float(r[2]);r[0]=float(r[0])/w;r[1]=float(r[0])/w;r.pop();var key=r+"";if(!seen[key]){seen[key]=true;isects.push(r)}}}return isects}},{"exact-segment-intersect":1,"robust-estimate-float":7}]},{},[]);var isects=require("2d-polygon-self-intersections");var poly=[[0,0],[10,0],[0,10],[10,10]];var r=isects(poly);console.log(r); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"2d-polygon-self-intersections": "1.1.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment