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;
}