Polygons

class sage.plot.polygon.Polygon(xdata, ydata, options)[source]

Bases: GraphicPrimitive_xydata

Primitive class for the Polygon graphics type. For information on actual plotting, please see polygon(), polygon2d(), or polygon3d().

INPUT:

  • xdata – list of \(x\)-coordinates of points defining Polygon

  • ydata – list of \(y\)-coordinates of points defining Polygon

  • options – dictionary of valid plot options to pass to constructor

EXAMPLES:

Note this should normally be used indirectly via polygon():

sage: from sage.plot.polygon import Polygon
sage: P = Polygon([1,2,3],[2,3,2],{'alpha':.5})
sage: P
Polygon defined by 3 points
sage: P.options()['alpha']
0.500000000000000
sage: P.ydata
[2, 3, 2]
>>> from sage.all import *
>>> from sage.plot.polygon import Polygon
>>> P = Polygon([Integer(1),Integer(2),Integer(3)],[Integer(2),Integer(3),Integer(2)],{'alpha':RealNumber('.5')})
>>> P
Polygon defined by 3 points
>>> P.options()['alpha']
0.500000000000000
>>> P.ydata
[2, 3, 2]
from sage.plot.polygon import Polygon
P = Polygon([1,2,3],[2,3,2],{'alpha':.5})
P
P.options()['alpha']
P.ydata
plot3d(z=0, **kwds)[source]

Plots a 2D polygon in 3D, with default height zero.

INPUT:

  • z – (optional) 3D height above \(xy\)-plane, or a list of heights corresponding to the list of 2D polygon points

EXAMPLES:

A pentagon:

sage: polygon([(cos(t), sin(t))                                             # needs sage.symbolic
....:          for t in srange(0, 2*pi, 2*pi/5)]).plot3d()
Graphics3d Object
>>> from sage.all import *
>>> polygon([(cos(t), sin(t))                                             # needs sage.symbolic
...          for t in srange(Integer(0), Integer(2)*pi, Integer(2)*pi/Integer(5))]).plot3d()
Graphics3d Object
polygon([(cos(t), sin(t))                                             # needs sage.symbolic
         for t in srange(0, 2*pi, 2*pi/5)]).plot3d()
../../_images/polygon-1.svg

Showing behavior of the optional parameter z:

sage: P = polygon([(0,0), (1,2), (0,1), (-1,2)])
sage: p = P[0]; p
Polygon defined by 4 points
sage: q = p.plot3d()
sage: q.obj_repr(q.testing_render_params())[2]
['v 0 0 0', 'v 1 2 0', 'v 0 1 0', 'v -1 2 0']
sage: r = p.plot3d(z=3)
sage: r.obj_repr(r.testing_render_params())[2]
['v 0 0 3', 'v 1 2 3', 'v 0 1 3', 'v -1 2 3']
sage: s = p.plot3d(z=[0,1,2,3])
sage: s.obj_repr(s.testing_render_params())[2]
['v 0 0 0', 'v 1 2 1', 'v 0 1 2', 'v -1 2 3']
>>> from sage.all import *
>>> P = polygon([(Integer(0),Integer(0)), (Integer(1),Integer(2)), (Integer(0),Integer(1)), (-Integer(1),Integer(2))])
>>> p = P[Integer(0)]; p
Polygon defined by 4 points
>>> q = p.plot3d()
>>> q.obj_repr(q.testing_render_params())[Integer(2)]
['v 0 0 0', 'v 1 2 0', 'v 0 1 0', 'v -1 2 0']
>>> r = p.plot3d(z=Integer(3))
>>> r.obj_repr(r.testing_render_params())[Integer(2)]
['v 0 0 3', 'v 1 2 3', 'v 0 1 3', 'v -1 2 3']
>>> s = p.plot3d(z=[Integer(0),Integer(1),Integer(2),Integer(3)])
>>> s.obj_repr(s.testing_render_params())[Integer(2)]
['v 0 0 0', 'v 1 2 1', 'v 0 1 2', 'v -1 2 3']
P = polygon([(0,0), (1,2), (0,1), (-1,2)])
p = P[0]; p
q = p.plot3d()
q.obj_repr(q.testing_render_params())[2]
r = p.plot3d(z=3)
r.obj_repr(r.testing_render_params())[2]
s = p.plot3d(z=[0,1,2,3])
s.obj_repr(s.testing_render_params())[2]
sage.plot.polygon.polygon(points, **options)[source]

