trouble bouncing pulses of the canvas
in
Programming Questions
•
1 years ago
here is my code and how do i bounce the pulse of array of the canvas. Also how can i make this draw inifinite forever and ever.
// ----------------------------------------------------------------------
// CUSTOM CLASSES
// ----------------------------------------------------------------------
//Pulse is a class that draws a pulsing dot on the screen
class Pulse {
/* properties */
int SCALE = 50;
int x; // x-coordinate of the pulse
int y; // y-coordinate of the pulse
float s; // size of the pulse
color myColor = color(255,255,255); //default color
/* methods */
// constructor 1
public Pulse(int pulseX, int pulseY) {
x = pulseX;
y = pulseY;
s = 5;
}
// constructor 2
public Pulse(int pulseX, int pulseY, int pulseScale) {
x = pulseX;
y = pulseY;
SCALE = pulseScale;
s = 0;
}
// constructor 3
public Pulse() {
x = 0;
y = 0;
s = 0;
}
// constructor 4
public Pulse(int pulseX, int pulseY, int pulseScale, color newColor) {
x = pulseX;
y = pulseY;
SCALE = pulseScale;
s = 0;
myColor = newColor;
}
// moves the pulse to the given position
void move(int newX, int newY) {
x = newX;
y = newY;
}
// draws the pulse and updates its size
void display() {
fill(myColor);
stroke(0);
ellipse(x, y, sin(s)*SCALE, sin(s)*SCALE);
s += 0.1;
}
}
// ----------------------------------------------------------------------
// GLOBAL VARIABLES
// ----------------------------------------------------------------------
int numPulses = 550;
//counter tracking how many pulse objects have been istantiated
int pulseArrayCounter = 0;
//create of any array type pulse pulse array with 1000 elements
Pulse[] pulseArray = new Pulse[numPulses];
//int pulseGap = 50;
// ----------------------------------------------------------------------
// BUILT-IN FUNCTIONS
// ----------------------------------------------------------------------
void setup() {
size(800, 800);
smooth();
color tempColor = color(random(255), random(255), random(255));
}
void draw() {
background(0);
// draw the pulse
for (int index = 0; index < pulseArrayCounter; index++) {
pulseArray[index].display();
}
}
void mouseDragged() {
color tempColor = color(random(255), random(255), random(255));
int newScale = int(random(100));
// initialize a new pulse object
// pulseArray[pulseArrayCounter] = new Pulse(mouseX, mouseY, newScale, tempColor);
// pulseArrayCounter++;
if (pulseArrayCounter < pulseArray.length) {
pulseArray[pulseArrayCounter] = new Pulse(mouseX, mouseY, newScale, tempColor);
pulseArrayCounter++;
}
}
// ----------------------------------------------------------------------
// CUSTOM CLASSES
// ----------------------------------------------------------------------
//Pulse is a class that draws a pulsing dot on the screen
class Pulse {
/* properties */
int SCALE = 50;
int x; // x-coordinate of the pulse
int y; // y-coordinate of the pulse
float s; // size of the pulse
color myColor = color(255,255,255); //default color
/* methods */
// constructor 1
public Pulse(int pulseX, int pulseY) {
x = pulseX;
y = pulseY;
s = 5;
}
// constructor 2
public Pulse(int pulseX, int pulseY, int pulseScale) {
x = pulseX;
y = pulseY;
SCALE = pulseScale;
s = 0;
}
// constructor 3
public Pulse() {
x = 0;
y = 0;
s = 0;
}
// constructor 4
public Pulse(int pulseX, int pulseY, int pulseScale, color newColor) {
x = pulseX;
y = pulseY;
SCALE = pulseScale;
s = 0;
myColor = newColor;
}
// moves the pulse to the given position
void move(int newX, int newY) {
x = newX;
y = newY;
}
// draws the pulse and updates its size
void display() {
fill(myColor);
stroke(0);
ellipse(x, y, sin(s)*SCALE, sin(s)*SCALE);
s += 0.1;
}
}
// ----------------------------------------------------------------------
// GLOBAL VARIABLES
// ----------------------------------------------------------------------
int numPulses = 550;
//counter tracking how many pulse objects have been istantiated
int pulseArrayCounter = 0;
//create of any array type pulse pulse array with 1000 elements
Pulse[] pulseArray = new Pulse[numPulses];
//int pulseGap = 50;
// ----------------------------------------------------------------------
// BUILT-IN FUNCTIONS
// ----------------------------------------------------------------------
void setup() {
size(800, 800);
smooth();
color tempColor = color(random(255), random(255), random(255));
}
void draw() {
background(0);
// draw the pulse
for (int index = 0; index < pulseArrayCounter; index++) {
pulseArray[index].display();
}
}
void mouseDragged() {
color tempColor = color(random(255), random(255), random(255));
int newScale = int(random(100));
// initialize a new pulse object
// pulseArray[pulseArrayCounter] = new Pulse(mouseX, mouseY, newScale, tempColor);
// pulseArrayCounter++;
if (pulseArrayCounter < pulseArray.length) {
pulseArray[pulseArrayCounter] = new Pulse(mouseX, mouseY, newScale, tempColor);
pulseArrayCounter++;
}
}
1