Draws a regular polygon (all sides equal, all angles equal) onto the display centered at x and y.
You can draw many things with the commands in the Drawing drawer of your Game Lab toolbox. For regularPolygon(), the x and y coordinates specify the center of the regular polygon, relative to the top-left corner of the display area (x:0 y:0). The third parameter is the number of sides for the regular polygon, and the last parameter is the distance from the center to each vertex. Regular polygons are drawn using the current stroke weight and current stroke color, and then filled with the current fill color (unless noStroke()
or noFill()
commands have been run).
regularPolygon(200, 200, 5, 50);
Display all the regular polygons with three to fourteen sides.
// Display all the regular polygons with three to fourteen sides. regularPolygon(50, 75, 3, 50); regularPolygon(150, 75, 4, 50); regularPolygon(250, 75, 5, 50); regularPolygon(350, 75, 6, 50); regularPolygon(50, 175, 7, 50); regularPolygon(150, 175, 8, 50); regularPolygon(250, 175, 9, 50); regularPolygon(350, 175, 10, 50); regularPolygon(50, 275, 11, 50); regularPolygon(150, 275, 12, 50); regularPolygon(250, 275, 13, 50); regularPolygon(350, 275, 14, 50);
Make a patterned tile floor.
// Make a patterned tile floor. background("orange"); for (var i = 0; i < 4; i++) { fill("pink"); regularPolygon(50+100*i, 5, 4, 35); regularPolygon(50+100*i, 145, 4, 35); regularPolygon(50+100*i, 285, 4, 35); fill("red"); regularPolygon(50+100*i, 75, 6, 50); regularPolygon(50+100*i, 215, 6, 50); regularPolygon(50+100*i, 355, 6, 50); }
regularPolygon(x, y, sides, size)
Name | Type | Required? | Description |
---|---|---|---|
x | Number | The x-location in pixels of the center of the regular polygon, from left to right on the display. Should be a number from 0 to 400, but negative numbers will center the regular polygon to the left of the display and numbers greater than 400 will center the regular polygon to the right of the display (possibly unseen). | |
y | Number | The y-location in pixels of the center of the regular polygon, from top to bottom on the display. Should be a number from 0 to 400, but negative numbers will center the regular polygon above the display and numbers greater than 400 will center the regular polygon below the display (possibly unseen). | |
sides | Number | The number of sides for the regular polygon. Should be a number greater than 2. | |
size | Number | The distance from the center to each vertex of the regular polygon. Should be a positive number. |
No return value. Outputs to the display only.
noFill()
or noStroke()
haven't been called, and where you're trying to draw the regular polygon fits within the coordinates of the display (400 x 400).strokeWeight()
, stroke()
, and fill()
.Found a bug in the documentation? Let us know at documentation@code.org