Return either a 2-dimensional or 3-dimensional polygon depending on value of points.

For information regarding additional arguments, see either polygon2d() or polygon3d(). Options may be found and set using the dictionaries polygon2d.options and polygon3d.options.

EXAMPLES:

sage: polygon([(0,0), (1,1), (0,1)])
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> polygon([(Integer(0),Integer(0)), (Integer(1),Integer(1)), (Integer(0),Integer(1))])
Graphics object consisting of 1 graphics primitive
polygon([(0,0), (1,1), (0,1)])
../../_images/polygon-2.svg
sage: polygon([(0,0,1), (1,1,1), (2,0,1)])
Graphics3d Object
>>> from sage.all import *
>>> polygon([(Integer(0),Integer(0),Integer(1)), (Integer(1),Integer(1),Integer(1)), (Integer(2),Integer(0),Integer(1))])
Graphics3d Object
polygon([(0,0,1), (1,1,1), (2,0,1)])

Extra options will get passed on to show(), as long as they are valid:

sage: polygon([(0,0), (1,1), (0,1)], axes=False)
Graphics object consisting of 1 graphics primitive
sage: polygon([(0,0), (1,1), (0,1)]).show(axes=False) # These are equivalent
>>> from sage.all import *
>>> polygon([(Integer(0),Integer(0)), (Integer(1),Integer(1)), (Integer(0),Integer(1))], axes=False)
Graphics object consisting of 1 graphics primitive
>>> polygon([(Integer(0),Integer(0)), (Integer(1),Integer(1)), (Integer(0),Integer(1))]).show(axes=False) # These are equivalent
polygon([(0,0), (1,1), (0,1)], axes=False)
polygon([(0,0), (1,1), (0,1)]).show(axes=False) # These are equivalent
sage.plot.polygon.polygon2d(points, alpha=1, rgbcolor=(0, 0, 1), edgecolor=None, thickness=None, legend_label=None, legend_color=None, aspect_ratio=1.0, fill=True, **options)[source]

Return a 2-dimensional polygon defined by points.

Type polygon2d.options for a dictionary of the default options for polygons. You can change this to change the defaults for all future polygons. Use polygon2d.reset() to reset to the default options.

EXAMPLES:

We create a purple-ish polygon:

