xpgl Shapes

To use:

from XPPython3 import xpgl

Every geometric shape is a polygon: a set of vertices which describe a set of connecting, (ideally) non-crossing lines. This polygon may be filled with a solid color. If not filled, it will be an outline, where each line-segment follows the rules of lines: they have thickness, color, and stipple pattern. Solid objects do not have a separate border.

drawTriangle(x1, y1, x2, y2, x3, y3, color=Colors['white']) None:
Parameters
  • x1 (float) –

  • y1 (float) – Point 1

  • x2 (float) –

  • y2 (float) – Point 2

  • x3 (float) –

  • y3 (float) – Point 3

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Draws a color-filled triangle with the specified three vertices.:

xpgl.drawTriangle(0, 0, screen.width / 2, screen.height / 2, screen.width, 0)
xpgl.drawTriangle(screen.width / 2, screen.height / 2, 400, 400, 50, 400, color=Colors['orange'])
xpgl.drawTriangle(500, 300, 500, 400, 550, 290, color=Colors['red'])
../../_images/xpgl_drawTriangle.png
drawRectangle(x, y, width, height, color=Colors['white']) None:
Parameters
  • x (float) –

  • y (float) – Origin of rectangle (lower-left corner)

  • width (float) – horizontal size of rectangle

  • height (float) – vertical size of rectangle

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Draws a color-filled rectangle of specified width and height, with origin at (x, y). Negative width and height work as expected.:

xpgl.drawRectangle(1, 1, screen.width / 2, screen.height / 2)
xpgl.drawRectangle(screen.width / 2, screen.height / 2, -100, -50, color=Colors['orange'])
xpgl.drawRectangle(500, 300, 20, 20, color=Colors['red'])
../../_images/xpgl_drawRectangle.png
drawFrame(x, y, width, height, thickness=1., color=Colors['white']) None:
Parameters
  • x (float) –

  • y (float) – Origin of rectangle (lower-left corner)

  • width (float) – horizontal size of rectangle

  • height (float) – vertical size of rectangle

  • thickness (float) – width of line

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Draws an outline of a rectangle of specified width and height, with origin at (x, y). Negative width and height work as expected.

xpgl.drawFrame(1, 1, screen.width / 2, screen.height / 2, thickness=10)
xpgl.drawFrame(1, 1, screen.width / 2, screen.height / 2, thickness=1, color=Colors['red'] )
xpgl.setLinePattern(0x5555)
xpgl.drawFrame(300, 300, 100, 100, thickness=4, color=Colors['cyan'])
xpgl.setLinePattern()

Note in the image below how the thickness of the line interacts with the frame’s origin. Also, line thickness does not “round-out” end caps on the lines.

../../_images/xpgl_drawFrame.png
drawPolygon(points, isFilled=True, thickness=1., color=Colors['white']) None:
Parameters
  • points (list) – List or tuple of pairs of floats (x, y) for vertices

  • isFilled (bool) – True, if should be a filled-in polyon. Outline otherwise

  • thickness (float) – Width of line (ignored if isFilled)

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Draws a polygon following specified vertices. If last vertex does not match the first vertex, we’ll automatically add it, closing the polygon. Polygon may be clockwise or counter-clockwise (if you don’t know what that means, don’t worry about it.) Polygon may be convex or concave.

Polygons which have crossing lines may yield surprising results, as with the yellow shape in the following example:

shape1 = [(150, 100), (100, 350), (200, 300), (175, 150), (400, 70)]
xpgl.drawPolygon(shape1)
xpgl.drawPolygon(shape1, isFilled=False, thickness=3, color=Colors['magenta'])

shape2 = [(300, 300), (300, 450), (450, 450), (250, 350)]
xpgl.drawPolygon(shape2, color=Colors['yellow'])
xpgl.drawPolygon(shape2, isFilled=False, thickness=3, color=Colors['red'])
../../_images/xpgl_drawPolygon.png
drawCircle(x, y, radius, isFilled=False, thickness=1., num_vertices=36, color=Colors['white']) None
Parameters
  • x (float) –

  • y (float) – (x, y) location of center of the circle

  • isFilled (bool) – if True, fill the circle with given color. Otherwise draw outline only.

  • thickness (float) – Width of line (ignored if isFilled)

  • num_vertices (int) – Optional parameter, allowing you to change the # of vertices used to draw the circle.

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Two circles:

xpgl.setLinePattern(0x5555)
xpgl.drawCircle(250, 300, 150, thickness=5, color=Colors['orange'])
xpgl.setLinePattern()
xpgl.drawCircle(450, 300, 50, thickness=5, num_vertices=3, color=Colors['red'])
xpgl.drawCircle(200, 200, 50, isFilled=True, color=Colors['green'])
../../_images/xpgl_drawCircle.png

Note, because a circle is actually drawn using many small line segments, the stipple pattern works, but is not particularly smooth.

drawArcLine(x, y, radius, start_angle, arc_angle, num_vertices=36, color=Colors['white']) None
Parameters
  • x (float) –

  • y (float) – (x, y) location of center of the arc (center of circle containing arc)

  • radius (float) – Radius of circle containing arc.

  • start_angle (float) – degrees at which to start drawing angle (0 is 3 o’clock, 90 is 12 o’clock)

  • arc_angle (float) – number of degrees to draw the arc (not ending angle)

  • thickness (float) – Width of line

  • num_vertices (int) – Optional parameter, allowing you to change the # of vertices used to draw the circle containing the arc.

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Draws a line, a portion of a circle outline. Zero degrees for start_angle is at 3 o’clock, relative the (x, y) center location. The arc is drawn counter-clockwise for positive arc_angle. Clockwise for negative arc_angle.:

xpgl.setLinePattern(0x5555)
xpgl.drawArcLine(250, 300, 150, start_angle=90, arc_angle=180, thickness=5, color=Colors['orange'])
xpgl.setLinePattern()
xpgl.drawArcLine(450, 300, 50, start_angle=45, arc_angle=-90, thickness=2, color=Colors['red'])
xpgl.drawArcLine(300, 200, 100, start_angle=270, arc_angle=120, thickness=4, color=Colors['green'])
../../_images/xpgl_drawArcLine.png
drawArc(x, y, radius_inner, radius_outer, start_angle, arc_angle, num_vertices=36, colorColors['white']) None
Parameters
  • x (float) –

  • y (float) – (x, y) location of center of the arc (center of circle containing arc)

  • radius_inner (float) –

  • radius_outer (float) – Inner and outer radius of the arc.

  • start_angle (float) – degrees at which to start drawing angle (0 is 3 o’clock, 90 is 12 o’clock)

  • arc_angle (float) – number of degrees to draw the arc (not ending angle)

  • num_vertices (int) – Optional parameter, allowing you to change the # of vertices used to draw the circle containing the arc.

  • color (RGBColor) – OpenGL color. Tuple of floats (r, g, b) ranging [0..1]

Returns

None

Similar to drawArcLine(), this function draws a solid (e.g., filled) arc with a minumum and maximum radius.:

xpgl.drawArc(250, 300, 100, 150, start_angle=90, arc_angle=180, color=Colors['orange'])
xpgl.drawArc(450, 300, 15, 50, start_angle=45, arc_angle=90, color=Colors['red'])
xpgl.drawArc(300, 200, 0, 100, start_angle=270, arc_angle=120, color=Colors['green'])
../../_images/xpgl_drawArc.png