Draw polygons using loops?

If i wanted to draw say 6-sided polygon or whatever number, using just simple loops, how would I go about doing it?

Answers

  • Look up "polygon" in the examples!

    There is an example of drawing regular polygons using simple loops.

  • how about if i wanted to use circles(ellipses) to draw one?

  • Well, the official example uses vertex().

    vertex(sx, sy);
    

    What if you used ellipse() instead?

    ellipse(sx, sy, 5, 5);
    

    Try it.

  • hmm, maybe im doing something wrong? but it just seems to draw a flower shape instead of a polygon

  • Please post your code.

    Kf

  • edited November 2017

    `int numSides = 9; float radius = 100;float angle = TWO_PI / numSides; // the angle of one "slice" of the circle

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

    void draw() { background(255); fill(100, 90, 255); noStroke();

    // move the the center of the canvas so we can draw around (0,0) translate(width/2, height/2);

    .

    for (int i = 0; i <= numSides; i++) { float x = cos(iangle) * radius; float y = sin(iangle) * radius; ellipse(x, y,200,200);

    }

    }`

    maybe it is easier to do it with lines?

  • Edit post, highlight code, press ctrl-o to format

  • Jeremy said: ellipse(sx, sy, 5, 5);

    You used: ellipse(x, y,200,200);

  • edited November 2017

    Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    You are drawing polygons using ellipses. Lines are a better option. You should consider checking: https://processing.org/reference/beginShape_.html

    ***EDITED: Use vertex. See next comment

    Kf

  • @kfrajer -- the thing is, though, using beginShape / endShape and vertex is the way you draw lines... and the example sketch already does exactly that, with vertex.

    @majestiee -- I'm assuming that the request to use ellipse means something else. Maybe trying to create dotted lines...? It isn't clear.

    You could manually save the n points to an array, then walk lines across the array. But that's more or less recreating what PShape does with vertex on a closed shape. Is this homework, and are there specific requirements...?

  • edited November 2017

    No i an just playing around with processing lol so sorry about the confusion. I am currently studying loops at school and wanted to create shapes using them.

  • Processing does most of the work for you using vertex. Now, if you want to go raw, then you can do using lines, and you will require to be comfortable working with lines equations and maybe some basic trig concepts. Working with loops and programming concepts, I will stick to what Processing offers.

    Please don't forget to format your code when posting in the forum.

    Kf

Sign In or Register to comment.