Circles¶
Circles are drawn by calculating a 2D array of Euclidean distances and clipping:

Here is the code that creates the circle:
from PIL import Image
import numpy as np
def circle(a, xcenter, ycenter, radius, color):
Y, X = np.ogrid[0:400, 0:400]
square_dist = (X - xcenter) ** 2 + (Y - ycenter) ** 2
mask = square_dist < radius ** 2
a[mask] = color
radius = 100
xcenter, ycenter = 200, 200
color = np.array([255, 128, 0], np.uint8)
a = np.zeros((400, 400, 3), np.uint8)
circle(a, xcenter, ycenter, radius, color)
im = Image.fromarray(a)
im.save('circle.png')
Hints¶
the
np.ogridfunction creates two 2D-array of the X/Y coordinatesthe
square_distcontains the squared distance to the center for each pixelfor the mask, the rule of Pythagoras is applied to select points inside the circle
Challenge:¶
Adopt the code to create patterns from circles, e.g.:


