We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › object location optimization
Page Index Toggle Pages: 1
object location optimization (Read 471 times)
object location optimization
Nov 5th, 2005, 10:56am
 
Hi all,

I've written a small sketch, called hotspots. It visualizes hotspots, areas where the mousepointer crosses often over time. Theres a grid with initially white squares. When the mouse passes over or near a square it shifts color from white to green then yellow and red. Similar to a thermal heat image.

The sketch works as it should do, but it feels sluggish. This is probably due to the fact, that I'm constantly checking all squares in the grid if they're within a defined distance to the mousepointer. The whole process could be sped up if I could ignore all the squares, except the ones which are within the "color-shifting" distance.
That would probably require restructuring the code. Instead of defining locations within the objects it should be possible to place all objects using a 2d array (e.g. locations[x]][y]). I see how I'd be possible to identify the one object lying exactly under the mouseloc, but I can't imagine how I could contact the surrounding objects.

Anyone got a hint Is it clear what I'm trying to do

cheers.
Re: object location optimization
Reply #1 - Nov 6th, 2005, 3:08pm
 
How about instead of running a check from every square, run a check close to the mouse. This gives you a square area to check. Then you can ask if those points are within the circular distance to the mouse pointer, giving you the round check zone.  The function I've written looks pretty exhaustive but it's dealing with a lot of stuff that you could initialise before hand, and I'm making sure the checking square doesn't step outside the array.
Code:

int _width = 500; // stagewidth
int _height = 500; // stageheight
int cellSize = 10; // resolution
int _w = _width/cellSize;
int _h = _height/cellSize;
int MAX = int(sq(_w)); // calculate number of cells
int near = 30; // affected area from mousepointer
float as = 15; // ageing speed
Cell [] cells = new Cell[MAX];
void setup() {
size(500,500);
background(255);
noStroke();
for (int i = 0; i < MAX; i++)
cells[i] = new Cell((i%_w)*cellSize, (i/_w)*cellSize, cellSize);
rectMode(CENTER);
}
void draw() {
background(255);
for (int i = 0; i < MAX; i++) {
cells[i].draw();
}
burn(int(mouseX/cellSize),int(mouseY/cellSize),near/cellSize);
}
void burn(int x, int y, int radius){
//cast square radius
int topLeftX = x - radius;
int topLeftY = y - radius;
int bottomRightX = x + radius;
int bottomRightY = y + radius;
if(topLeftX < 0) topLeftX = 0;
if(topLeftY < 0) topLeftY = 0;
if(bottomRightX >= _w) bottomRightX = _w - 1;
if(bottomRightY >= _h) bottomRightY = _h - 1;
int wide = bottomRightX - topLeftX;
int high = bottomRightY - topLeftY;
int cellsInside = wide * high;
int [] burnCells = new int [cellsInside];
int i = 0;
for(int row = topLeftX + topLeftY * _w; row < topLeftX + (topLeftY + high) * _w; row += _w){
for(int col = 0; col < wide; col++){
burnCells[i++] = row + col;
}
}
for(i = 0; i < burnCells.length; i++){
cells[burnCells[i]].stoke();
}
}
class Cell {
float xpos;
float ypos;
int xsize;
int ysize;
float r = 255;
float g = 255;
float b = 255;
Cell(float xp, float yp, int _size) {
xpos = xp;
ypos = yp;
xsize = _size;
ysize = xsize;
}
void draw () {
fill(r,g,b);
rect(xpos,ypos,xsize,ysize);
}
void stoke(){
if ((dist(xpos, ypos, mouseX, mouseY) < near) && (b >= 0)) {
r-=as;
b-=as;
}
if ((dist(xpos, ypos, mouseX, mouseY) < near) && (b < 0)) {
r+=as;
}
if ((dist(xpos, ypos, mouseX, mouseY) < near) && (b < 0) && (r >= 255) ) {
g-=as;
}
}
}
Page Index Toggle Pages: 1