Draw this Polygon using loops?

Its from a textbook and I just need a solution so I can get an idea on how to use loops

If you want to get an idea on how to use loops, check out the processing reference. You can find the section on for loops here. Also, post your attempt at your question first if you need more help.

int points= 9; 
float radius = 50; 
float angle = 0;


void setup() {
  size(600, 600); 
  strokeWeight(3);
}

void draw() {
  background(0);
  for (int i = 0; i <= points; i++) {
    float x = cos(i*angle) * radius;
    float y = sin(i*angle) * radius;
    
 
}}

I suck at this i know

to processsing, iangle isnt a thing yet. if you wanted to multiply i and angle, you would use i*angle
you would then have to draw the point, (there is a point() you can check the reference for that) then I (there are many ways of doing everything) would find a way to draw a line() (reference page) to each of those points linking to another.
Im not one for cos and sin as I haven’t learnt that in school yet
Hopefully this helped


Doge

Not at all. This is good. As doge mentioned, make sure you’re multiplying i * angle. But angle right now is zero. If the full circle is 360 degrees or TWO_PI radians, then angle should be a fraction of that. I’m sure you could do the math for that. (Processing sin() and cos() functions take in radian values)

Now when you figure out your x and y values, here’s a hint on how to display it.

int points = 9; 
float radius = 100;  
float angle = TWO_PI / points;


void setup() {
  size(600, 600); 
  strokeWeight(3);
}


void draw() {
  background(0);
  noFill();
  stroke(255);

 

  
  beginShape();
  for (int i = 0; i <= points; i++) {
    float x = cos(i*angle) * radius;
    float y = sin(i*angle) * radius;
    vertex(x, y);
  }
  endShape();
}

Drawn the polygon but I dont get the mouse part

Great. Before you move on to the mouse part, you haven’t moved the shape to the center of canvas yet. You can do that by adding width/2 to x and height/2 to y.

The mouse part is rather easy. It’s asking you to assign points inside void draw(){} to the value of mouseX/20. Try doing that and see what happens to the polygon as you move your mouse horizontally.

You guys are all getting confused because this person isn’t posting their code using the forum’s code formatting tool. The asterisk between i and angle is being interpreted as “start using italics”!

Edit your post.
Select your code.
Press the </> button!

Im not getting confused but lol ok it is still a good practice to use the </>for typing code
:stuck_out_tongue:

u get your answer or not???

i have the same lab , i need help to done this

How to draw regular polygons using loops is one of the standard examples on the Processing website. You can find it by looking at the Examples page and searching for “polygon.”

https://processing.org/examples/regularpolygon.html

Notice that the approach may not be exactly the same as in your book, because there are several ways of approaching this problem. For example:

The example for loop increments by angle – but you could also use the for loop to count up by points, and compute each angle inside the loop.

For output, the example also uses beginShape / vertex / endShape, but you could instead draw points, or store the previous point and simply draw a series of point-point lines using line() in your loop.

Hi, the key here is use polar coordinates. I think this coding challenge (https://www.youtube.com/watch?v=u2D4sxh3MTs) could be useful to you

Thx it can be helpful for me

void setup() {
  size(640, 360);
}

void draw() {
  background(102);
  
  pushMatrix();
  translate(width*0.2, height*0.5);
  rotate(frameCount / 200.0);
  polygon(0, 0, 82, 3);  // Triangle
  popMatrix();
  
  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 50.0);
  polygon(0, 0, 80, 20);  // Icosahedron
  popMatrix();
  
  pushMatrix();
  translate(width*0.8, height*0.5);
  rotate(frameCount / -100.0);
  polygon(0, 0, 70, 7);  // Heptagon
  popMatrix();
}

void polygon(float x, float y, float radius, int npoints) {
  float angle = TWO_PI / npoints;
  beginShape();
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius;
    float sy = y + sin(a) * radius;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}

how to add mouse movement in this/////???

That’s just a cut-paste of the example from Regular Polygon / Examples / Processing.org .

You haven’t even changed it by removing two of the shapes, or by adding more sides. Do you want to try editing the example first to make it do what you want?

You can move things with translate() – you can move them to the mouse location with mouseX, mouseY.

int points=9;
void setup()
{
  size(600, 600);
  background(0);
}
 void draw()
{
  float radio=70;
  for (int k=points;k<points+1;k++)
  {
    int numberOfSides=k;
    float centerPositionX=(k-points+1)*2.2*radio-radio/2;
    float centerPositionY=height/2;
    drawPolygon(radio, centerPositionX, centerPositionY, numberOfSides);
  }
}
 
void drawPolygon(float radio, float centerPositionX, float centerPositionY, int numberOfSides)
{  

  float sideAngle=TWO_PI/numberOfSides;
  float halfSideLineLength=sin(sideAngle/2)*radio;
  float lineY=cos(sideAngle/2)*radio;
  strokeWeight(1);
  stroke(255); 
  for (int i=0;i<numberOfSides;i++)
  {    
    pushMatrix();
    translate(centerPositionX, centerPositionY);    
    rotate(i*sideAngle); 
    line(-halfSideLineLength, -lineY, halfSideLineLength, -lineY);    
    //line(0,0, halfSideLineLength, -lineY);
    popMatrix();
  }
  //fill(255);
  //ellipse(centerPositionX, centerPositionY,4,4);
  //noFill();
  //ellipse(centerPositionX, centerPositionY,2*radio,2*radio);
}

now tell me what to do