sage: polygon2d([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> polygon2d([[Integer(1),Integer(2)], [Integer(5),Integer(6)], [Integer(5),Integer(0)]], rgbcolor=(Integer(1),Integer(0),Integer(1)))
Graphics object consisting of 1 graphics primitive
polygon2d([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))
../../_images/polygon-3.svg

By default, polygons are filled in, but we can make them without a fill as well:

sage: polygon2d([[1,2], [5,6], [5,0]], fill=False)
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> polygon2d([[Integer(1),Integer(2)], [Integer(5),Integer(6)], [Integer(5),Integer(0)]], fill=False)
Graphics object consisting of 1 graphics primitive
polygon2d([[1,2], [5,6], [5,0]], fill=False)
../../_images/polygon-4.svg

In either case, the thickness of the border can be controlled:

sage: polygon2d([[1,2], [5,6], [5,0]], fill=False, thickness=4, color='orange')
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> polygon2d([[Integer(1),Integer(2)], [Integer(5),Integer(6)], [Integer(5),Integer(0)]], fill=False, thickness=Integer(4), color='orange')
Graphics object consisting of 1 graphics primitive
polygon2d([[1,2], [5,6], [5,0]], fill=False, thickness=4, color='orange')
../../_images/polygon-5.svg

For filled polygons, one can use different colors for the border and the interior as follows:

sage: L = [[0,0]]+[[i/100, 1.1+cos(i/20)] for i in range(100)]+[[1,0]]          # needs sage.symbolic
sage: polygon2d(L, color='limegreen', edgecolor='black', axes=False)            # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[Integer(0),Integer(0)]]+[[i/Integer(100), RealNumber('1.1')+cos(i/Integer(20))] for i in range(Integer(100))]+[[Integer(1),Integer(0)]]          # needs sage.symbolic
>>> polygon2d(L, color='limegreen', edgecolor='black', axes=False)            # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[0,0]]+[[i/100, 1.1+cos(i/20)] for i in range(100)]+[[1,0]]          # needs sage.symbolic
polygon2d(L, color='limegreen', edgecolor='black', axes=False)            # needs sage.symbolic
../../_images/polygon-6.svg

Some modern art – a random polygon, with legend:

sage: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)]
sage: polygon2d(v, legend_label='some form')
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> v = [(randrange(-Integer(5),Integer(5)), randrange(-Integer(5),Integer(5))) for _ in range(Integer(10))]
>>> polygon2d(v, legend_label='some form')
Graphics object consisting of 1 graphics primitive
v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)]
polygon2d(v, legend_label='some form')
../../_images/polygon-7.svg

An aperiodic monotile, [Smi2023]:

sage: s = sqrt(3)                                                               # needs sage.symbolic
sage: polygon2d([[0, 0], [0, s], [1, s], [3/2, 3/2*s], [3, s], [3, 0], [4, 0],  # needs sage.symbolic
....:            [9/2, -1/2*s], [3, -s], [3/2, -1/2*s], [1, -s], [-1, -s],
....:            [-3/2, -1/2*s]], axes=False)
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> s = sqrt(Integer(3))                                                               # needs sage.symbolic
>>> polygon2d([[Integer(0), Integer(0)], [Integer(0), s], [Integer(1), s], [Integer(3)/Integer(2), Integer(3)/Integer(2)*s], [Integer(3), s], [Integer(3), Integer(0)], [Integer(4), Integer(0)],  # needs sage.symbolic
...            [Integer(9)/Integer(2), -Integer(1)/Integer(2)*s], [Integer(3), -s], [Integer(3)/Integer(2), -Integer(1)/Integer(2)*s], [Integer(1), -s], [-Integer(1), -s],
...            [-Integer(3)/Integer(2), -Integer(1)/Integer(2)*s]], axes=False)
Graphics object consisting of 1 graphics primitive
s = sqrt(3)                                                               # needs sage.symbolic
polygon2d([[0, 0], [0, s], [1, s], [3/2, 3/2*s], [3, s], [3, 0], [4, 0],  # needs sage.symbolic
           [9/2, -1/2*s], [3, -s], [3/2, -1/2*s], [1, -s], [-1, -s],
           [-3/2, -1/2*s]], axes=False)
../../_images/polygon-8.svg

A purple hexagon:

sage: L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)]                         # needs sage.symbolic
sage: polygon2d(L, rgbcolor=(1,0,1))                                            # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[cos(pi*i/Integer(3)),sin(pi*i/Integer(3))] for i in range(Integer(6))]                         # needs sage.symbolic
>>> polygon2d(L, rgbcolor=(Integer(1),Integer(0),Integer(1)))                                            # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)]                         # needs sage.symbolic
polygon2d(L, rgbcolor=(1,0,1))                                            # needs sage.symbolic
../../_images/polygon-9.svg

A green deltoid:

