Skip to content

POLYGON ​

Draws a closed shape by connecting several points.

Syntax ​

Syntax with a fixed amount of points:

leo-grammar
POLYGON intValue
    [ x1:measureValue ] [ y1:measureValue ]
    [ x2:measureValue ] [ y2:measureValue ]
    ...
    [ xn:measureValue ] [ yn:measureValue ] .

Syntax using an array of points:

leo-grammar
POLYGON arrayValue .

Parameters ​

Parameters for fixed amount of points:

  • <main-parameter> (intValue) - Used in the fixed amount of points version to specify the amount of points.
  • x<intValue> (measureValue, optional) - The x-coordinate of the n-th point where n-th is specified through <intValue>. The default is 0cm.
  • y<intValue> (measureValue, optional) - The y-coordinate of the n-th point where n-th is specified through <intValue>. The default is 0cm.

Parameters for array of points:

  • <main-parameter> (arrayValue) - Used in the array version to specify the points. The array must have an even amount of measure values.

Details ​

The style of this command is defined by the PEN command for the outline and the FILL command to fill the shape.

When using the syntax with a fixed amount of points the coordinates are grouped by the number after x and y, like x1:... with y1:... and x2:... with y2:... etc. If more groups of points are provided than the amount of points stated in the main parameter, then excess coordinates are ignored.

When using the syntax with the array the amount of values in the array must be an equal number and all values must be a measureValue. The first two values are used as the x- and y-coordinate of the first point, the next two values as the x- and y-coordinate of the second point and so on.

The benefit of the array version is that it doesn't require to specify the amount of points upfront. Instead they are determined based on the array's length. This way other commands can be used to build the array dynamically.

This command differs from POLYLINE in that it connects the last point back to the first, creating a closed shape that can be filled with a color. The lines of the polygon can intersect, in which case the area that is considered part of the shape depends on the sequence in which the points are connected.

See Also ​

Examples ​

Draw a triangle using a polygon with a "fixed" amount of points.
A triangle

leo
GRAPHREP
SHADOW off

POLYGON 3
    x1:-0.5cm y1:0.5cm
    x2:0cm y2:-0.5cm
    x3:0.5cm y3:0.5cm

Draw a triangle using an array of points.
A triangle

leo
GRAPHREP
SHADOW off

POLYGON {-0.5cm, 0.5cm, 0cm, -0.5cm, 0.5cm, 0.5cm}

Draw a purple hour-glass shape and a lying hour-glass shape to the right of it. The order of the points influences which areas are filled as part of the shape.
A purple hour-glass shape and a lying hour-glass shape to the right of it

leo
GRAPHREP
SHADOW off

FILL color:"purple"
# 1st: top-left, 2nd: top-right, 3rd: bottom-left, 4th: bottom-right.
POLYGON 4
    x1:-0.5cm y1:-0.5cm
    x2:0.5cm y2:-0.5cm
    x3:-0.5cm y3:0.5cm
    x4:0.5cm y4:0.5cm

# A different order of the points causes different areas to be filled in.
# 1st: top-left, 2nd: bottom-left, 3rd: top-right, 4th: bottom-right.
POLYGON 4
    x1:0.6cm y1:-0.5cm
    x2:0.6cm y2:0.5cm
    x3:1.6cm y3:-0.5cm
    x4:1.6cm y4:0.5cm

Draw a green zig-zag shape based on dynamically calculated points.
A triangular wavy line

leo
GRAPHREP
SHADOW off

# Configuration parameters.
SET dX:(0cm) dStartY:(0cm)
SET dW:(0.3cm) dH:(0.5cm)
SET nPoints:(7)
# Create array with a starting point.
SET aPattern:({dX, dStartY})
# `to` uses -2 because the first point has been added and the last point will be added later.
FOR nI from:(0) to:(nPoints - 2) {
  SET dX:(dX + cond(nI, dW, dW/2))
  SET dummy:(aappend(aPattern, dX))
  SET dummy:(aappend(aPattern, dStartY + cond(nI MOD 2, dH, -dH)))
}
# Addint the last point in the array.
SET dummy:(aappend(aPattern, dX + dW/2))
SET dummy:(aappend(aPattern, dStartY))

# Draw the shape, filling it with a green color.
FILL color:"chartreuse"
POLYGON (aPattern)

Versions and Changes ​

Available since ADOxx 1.3