Skip to content

Drawing Ellipses

Since version 1.19.1-724 of the MicroPython runtime was released in February 2023 we have support for the framebuf Ellipse function. This function allows us to draw circles and ellipses that are both filled and un-filled.

We can also draw into one or more quadrants so we can do 1/4, 1/2, or 3/4 filled ellipses.

Sample Program Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Test of the micropython ellipse function
# display.ellipse(x, y, HORZ_RADIUS, VERT_RADIUS, COLOR, FILL_FLAG, QUAD_CODE)

from machine import Pin
from utime import sleep
import framebuf
import ssd1306

WIDTH = 128
HEIGHT = 64

clock=Pin(2) #SCL
data=Pin(3) #SDA
RES = machine.Pin(4)
DC = machine.Pin(5)
CS = machine.Pin(6)

spi=machine.SPI(0, sck=clock, mosi=data)
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)

# center the ellipse in the middle of the display
x = int(WIDTH / 2)
y = int(HEIGHT / 2)

HORZ_RADIUS = 30 # 30 over for the left and 30 to the right 
VERT_RADIUS = 20 # 20 up and 20 down
COLOR = 1 # 0 or 1

# draw an ellipse wihout the fill
oled.ellipse(x, y, HORZ_RADIUS, VERT_RADIUS, COLOR)
oled.show()

Ellipse Parameters

Ellipse Parameters

display.ellipse(x, y, HORZ_RADIUS, VERT_RADIUS, COLOR, FILL_FLAG, QUAD_CODE)

Draw an ellipse at the given location. Radii HORZ_RADIUS and VERT_RADIUS define the geometry.

If HORZ_RADIUS equal the VERT_RADIUS a circle is drawn. The COLOR parameter defines the color. 0 for OFF and 1 for ON.

The optional FILL_FLAG parameter can be set to True to fill the ellipse. Otherwise, just a single pixel outline is drawn.

Quadrant Fill Codes

Ellipse Quadrants

The optional QUAD_CODE parameter enables drawing to be restricted to certain quadrants of the ellipse.

You can add the following numbers to figure out the quadrant code:

  • 1 - upper right
  • 2 - upper left
  • 4 - lower left
  • 8 - lower right

For example for the lower half of the ellipse on you add 4 + 8 = 12.

Here is a complete list of the 16 possible values of the code:

Code Description
0 all off
1 upper right on
2 upper left on
3 top half on
4 lower left on
5 lower left and upper right on
6 left side on
7 lower right off
9 right side on
10 upper left and lower right are on
11 all but lower left are on
12 lower half on
13 all but upper left are on
14 all but upper right are on
15 all four quadrants are drawn

References

MicroPython Framebuf Documentation