regularPolygon()
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).
Examples
regularPolygon(200, 200, 5, 50);
Three to Fourteen Sides
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);
Tile Floor
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);
}
Syntax
regularPolygon(x, y, sides, size)
Parameters
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. |
Returns
Tips
- If you're having trouble getting a regular polygon to show up, make sure that
noFill()
ornoStroke()
haven't been called, and where you're trying to draw the regular polygon fits within the coordinates of the display (400 x 400). - Anything you draw will overlap previous things you drew. The sequence of drawing statements is usually important.
- The default stroke is black, the default stroke weight is 1 pixel, and the default fill is gray. Change the width of the line, color of the line, and fill color used to draw all subsequent shapes using
strokeWeight()
,stroke()
, andfill()
. - When drawing thick lines, the size of the regular polygon is relative to the center of the perimeter line. The outside perimeter of the regular polygon will be one half the stroke weight larger all around.
Found a bug in the documentation? Let us know at support@code.org.