How to draw a polygon, when x is getting bigger, its sides getting more

Here is my code. I wang polygon’s sided gets more when x gets bigger. But it looks incorrect. Can someone help me check it.

int points;
int radius=100;

void setup() {
size(500, 500);
points=mouseX/20;
}

void draw() {
drawPolygon();

}

void drawPolygon() {

float angleRange = TWO_PI / points;

for (int i = 0; i < points; i++) {
// First angle
float angle = i * angleRange;
// Next angle
float nextAngle = (i + 1) * angleRange;

line(width/2+cos(angle) * radius, height/2+ sin(angle) * radius, width/2+ cos(nextAngle) * radius, height/2+ sin(nextAngle) * radius);

}
}

please don’t start a new discussion with the same code---------------------------------

Remark

points=mouseX/20;

won’t work in setup()

place it in draw() instead

Remark

I got a good feeling about this.

Format your code nicely:

    line(
      width/2  + cos(angle)     * radius, 
      height/2 + sin(angle)     * radius, 
      width/2  + cos(nextAngle) * radius, 
      height/2 + sin(nextAngle) * radius
      );
1 Like

Thank you very much. I will pay attention next time.

1 Like

Can I ask one more question? Now I want to draw a chained mouse, but I dont know how to calculate the distance from the canvas’s center to the mouseX, mouseY;here is my code, I just wrote a little, also I want the circle follow the mouse:

final float CIRCLE_DIAMETER = 20;
final float TARGET_SPACING = CIRCLE_DIAMETER * 0.75;
float anchorX, anchorY;

void setup() {
size(500,500);
anchorX = width/2;
anchorY = height/2;
strokeWeight(2);
stroke(255);
noFill();
}

void draw() {

}

Mouse position is mouseX,mouseY

For a chain look at the lerp command in the reference

use “for” loop to draw a chain. The end circle position is mouseX and mouseY

Yes. Use for loop with lerp

See reference under lerp()

2nd example here: https://www.processing.org/reference/lerp_.html

Here is my new code, I calculate the distance between mouse and the center of canvas, but now I only can draw one circle on the canvas, how to draw a chain?

final float CIRCLE_DIAMETER = 20;
final float TARGET_SPACING = CIRCLE_DIAMETER * 0.75;
float anchorX, anchorY;
float xOffSet;
float yOffSet;
float distance;
float numbers;

void setup() {
size(500,500);
anchorX = width/2;
anchorY = height/2;
strokeWeight(2);
stroke(255);
noFill();
}

void draw() {
background(0);
xOffSet=abs(anchorX-mouseX);
yOffSet=abs(anchorX-mouseY);
distance=sqrt(xOffSetxOffSet+yOffSetyOffSet);
// println(distance);

numbers=round(distance/TARGET_SPACING);
// println(numbers);
for(int x=0;x<=numbers;x++);
circle(anchorX,anchorY,CIRCLE_DIAMETER);
}

I can understand lerp, but I am not allowed to use this in this program

No ; sign allowed after x++) !!!

you need to have

for(int x=0;x<=numbers;x++);  {  // error here 
     // calculate anchorX,anchorY here!!!
     circle(anchorX,anchorY,CIRCLE_DIAMETER);
} // end of for-loop

Chrisir

the anchorX and anchorY is the center of canvas, the first circle, I dont need to calculate.

I need to calculate how many circle I need to draw and I did. the problem is I cant keep drawing circles

ah, anchorX and anchorY is the start of the chain.

Where you click with the mouse is the end of the chain.

Store the end of the chain as targetX and targetY

The chain consists of links.

The link have certain distance to the next link (distLinkX and distLinkY you can call them)

in the for-loop :

  • Calculate the position for each link and display them. How can you do this?

the end of the chain is mouseX and mouseY, I have calculated the distance between beginning and end .
xOffSet=abs(anchorX-mouseX);
yOffSet=abs(anchorX-mouseY);
distance=sqrt(xOffSetxOffSet+yOffSetyOffSet);

And the number of circles is
numbers=round(distance/TARGET_SPACING);

How much is this?

Chrisir

CIRCLE_DIAMETER * 0.25?
is it correct?



anchor 
        x
          x
            x
              x
                x
                 Mouse

This is the chain with the links.

What is the step from one link to the next? It’s like this: “L”. This is distLinkX and distLinkY.

You calculate this in your for-loop please

Chrisir

I don’t know. Test it.