sage: L = [[-1+cos(pi*i/100)*(1+cos(pi*i/100)),                                 # needs sage.symbolic
....:       2*sin(pi*i/100)*(1-cos(pi*i/100))] for i in range(200)]
sage: polygon2d(L, rgbcolor=(1/8,3/4,1/2))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[-Integer(1)+cos(pi*i/Integer(100))*(Integer(1)+cos(pi*i/Integer(100))),                                 # needs sage.symbolic
...       Integer(2)*sin(pi*i/Integer(100))*(Integer(1)-cos(pi*i/Integer(100)))] for i in range(Integer(200))]
>>> polygon2d(L, rgbcolor=(Integer(1)/Integer(8),Integer(3)/Integer(4),Integer(1)/Integer(2)))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[-1+cos(pi*i/100)*(1+cos(pi*i/100)),                                 # needs sage.symbolic
      2*sin(pi*i/100)*(1-cos(pi*i/100))] for i in range(200)]
polygon2d(L, rgbcolor=(1/8,3/4,1/2))                                      # needs sage.symbolic
../../_images/polygon-10.svg

A blue hypotrochoid:

sage: L = [[6*cos(pi*i/100)+5*cos((6/2)*pi*i/100),                              # needs sage.symbolic
....:       6*sin(pi*i/100)-5*sin((6/2)*pi*i/100)] for i in range(200)]
sage: polygon2d(L, rgbcolor=(1/8,1/4,1/2))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[Integer(6)*cos(pi*i/Integer(100))+Integer(5)*cos((Integer(6)/Integer(2))*pi*i/Integer(100)),                              # needs sage.symbolic
...       Integer(6)*sin(pi*i/Integer(100))-Integer(5)*sin((Integer(6)/Integer(2))*pi*i/Integer(100))] for i in range(Integer(200))]
>>> polygon2d(L, rgbcolor=(Integer(1)/Integer(8),Integer(1)/Integer(4),Integer(1)/Integer(2)))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[6*cos(pi*i/100)+5*cos((6/2)*pi*i/100),                              # needs sage.symbolic
      6*sin(pi*i/100)-5*sin((6/2)*pi*i/100)] for i in range(200)]
polygon2d(L, rgbcolor=(1/8,1/4,1/2))                                      # needs sage.symbolic
../../_images/polygon-11.svg

Another one:

sage: n = 4; h = 5; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),                              # needs sage.symbolic
....:       n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: polygon2d(L, rgbcolor=(1/8,1/4,3/4))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> n = Integer(4); h = Integer(5); b = Integer(2)
>>> L = [[n*cos(pi*i/Integer(100))+h*cos((n/b)*pi*i/Integer(100)),                              # needs sage.symbolic
...       n*sin(pi*i/Integer(100))-h*sin((n/b)*pi*i/Integer(100))] for i in range(Integer(200))]
>>> polygon2d(L, rgbcolor=(Integer(1)/Integer(8),Integer(1)/Integer(4),Integer(3)/Integer(4)))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
n = 4; h = 5; b = 2
L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),                              # needs sage.symbolic
      n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
polygon2d(L, rgbcolor=(1/8,1/4,3/4))                                      # needs sage.symbolic
../../_images/polygon-12.svg

A purple epicycloid:

sage: m = 9; b = 1
sage: L = [[m*cos(pi*i/100)+b*cos((m/b)*pi*i/100),                              # needs sage.symbolic
....:       m*sin(pi*i/100)-b*sin((m/b)*pi*i/100)] for i in range(200)]
sage: polygon2d(L, rgbcolor=(7/8,1/4,3/4))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> m = Integer(9); b = Integer(1)
>>> L = [[m*cos(pi*i/Integer(100))+b*cos((m/b)*pi*i/Integer(100)),                              # needs sage.symbolic
...       m*sin(pi*i/Integer(100))-b*sin((m/b)*pi*i/Integer(100))] for i in range(Integer(200))]
>>> polygon2d(L, rgbcolor=(Integer(7)/Integer(8),Integer(1)/Integer(4),Integer(3)/Integer(4)))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
m = 9; b = 1
L = [[m*cos(pi*i/100)+b*cos((m/b)*pi*i/100),                              # needs sage.symbolic
      m*sin(pi*i/100)-b*sin((m/b)*pi*i/100)] for i in range(200)]
polygon2d(L, rgbcolor=(7/8,1/4,3/4))                                      # needs sage.symbolic
../../_images/polygon-13.svg

