From 171a5b6e0c4f39cbb23014e186eb304930524790 Mon Sep 17 00:00:00 2001 From: Francisco Dans Date: Wed, 25 Feb 2015 18:01:38 +0100 Subject: [PATCH] optimizes horizontalLine --- lib/torque/renderer/ball.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/torque/renderer/ball.js b/lib/torque/renderer/ball.js index 6789e50..cccd386 100644 --- a/lib/torque/renderer/ball.js +++ b/lib/torque/renderer/ball.js @@ -15,26 +15,28 @@ function BallRenderer(thecanvas){ this.drawnTemp = {}; this.prof = 0; this.gradient = {}; + this.ballsSoFar = 0; + this.balls = 0; + this.cumulative = true; } BallRenderer.prototype = { addBall: function(x0, y0){ var orad = this.radius; - var increment = this.map_range(1, 0, orad,0,255); var x = this.radius; var y = 0; var radiusError = 1 - x; while (x >= y){ // Try not to touch the following, it's a pain in the ass to write - this.horizontalLine(-x + x0, y + y0, x + x0, y + y0, x0,y0); - this.horizontalLine(-y + x0, -x + y0, y + x0, -x + y0, x0,y0); - this.horizontalLine(-x + x0, -y + y0, x + x0, -y + y0, x0,y0); - this.horizontalLine(-y + x0, x + y0, y + x0, x + y0, x0,y0); - y++; + this.horizontalLine(-x + x0, x + x0, y + y0, x0,y0); + this.horizontalLine(-y + x0, y + x0, -x + y0, x0,y0); + this.horizontalLine(-x + x0, x + x0, -y + y0, x0,y0); + this.horizontalLine(-y + x0, y + x0, x + y0, x0,y0); + ++y; if (radiusError<0){ radiusError += 2 * y + 1; } else{ - x--; + --x; radiusError += 2 * (y - x) + 1; } } @@ -44,18 +46,20 @@ BallRenderer.prototype = { var indexPos = this.getRIndexPos(x, y); this.pointLayer[indexPos + 3] = this.pointLayer[indexPos + 3] + alpha; }, - horizontalLine: function(xi, yi, xf, yf, x0, y0){ - function lineDistance(x,y){ + map_range: function(value, low1, high1, low2, high2) { + return low2 + (high2 - low2) * (value - low1) / (high1 - low1); + }, + lineDistance: function(x,y,x0,y0){ var xs = Math.pow(x - x0, 2); var ys = Math.pow(y - y0, 2); return Math.sqrt( xs + ys ); - } + }, + horizontalLine: function(xi, xf, yi, x0, y0){ // Assumes xi is on the left and xf is on the right if(typeof this.drawnTemp[yi] === "undefined"){ while (xi <= xf){ - var alpha = this.map_range(lineDistance(xi, yi), 0, this.radius, 30, 0); - this.addPoint(xi, yi, alpha); - xi ++; + this.addPoint(xi, yi, 30 - ((30 * this.lineDistance(xi, yi,x0,y0)) / this.radius)); + ++xi; } this.drawnTemp[yi]=true; }