Beginner having trouble with for loop.
in
Programming Questions
•
2 years ago
I am working on a sketch based on Example 10-10 from the Learning Processing book if anyone is familiar with it. My version creates a number or randomly positioned ellipses. An ellipse is also created at the cursor position. When the ellipse at the cursor touches a random ellipse, it should change color. The problem is that it only works with one of the random ellipses.
The stinker is that if I replace line 23 with:
- if (circle.intersect(randC[0]) || circle.intersect(randC[1]) || circle.intersect(randC[2])) {
it works how I want it to, the cursor ellipse changes color when it touches any of the random ellipses. Any suggestions would be greatly appreciated. Thanks!
- Circle circle;
- randCircle[] randC;
- int totalCircles = 3;
- void setup() {
- size(400, 400);
- circle = new Circle();
- randC = new randCircle[totalCircles];
- for (int i = 0; i < totalCircles; i++) {
- randC[i] = new randCircle(color(255,0,0));
- }
- frameRate = 30;
- smooth();
- }
- void draw() {
- background(200);
- for (int i = 0; i < totalCircles; i++) {
- randC[i].create();
- if (circle.intersect(randC[i])) {
- circle.c = 255;
- } else {
- circle.c = 0;
- }
- }
- circle.go();
- }
- class Circle {
- float[] xPos;
- float[] yPos;
- int drag,c;
- float r;
- Circle() {
- drag = 7;
- r = 15.5;
- c = 0;
- xPos = new float[drag];
- yPos = new float[drag];
- for (int i = 0; i < xPos.length; i++) {
- xPos[i] = 0;
- yPos[i] = 0;
- }
- }
- float xLoc() {
- xPos[0] = mouseX;
- for (int i = xPos.length-1; i >= 1; i--) {
- xPos[i] = xPos[i-1];
- }
- return xPos[xPos.length-1];
- }
- float yLoc() {
- yPos[0] = mouseY;
- for (int i = yPos.length-1; i >= 1; i--) {
- yPos[i] = yPos[i-1];
- }
- return yPos[yPos.length-1];
- }
- boolean intersect(randCircle rC) {
- float distance = dist(xLoc(),yLoc(),rC.x,rC.y);
- if (distance < r/2 + rC.r/2) {
- return true;
- } else {
- return false;
- }
- }
- void go() {
- noStroke();
- fill(c);
- ellipse(xLoc(),yLoc(),r,r);
- }
- }
- class randCircle {
- float r,x,y;
- color c;
- randCircle(int c_) {
- c = c_;
- r = 15;
- x = random(width);
- y = random(height);
- }
- void create() {
- noStroke();
- fill(c);
- ellipse(x,y,r,r);
- }
- }
1