Skip to content

# dethe/obloq

### Subversion checkout URL

You can clone with
or
.

fix for sketchy

• Loading branch information...
commit a037f0043b80ebb1c105c796ed1f481a50d0ad52 1 parent 234136c
authored
Showing with 99 additions and 95 deletions.
1. +99 −95 lib/sketchy.js
194 lib/sketchy.js
 @@ -1,3 +1,102 @@ +// UTILITIES + +var \$u = { + removeItem: function(list, item){ + list.splice(list.indexOf(item), 1); + }, + dcos: function(degrees){ + return Math.cos(Raphael.rad(degrees)); + }, + dsin: function(degrees){ + return Math.sin(Raphael.rad(degrees)); + }, + random: function(a,b){ + // 'Returns an integer between a and b, inclusive'; + // 'If b is not specified, returns an integer between 0 and a'; + if (b === undefined){ + b = a; + a = 0; + } + return Math.floor(Math.random() * (b-a + 1)) + a; + }, + choice: function(list){ + // This is an exclusive, or mutating choice that + // picks a random item from a list and removes that + // item before returning it + var idx = \$u.random(0, list.length - 1); + var item = list[idx]; + list.splice(idx, 1); // remove item from list + return item; + }, + range: function(start, stop, step){ + // similar to Python's range function + // should be extended to handle negative steps + if (stop === undefined){ + stop = start; + start = 0; + } + if (step === undefined){ + step = 1; + } + var r = [], i; + for(i = start; i < stop; i += step){ + r.push(i); + } + return r; + }, + iterrange: function(start, stop, step, func){ + var r = \$u.range(start, stop, step); + for (var i in r){ + func(r[i]); + } + }, + intarray: function(array){ + return \$.map(array, \$u.integ); + }, + integ: function(str){ + return parseInt(str, 10); + }, + nudge: function(pt){ + return [pt[0] + \$u.random(-2,2), pt[1] + \$u.random(-2, 2)]; + }, + ellipse: function(x,y,w,h,steps){ + /* + * This functions returns an array containing points to draw an + * ellipse in the rect defined by x,y,w,h + */ + w = w/2; + h = h/2; + x = x + w; + y = y + h; + if (steps === undefined){ + steps = 36; + } + var points = []; + var a = w, // semi-major axis + b = h, // semi-minor axis + beta = 0; + if (w > h){ + a = h; + b = w; + beta = -Math.PI / 2; + } + var sinbeta = Math.sin(beta); + var cosbeta = Math.cos(beta); + var i, alpha, sinalpha, cosalpha,X,Y; + for (i = 0; i < 360; i += 360 / steps) { + alpha = i * (Math.PI / 180) ; + sinalpha = Math.sin(alpha); + cosalpha = Math.cos(alpha); + X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta); + Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta); + + points.push([X,Y]); + } + return points; + } +}; + +// Raphael Extensions Raphael.fn.line = function(x1,y1, x2,y2){ var path = new Path() .moveto(x1, y1) @@ -339,101 +438,6 @@ Sketch.prototype.draw = function(){ return this; }; -var \$u = { - removeItem: function(list, item){ - list.splice(list.indexOf(item), 1); - }, - dcos: function(degrees){ - return Math.cos(Raphael.rad(degrees)); - }, - dsin: function(degrees){ - return Math.sin(Raphael.rad(degrees)); - }, - random: function(a,b){ - // 'Returns an integer between a and b, inclusive'; - // 'If b is not specified, returns an integer between 0 and a'; - if (b === undefined){ - b = a; - a = 0; - } - return Math.floor(Math.random() * (b-a + 1)) + a; - }, - choice: function(list){ - // This is an exclusive, or mutating choice that - // picks a random item from a list and removes that - // item before returning it - var idx = \$u.random(0, list.length - 1); - var item = list[idx]; - list.splice(idx, 1); // remove item from list - return item; - }, - range: function(start, stop, step){ - // similar to Python's range function - // should be extended to handle negative steps - if (stop === undefined){ - stop = start; - start = 0; - } - if (step === undefined){ - step = 1; - } - var r = [], i; - for(i = start; i < stop; i += step){ - r.push(i); - } - return r; - }, - iterrange: function(start, stop, step, func){ - var r = \$u.range(start, stop, step); - for (var i in r){ - func(r[i]); - } - }, - intarray: function(array){ - return \$.map(array, \$u.integ); - }, - integ: function(str){ - return parseInt(str, 10); - }, - nudge: function(pt){ - return [pt[0] + \$u.random(-2,2), pt[1] + \$u.random(-2, 2)]; - }, - ellipse: function(x,y,w,h,steps){ - /* - * This functions returns an array containing points to draw an - * ellipse in the rect defined by x,y,w,h - */ - w = w/2; - h = h/2; - x = x + w; - y = y + h; - if (steps === undefined){ - steps = 36; - } - var points = []; - var a = w, // semi-major axis - b = h, // semi-minor axis - beta = 0; - if (w > h){ - a = h; - b = w; - beta = -Math.PI / 2; - } - var sinbeta = Math.sin(beta); - var cosbeta = Math.cos(beta); - var i, alpha, sinalpha, cosalpha,X,Y; - for (i = 0; i < 360; i += 360 / steps) { - alpha = i * (Math.PI / 180) ; - sinalpha = Math.sin(alpha); - cosalpha = Math.cos(alpha); - X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta); - Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta); - - points.push([X,Y]); - } - return points; - } -}; // Raphael cheat sheet: // paper.circle(x,y,radius) // paper.rect(x,y,width,height[,corner_radius])
Please sign in to comment.
Something went wrong with that request. Please try again.