Last active
June 22, 2019 07:07
-
-
Save josherich/3f83cb111e52eccf63da8093b254a5bb to your computer and use it in GitHub Desktop.
geometry
This file contains hidden or 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 rad2deg = 180/Math.PI | |
var deg2rad = Math.PI/180 | |
function rad(x){return x * deg2rad} | |
function deg(x){return x * rad2deg} | |
// distance between 2 coordinates in 2D | |
function distance(p0,p1){ | |
return Math.sqrt(Math.pow(p0[0]-p1[0],2) + Math.pow(p0[1]-p1[1],2)); | |
} | |
// map float from one range to another | |
function mapval(value,istart,istop,ostart,ostop){ | |
return ostart + (ostop - ostart) * ((value - istart)*1.0 / (istop - istart)) | |
} | |
// sigmoid curve | |
function sigmoid(x,k){ | |
k = (k != undefined) ? k : 10 | |
return 1/(1+Math.exp(-k*(x-0.5))) | |
} | |
// pseudo bean curve | |
// x^4 + x^2y^2 + y^4 = ax(x^2 + y^2) | |
// horizontal tangent (2/3a, +-2/3a) | |
// vertical tangent (0,0) (a,0) | |
function bean(x){ | |
return pow(0.25-pow(x-0.5,2),0.5)*(2.6+2.4*pow(x,1.5))*0.54 | |
} | |
// interpolate between square and circle | |
var squircle = function(r,a){ | |
return function(th){ | |
while (th > PI/2){ | |
th -= PI/2 | |
} | |
while (th < 0){ | |
th += PI/2 | |
} | |
return r*pow(1/(pow(cos(th),a)+pow(sin(th),a)),1/a) | |
} | |
} | |
// mid-point of an array of points | |
function midPt(){ | |
var plist = (arguments.length == 1) ? | |
arguments[0] : Array.apply(null, arguments) | |
return plist.reduce(function(acc,v){ | |
return [v[0]/plist.length+acc[0], | |
v[1]/plist.length+acc[1], | |
v[2]/plist.length+acc[2]] | |
},[0,0,0]) | |
} | |
// rational bezier curve | |
function bezmh(P, w){ | |
w = (w == undefined) ? 1 : w | |
if (P.length == 2){ | |
P = [P[0],midPt(P[0],P[1]),P[1]]; | |
} | |
var plist = []; | |
for (var j = 0; j < P.length-2; j++){ | |
var p0; var p1; var p2; | |
if (j == 0){p0 = P[j];}else{p0 = midPt(P[j],P[j+1]);} | |
p1 = P[j+1]; | |
if (j == P.length-3){p2 = P[j+2];}else{p2 = midPt(P[j+1],P[j+2]);} | |
var pl = 20; | |
for (var i = 0; i < pl+(j==P.length-3); i+= 1){ | |
var t = i/pl; | |
var u = (Math.pow (1 - t, 2) + 2 * t * (1 - t) * w + t * t); | |
plist.push([ | |
(Math.pow(1-t,2)*p0[0]+2*t*(1-t)*p1[0]*w+t*t*p2[0])/u, | |
(Math.pow(1-t,2)*p0[1]+2*t*(1-t)*p1[1]*w+t*t*p2[1])/u, | |
(Math.pow(1-t,2)*p0[2]+2*t*(1-t)*p1[2]*w+t*t*p2[2])/u]); | |
} | |
} | |
return plist; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment