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() {
}
Chrisir
October 29, 2020, 12:05am
5
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
Chrisir
October 29, 2020, 12:24am
7
Yes. Use for loop with lerp
See reference under lerp()
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+yOffSet yOffSet);
// 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
Chrisir
October 29, 2020, 5:19pm
11
No ; sign allowed after x++) !!!
Chrisir
October 29, 2020, 5:23pm
12
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
Chrisir
October 29, 2020, 5:29pm
15
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+yOffSet yOffSet);
And the number of circles is
numbers=round(distance/TARGET_SPACING);
CIRCLE_DIAMETER * 0.25?
is it correct?
Chrisir
October 29, 2020, 5:37pm
19
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