I wrote a sketch using arrays and then tried to rewrite the sketch using OOP, thinking this would be less memory and processing intensive for my computer. But I am surprised that the OOP sketch is crashing.
I will post both sketches here.
and the OOP sketch is
Thanks,
Shane
I will post both sketches here.
- int numDots = 400;
- int amplitude = 30;
- float frequency = PI/30;
- float xPos, xPosLine;
- float vel = 0.01;
- int[]ArrayX = new int[numDots];
- int[]ArrayY = new int[numDots];
- float[]XsinVal = new float[numDots];
- float[]angle = new float[numDots];
- void setup() {
- size (600, 300);
- for (int i = 0; i < numDots; i++) {
- ArrayX[i] = (int) random(50, width-amplitude);
- ArrayY[i] = (int) random(30, height/2);
- }
- }
- void draw() {
- frameRate(24);
- background (33, 204, 232);
- noStroke();
- fill(24, 35, 234);
- for (int i = 0; i < numDots; i++) {
- fill(24, 35, 234);
- XsinVal[i] = sin(angle[i])*amplitude;
- ellipse(ArrayX[i] + sin(angle[i] + HALF_PI)*amplitude, ArrayY[i], 3, 3);
- if (xPos > ArrayX[i]) {
- angle[i] += frequency;
- }
- ellipse(ArrayX[i], XsinVal[i] + 230, 3, 3);
- stroke(250, 0, 0);
- fill(0);
- ellipse(xPos, 100, 3, 3);
- noStroke();
- fill(0);
- xPos += vel;
- stroke(0);
- line(35, 230, 570, 230);
- line(55, 180, 55, 280);
- }
- }
- boolean bStop;
- void mousePressed()
- {
- bStop = !bStop;
- if (bStop)
- noLoop();
- else
- loop();
- }
and the OOP sketch is
- int numSpots = 400;
- int amplitude = 30;
- float frequency = PI/30;
- //Declare and create the array
- Spot[] spots = new Spot[numSpots];
- void setup() {
- size(600, 300);
- smooth();
- noStroke();
- for (int i = 0; i < spots.length; i++) {
- float x = random(50, width-amplitude);
- float equilib = x;
- float xGlide = 10;
- float y = random(30, height/2);
- //Create each object
- spots[i] = new Spot(x, equilib, xGlide, y, 10, frequency);
- }
- }
- void draw() {
- fill(0);
- rect(0, 0, width, height);
- fill(255);
- for (int i = 0; i < spots.length; i++) {
- if (i==0) {
- spots[0].glide();
- spots[0].display();
- }
- else {
- spots[i].move(); //Move each object
- spots[i].display(); //Display each object
- }
- }
- }
- class Spot {
- float x, y, equilib, xGlide, diameter, frequency, angle;
- int direction = -1;
- //Constructor
- Spot(float xpos, float equilibpos, float xGlidepos, float ypos, float dia, float fr) {
- x = xpos;
- xGlide = xGlidepos;
- equilib = equilibpos;
- y = ypos;
- diameter = dia;
- frequency = fr;
- }
- void move() {
- if (equilib < spots[0].x) {
- x = equilib + (sin(angle))*amplitude;
- angle += frequency;
- println(angle);
- }
- }
- void display() {
- ellipse(x, y, diameter, diameter);
- }
- void glide() {
- xGlide += 1.0;
- x = xGlide;
- }
- }
- boolean bStop;
- void mousePressed()
- {
- bStop = !bStop;
- if (bStop)
- noLoop();
- else
- loop();
- }
Thanks,
Shane
1