I just studied user defined function, got some trouble

I am trying to draw a picture like this.115æˆȘć›Ÿ20201113170735
here is my code. I just start it.

void setup() {
  size(800, 800);
  noStroke();     //No black outline around the circles
}

void draw() {
  final int CIRCLE_SIZE = 20;
  final int NUM_CIRCLES = 7;
  final float ANGLE = -PI/4;
  background(0);
  lineOfCircles(mouseX,mouseY,ANGLE,CIRCLE_SIZE,NUM_CIRCLES);
}

void lineOfCircles(float xStart, float yStart, float angle, float size, int numCirc){

}

void drawCircle(float x,float y, float d,float theta, float diam){
 for(int i=0;i<NUM_CIRCLES;i++){

   circle(x,y,diam);
}
}


float xAtAngle(float xFrom,float dist, float angle){
return  xFrom+cos(angle)*dist;
}
float yAtAngle(float yFrom,float dist, float angle){
return yFrom+sin(angle)*dist;
}


Hi @mengtong

  • First, you need to take the three final variables out of the draw() function and place them above setup() as global.
  • Second, there is no code in the lineOfCircles(), so nothing will happen.
  • Third, if you use drawCircle() in draw() instead the for() loop has no functionality, so thus only one sphere will appear.

Sorry, I did not finish the code. I dont know how to write. And in this question, I can not move those constants, and also I cant create any global variables

Why not? As they are final they will not change anymore. So you put them on the top of your sketch, where they are considered as global.

This is a question from old exam, I am not allowed to use those constants in others function. They are local constants

I just checked your earlier post, where you have these constants at the right place. You are trolling.

I don’t think this is necessary

I don’t think that’s correct. I worked together with him very nicely.


//

void setup() {
  size(800, 800);
  noStroke();     //No black outline around the circles
}

void draw() {
  final int CIRCLE_SIZE = 20;
  final int NUM_CIRCLES = 7;
  final float ANGLE = -PI/4;

  // background(0);
  lineOfCircles(mouseX, mouseY, ANGLE, CIRCLE_SIZE, NUM_CIRCLES);
}

void lineOfCircles(float xStart, float yStart, float angle, float size, int numCirc) {
  for (int i=0; i<numCirc; i++) {

    float x1=xAtAngle(xStart+i*size, 13, angle);
    float y1=yAtAngle(yStart+i*size, 13, angle);

    ellipse(x1, y1, 
      size, size);
  }
}

//void drawCircle(float x, float y, float d, float theta, float diam) {
//}


float xAtAngle(float xFrom, float dist, float angle) {
  return  xFrom+cos(angle)*dist;
}

float yAtAngle(float yFrom, float dist, float angle) {
  return yFrom+sin(angle)*dist;
}

Oh, then I apologize. But I don’t understand why one would put final variables in draw().

1 Like

Just for me to get it correct I searched for code containing final variables in draw(); and I found a very nice code from @GoToLoop here at the bottom of the post. But I really think he used it there solely out of ease.
In an article about “The Final Word On the final Keyword” here it states:

1 Like

Because I am a student, and this is a question from my instructor, she said that I can not change those constants. I just learned a new thing which called user defined function. My instructor just want us to practice the new thing.

But, I know you are right. it’s just that in this case, we are not allowed to do that

I’ve got an old example on the previous forum where the anonymous instantiated class TimerTask access the final parameter sec from the function createScheduleTimer(): :nerd_face:

Such local variables/parameters are known as closures in the JS world & are rare to find in Java: :coffee:

1 Like

Thanks for your help. I have modified a little code, but the result is not the same as what I want115æˆȘć›Ÿ20201114115942

void setup() {
  size(800, 800);
  noStroke();     //No black outline around the circles
}

void draw() {
  final int CIRCLE_SIZE = 20;
  final int NUM_CIRCLES = 7;
  final float ANGLE = -PI/4;

   background(0);
  lineOfCircles(mouseX, mouseY, ANGLE, CIRCLE_SIZE, NUM_CIRCLES);
}

void lineOfCircles(float xStart, float yStart, float angle, float size, int numCirc) {
  for (int i=0; i<numCirc; i++) {

    float x1=xAtAngle(xStart+i*size, 13, angle);
    float y1=yAtAngle(yStart-i*size, 13, angle);
    ellipse(x1, y1, size, size);
  }
}

//void drawCircle(float x, float y, float d, float theta, float diam) {
//}

float xAtAngle(float xFrom, float dist, float angle) {
  return  xFrom+cos(angle)*dist;
}

float yAtAngle(float yFrom, float dist, float angle) {
  return yFrom+sin(angle)*dist;
}

In your code, the first circle’s center should stay with mouse’s center. I should set 13 to 0, then the first circle’s center gonna stay with mouse’s center. But I dont think it is correct. Also if I want to use another function which call void drawCircle, you commented it. How can I use it? I mean I will draw all the circles in void drawCircle function, and call this function from void lineOfCircles function.

1 Like

I guess you need to move the code from inside the for loop into drawCircle function and then call this function from inside the for loop please

You can replace the 13 with 13*i

Why is there a distance in the middle of each circle? I don’t understand why.

Maybe replace 13 with circle_size

This is a copy of my question, I dont know where the float d comes from.

void setup() {
  size(800, 800);
  noStroke();     //No black outline around the circles
}

void draw() {
  final int CIRCLE_SIZE = 20;
  final int NUM_CIRCLES = 7;
  final float ANGLE = -PI/4;
  background(0);
  lineOfCircles(mouseX, mouseY, ANGLE, CIRCLE_SIZE, NUM_CIRCLES);
}

void lineOfCircles(float xStart, float yStart, float angle, float size, int numCirc) {
  for (int i=0; i<numCirc; i++) {
    float  nextX=xStart+i*cos(angle)*size;
    float  nextY= yStart+i*sin(angle)*size;
    circle(nextX, nextY, size);
  }
} 


I use another way to do this question is very easy. But in this way, I feel confused.

d is just the distance of the circle from point x,y