I fixed my code following comments, but still have some error messages left about too much recursion

edited September 2017 in Questions about Code

Hello Forum, Really appreciate for y'all comments on mine. Based off of the comments left from forum, I revised some of my code. But still facing the error messages about too much recursion. And I really don't know why I got an error message saying rect (x,y,w,h) also pops me up the same error message.

Can I ask forum to let me know how to fix this problem? My modified sketch code and class codes are as below.

=Sketch=

Circle c;

Rectangle[] rects = new Rectangle[8];


void setup() {
  size(600,400);
  c = new Circle(0,0,30);
  for (int i=0; i<rects.length; i++) {
    float x = int(random(50,width-50)/50) * 50;
    float y = int(random(50,height-50)/50) * 50;
    rects[i] = new Rectangle(x,y, 50,50);
  }
}


void draw() {
  background(255);


  for (Rectangle r : rects) {
    r.checkCollision(c);  
    r.display();               
  }


  c.update();
  c.display();
}

=Circle Class=

class Circle {
  float x, y;    // position
  float r;       // radius

  Circle (float tempX, float tempY, float tempR) {
    x = tempX;
    y = tempY;
    r = tempR;
  }

  // move into mouse position
  void update() {
    x = mouseX;
    y = mouseY;
  }

  // draw
  void display() {
    fill(0, 150);
    noStroke();
    ellipse(x,y, r*2, r*2);
  }



}

=Rectangle Class=

class Rectangle {
  float x, y;            // position
  float w, h;            // size
  boolean hit = false;   // is it hit?
  boolean circleRect(float cx, float cy, float radius, float rx, float ry, float rw, float rh) {


  float testX = cx;
  float testY = cy;


  if (cx < rx)         testX = rx;      // compare left edge
  else if (cx > rx+rw) testX = rx+rw;   // right edge
  if (cy < ry)         testY = ry;      // top edge
  else if (cy > ry+rh) testY = ry+rh;   // bottom edge


  float distX = cx-testX;
  float distY = cy-testY;
  float distance = sqrt( (distX*distX) + (distY*distY) );

  // if the distance is less than the radius, collision!
  if (distance <= radius) {
    return true;
  }
  return false;
}

  Rectangle (float tempX, float tempY, float tempW, float tempH) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
  }

  // draw the rectangle
  // if hit, change the fill color
  void display() {
    if (hit) {
    fill(255,150,0);
    }
    else{ 
    fill(0,150,255);
    noStroke();
    rect(x,y, w,h);
  }
  for (Rectangle r : rects) {
  r.checkCollision(c);  // check for collision
  r.display();               // and draw
}
  }
   void checkCollision(Circle c) {
  hit = circleRect(c.x,c.y,c.r, x,y,w,h);
}
}
Tagged:

Answers

  • @soonk -- re:

    still facing the error messages
    I got an error message

    Please post the exact error message text that you are getting. Do they indicate a line of code each error is happening on? What is happening on that line?

  • Answer ✓

    line 49 in class Rectangle {

     r.display();               // and draw
    

    this is an recursion

    imho you shouldn't loop over the array of a class inside a class in the first place.

    loop over it in a new function outside draw and outside the class

  • Answer ✓

    I have delete the duplicate discussion and reopened this one in case there is more to say.

    DO NOT CREATE DUPLICATE DISCUSSIONS.

    If you have further information about a problem add it to the original discussion. You had an answer on the other discussion but it had already been provided in this discussion so you effectively wasted the respondent's time.

Sign In or Register to comment.