A brown astroid:

sage: L = [[cos(pi*i/100)^3, sin(pi*i/100)^3] for i in range(200)]              # needs sage.symbolic
sage: polygon2d(L, rgbcolor=(3/4,1/4,1/4))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[cos(pi*i/Integer(100))**Integer(3), sin(pi*i/Integer(100))**Integer(3)] for i in range(Integer(200))]              # needs sage.symbolic
>>> polygon2d(L, rgbcolor=(Integer(3)/Integer(4),Integer(1)/Integer(4),Integer(1)/Integer(4)))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[cos(pi*i/100)^3, sin(pi*i/100)^3] for i in range(200)]              # needs sage.symbolic
polygon2d(L, rgbcolor=(3/4,1/4,1/4))                                      # needs sage.symbolic
../../_images/polygon-14.svg

And, my favorite, a greenish blob:

sage: L = [[cos(pi*i/100)*(1+cos(pi*i/50)),                                     # needs sage.symbolic
....:       sin(pi*i/100)*(1+sin(pi*i/50))] for i in range(200)]
sage: polygon2d(L, rgbcolor=(1/8,3/4,1/2))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[cos(pi*i/Integer(100))*(Integer(1)+cos(pi*i/Integer(50))),                                     # needs sage.symbolic
...       sin(pi*i/Integer(100))*(Integer(1)+sin(pi*i/Integer(50)))] for i in range(Integer(200))]
>>> polygon2d(L, rgbcolor=(Integer(1)/Integer(8),Integer(3)/Integer(4),Integer(1)/Integer(2)))                                      # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[cos(pi*i/100)*(1+cos(pi*i/50)),                                     # needs sage.symbolic
      sin(pi*i/100)*(1+sin(pi*i/50))] for i in range(200)]
polygon2d(L, rgbcolor=(1/8,3/4,1/2))                                      # needs sage.symbolic
../../_images/polygon-15.svg

This one is for my wife:

sage: L = [[sin(pi*i/100)+sin(pi*i/50),                                         # needs sage.symbolic
....:       -(1+cos(pi*i/100)+cos(pi*i/50))] for i in range(-100,100)]
sage: polygon2d(L, rgbcolor=(1,1/4,1/2))                                        # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> L = [[sin(pi*i/Integer(100))+sin(pi*i/Integer(50)),                                         # needs sage.symbolic
...       -(Integer(1)+cos(pi*i/Integer(100))+cos(pi*i/Integer(50)))] for i in range(-Integer(100),Integer(100))]
>>> polygon2d(L, rgbcolor=(Integer(1),Integer(1)/Integer(4),Integer(1)/Integer(2)))                                        # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
L = [[sin(pi*i/100)+sin(pi*i/50),                                         # needs sage.symbolic
      -(1+cos(pi*i/100)+cos(pi*i/50))] for i in range(-100,100)]
polygon2d(L, rgbcolor=(1,1/4,1/2))                                        # needs sage.symbolic
../../_images/polygon-16.svg

One can do the same one with a colored legend label:

sage: polygon2d(L, color='red', legend_label='For you!', legend_color='red')    # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
>>> from sage.all import *
>>> polygon2d(L, color='red', legend_label='For you!', legend_color='red')    # needs sage.symbolic
Graphics object consisting of 1 graphics primitive
polygon2d(L, color='red', legend_label='For you!', legend_color='red')    # needs sage.symbolic
../../_images/polygon-17.svg

Polygons have a default aspect ratio of 1.0:

sage: polygon2d([[1,2], [5,6], [5,0]]).aspect_ratio()
1.0
>>> from sage.all import *
>>> polygon2d([[Integer(1),Integer(2)], [Integer(5),Integer(6)], [Integer(5),Integer(0)]]).aspect_ratio()
1.0
polygon2d([[1,2], [5,6], [5,0]]).aspect_ratio()

AUTHORS:

  • David Joyner (2006-04-14): the long list of examples above.