|
Author |
Topic: Magnets and shavings (Read 535 times) |
|
David Mear
|
Magnets and shavings
« on: Sep 21st, 2003, 3:49pm » |
|
Not supposed to be an accurate representation of a magnet. I just thought it seemed a little like playing with magnets and iron shavings . http://www.davidmear.com/p55/Magshavings/ Can anyone point out any bugs/mistakes in my code? Specifically, sometimes the reset doesn't seem to reset all of the points. Also, I'd like to make the points that are further from the mouse react less (Make the power of the magnet decrease with distance). I tried simply dividing the movement by distance, but that produces strange results (which I think are something to do with it rounding down to 0). Can anyone help me out? Thanks!
|
|
|
|
elout
|
Re: Magnets and shavings
« Reply #1 on: Sep 21st, 2003, 6:42pm » |
|
I optimised it a bit for speed, like you draw while mousepressed, and after that again in the screen drawing routine. And deleted some variables that you weren`t using. I used drawing in the pixels directly, a bit faster then setpixel. Added a little circle-sizer with 'z/x' keys Code: // Magshavings p2 // Like playing with iron shavings and a magnet. // By David Mear // // changed a bit around By Elout de Kok int totalpoints = 40000; int[] pointx = new int[totalpoints]; int[] pointy = new int[totalpoints]; boolean[] pointtype = new boolean[totalpoints]; int speed = 3; int distx; int disty; float angle; float dist; int predcolor2,preycolor2; int circlesize=50; void setup() { size(300, 300); background(255,255,255); createpoints(); predcolor2 = ( ( 170 << 16) | ( 0 << 8) | 0); preycolor2 = ( ( 0 << 16) | ( 0 << 8) | 170); } void createpoints () { for (int i = 0; i<totalpoints; i++) { pointx[i] = (int)random(width); pointy[i] = (int)random(height); if (random(1) < 0.5) {pointtype[i] = false;} else { pointtype[i] = true;} } } void keyPressed () { if (key == 'r') { setup();} if (key == '+' || key == '=') { speed += 1;} if (key == '-' || key == '_') { speed -= 1;} if (key == 'z' || key == 'Z') { circlesize -= 1;} if (key == 'x' || key == 'X') { circlesize += 1;} } void loop() { //start mousecheck if (mousePressed == true) { for (int i = 0; i<totalpoints; i++) { distx = mouseX-pointx[i]; disty = mouseY-pointy[i]; dist = sqrt(sq(distx)+sq(disty)); angle =(atan2(disty,distx)); if (dist < circlesize) { if (pointtype[i] == true) { pointx[i] += (speed*cos(angle)); pointy[i] += (speed*sin(angle)); } else { pointx[i] -= (speed*cos(angle)); pointy[i] -= (speed*sin(angle)); } while(pointx[i] < 0) pointx[i] += width; while(pointx[i] > width - 1) pointx[i] -= width; while(pointy[i] < 0) pointy[i] += height; while(pointy[i] > height - 1) pointy[i] -= height; } } } // draw the points for (int i = 0; i<totalpoints; i++) { if (pointtype[i] == true) { pixels[(pointy[i]*width)+pointx[i]] = predcolor2; } else { pixels[(pointy[i]*width)+pointx[i]] = preycolor2; } } } |
|
|
|
|
|
David Mear
|
Re: Magnets and shavings
« Reply #2 on: Sep 21st, 2003, 10:25pm » |
|
Thanks elout! I'll look through your changes and see what I can learn.
|
|
|
|
|