Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created October 1, 2015 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmpvar/513f3629c77eeaa3f629 to your computer and use it in GitHub Desktop.
Save tmpvar/513f3629c77eeaa3f629 to your computer and use it in GitHub Desktop.
requirebin sketch
var split = require('split-polygon')
var poly = [[0,0], [1, 0], [1, 1], [0,1]]
function line(a, b) {
var dx = a[0] - b[0];
var dy = a[1] - b[1];
// normalize the direction vector
var imag = 1/Math.sqrt(dx*dx+dy*dy);
var ndx = dx * imag;
var ndy = dy * imag;
var vx = ndx - a[1];
var vy = ndy - a[0];
var dist = ndy * vx - ndx * vy;
// return the "cross" of the 2d normal and the computed distance to the line
return [-ndy, ndx, dist]
}
function debug(o) {
console.group('-')
console.log('positive', o.positive.join(' - '))
console.log('negative', o.negative.join(' - '))
console.groupEnd('-')
}
debug(split(poly, line([0, 0], [.25, .25])))
debug(split(poly, line([.5, 0], [.5, .25])))
debug(split(poly, line([-1, 0], [0, 1])))
debug(split(poly, line([-1, .75], [-.5, .75])))
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";var twoProduct=require("two-product");var robustSum=require("robust-sum");module.exports=robustDotProduct;function robustDotProduct(a,b){var r=twoProduct(a[0],b[0]);for(var i=1;i<a.length;++i){r=robustSum(r,twoProduct(a[i],b[i]))}return r}},{"robust-sum":3,"two-product":2}],2:[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]}},{}],3:[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}},{}],"split-polygon":[function(require,module,exports){"use strict";var robustDot=require("robust-dot-product");var robustSum=require("robust-sum");module.exports=splitPolygon;module.exports.positive=positive;module.exports.negative=negative;function planeT(p,plane){var r=robustSum(robustDot(p,plane),[plane[plane.length-1]]);return r[r.length-1]}function lerpW(a,wa,b,wb){var d=wb-wa;var t=-wa/d;if(t<0){t=0}else if(t>1){t=1}var ti=1-t;var n=a.length;var r=new Array(n);for(var i=0;i<n;++i){r[i]=t*a[i]+ti*b[i]}return r}function splitPolygon(points,plane){var pos=[];var neg=[];var a=planeT(points[points.length-1],plane);for(var s=points[points.length-1],t=points[0],i=0;i<points.length;++i,s=t){t=points[i];var b=planeT(t,plane);if(a<0&&b>0||a>0&&b<0){var p=lerpW(s,b,t,a);pos.push(p);neg.push(p.slice())}if(b<0){neg.push(t.slice())}else if(b>0){pos.push(t.slice())}else{pos.push(t.slice());neg.push(t.slice())}a=b}return{positive:pos,negative:neg}}function positive(points,plane){var pos=[];var a=planeT(points[points.length-1],plane);for(var s=points[points.length-1],t=points[0],i=0;i<points.length;++i,s=t){t=points[i];var b=planeT(t,plane);if(a<0&&b>0||a>0&&b<0){pos.push(lerpW(s,b,t,a))}if(b>=0){pos.push(t.slice())}a=b}return pos}function negative(points,plane){var neg=[];var a=planeT(points[points.length-1],plane);for(var s=points[points.length-1],t=points[0],i=0;i<points.length;++i,s=t){t=points[i];var b=planeT(t,plane);if(a<0&&b>0||a>0&&b<0){neg.push(lerpW(s,b,t,a))}if(b<=0){neg.push(t.slice())}a=b}return neg}},{"robust-dot-product":1,"robust-sum":3}]},{},[]);var split=require("split-polygon");var poly=[[0,0],[1,0],[1,1],[0,1]];function line(a,b){var dx=a[0]-b[0];var dy=a[1]-b[1];var imag=1/Math.sqrt(dx*dx+dy*dy);var ndx=dx*imag;var ndy=dy*imag;var vx=ndx-a[1];var vy=ndy-a[0];var dist=ndy*vx-ndx*vy;return[-ndy,ndx,dist]}function debug(o){console.group("-");console.log("positive",o.positive.join(" - "));console.log("negative",o.negative.join(" - "));console.groupEnd("-")}debug(split(poly,line([0,0],[.25,.25])));debug(split(poly,line([.5,0],[.5,.25])));debug(split(poly,line([-1,0],[0,1])));debug(split(poly,line([-1,.75],[-.5,.75])));
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"split-polygon": "1.0.0"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- 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