Skip to content

Instantly share code, notes, and snippets.

@justjake
Last active December 30, 2015 07:59
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 justjake/7799294 to your computer and use it in GitHub Desktop.
Save justjake/7799294 to your computer and use it in GitHub Desktop.
Power of anonymous functions

The relevent bits

Plotting from euclidean space to screen coordinates in Coffeescript, for my Spectrograph class

plot: (width, height, max_Y) ->
 # re-generate coeficceint functions
 if ! (@_width == width && @_height == height && @_max_Y == max_Y)
   w_coef = width / SIZE
   h_coef = height / max_Y # consider changing this to absolute w/height? nah
   # @X and @Y transform normal euclidean X, Y points into screen X Y points
   # for the given graph widht, height, maxY values
   @X = (x) -> w_coef * x
   @Y = (y) -> height - (h_coef * y)

 plotted = []
 i = 0
 for magnitude in @graph
   plotted.push make_pair(@X(i), @Y(magnitude))
   i++
   
 return plotted

It now looks like this, in Java:

class SpaceMap {
  float from_width,
    from_max_y,
    to_width,
    to_height;
    
  SpaceMap(float fw, float max_y, float tw, float th) {
    from_width = fw;
    from_max_y = max_y;
    to_width = tw;
    to_height = th;
  }
  
  float X(float x) {
    return (to_width/from_width) * x;
  }
  
  float Y(float y) {
    return to_height - (to_height / from_max_y) * y;
  }
}
// bla bla .......
PVector[] plot(float w, float h, float max_Y) {
  SpaceMap smap = new SpaceMap(size, max_Y, w, h);
  PVector[] plotted = new PVector[size];
  
  for (int i=0; i<size; i++) {
    plotted[i] = new PVector(smap.X(i), smap.Y(graph[i]));
  }
  
  return plotted;
}
/**
* map from euclidean coordinate space to screen coordinate space
* of a certain size.
*/
class SpaceMap {
float from_width,
from_max_y,
to_width,
to_height;
SpaceMap(float fw, float max_y, float tw, float th) {
from_width = fw;
from_max_y = max_y;
to_width = tw;
to_height = th;
}
float X(float x) {
return (to_width/from_width) * x;
}
float Y(float y) {
return to_height - (to_height / from_max_y) * y;
}
}
/**
* a graph
*/
class Spectrograph {
public int size = 64;
public float[] graph;
Spectrograph(float[] g, int s) {
size = s;
graph = g;
}
Spectrograph(int s) {
size = s;
graph = new float[s];
this.randomize(0.0, 1.0);
}
void mutate(float coeff) {
for (int i = 0; i < size; i++) {
graph[i] = random(0, coeff) * graph[i];
}
}
void randomize(float low, float high) {
for (int i=0; i < size; i++) {
graph[i] = random(low, high);
}
}
/**
* plot all points in the base graph into screen-space coorinates as
* an ordered array of line point PVectors
*/
PVector[] plot(float w, float h, float max_Y) {
SpaceMap smap = new SpaceMap(size, max_Y, w, h);
PVector[] plotted = new PVector[size];
for (int i=0; i<size; i++) {
plotted[i] = new PVector(smap.X(i), smap.Y(graph[i]));
}
return plotted;
}
void draw(float w, float h, float max_Y) {
PVector[] points = this.plot(w, h, max_Y);
for (int i=1; i<size; i++) {
line(points[i-1].x, points[i-1].y, points[i].x, points[i].y);
}
}
}
setup: ->
noLoop()
size(500, 500)
SIZE = 64 # columns in the spectrograph
make_pair = (_x, _y) -> {x: _x, y: _y}
# holds a 1-d matrix of a certain size
class Spectrograph
constructor: (values) ->
@graph = values
# retrieve float values interpolated from the nearby indexes
interpolate: (f) ->
floor = Math.floor(f)
ceil = Math.ceil(f)
dif = ceil - f
if dif == 0
return @graph[f]
return @graph[floor]*(1-dif) + @graph[ceil]*dif
mutate: (coef) ->
@graph = @graph.map (v) ->
Math.random() * coef * v
randomize: ->
@graph = @graph.map (v) -> Math.random()
plot: (width, height, max_Y) ->
# re-generate coeficceint functions
if ! (@_width == width && @_height == height && @_max_Y == max_Y)
w_coef = width / SIZE
h_coef = height / max_Y # consider changing this to absolute w/height? nah
console.log(w_coef, h_coef)
# @X and @Y transform normal euclidean X, Y points into screen X Y points
# for the given graph widht, height, maxY values
@X = (x) ->
res = w_coef * x
# console.log("x", x, res)
res
@Y = (y) ->
res = height - (h_coef * y)
console.log("y", y, res)
res
plotted = []
i = 0
for magnitude in @graph
plotted.push make_pair(@X(i), @Y(magnitude))
i++
return plotted
# draws the whole graph. translate before calling if you need it.
draw: (width, height, max_Y) ->
plot = @plot(width, height, max_Y)
console.log("plot", plot)
# start to point 0
# line(plot[0].x, @Y 0, plot[0].x, plot[0].y)
# stroke(0)
# all points
i = 1
while i < @graph.length
line(plot[i-1].x, plot[i-1].y, plot[i].x, plot[i].y)
stroke(0)
i++
# last to end
# line(plot[plot.length-1].x, plot[plot.length-1].y, plot[plot.length-1].x, @Y(0))
# stroke(0)
ar = []
i = 0
while i < SIZE
ar.push Math.random()
i++
@spectro = new Spectrograph(ar)
console.log(@spectro)
draw: ->
newMatrix()
@spectro.draw(500, 200, 1)
stroke(0)
mouseDragged: ->
@spectro.mutate(0.9)
redraw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment