Challenge¶
Create an image of a circle like this one, from scratch, using NumPy. Then plot it with Matplotlib.
The image should be a raster image, 1000 pixels wide by 1000 pixels tall. The circle is purple above and pink below.
Solution¶
Imagine the raster grid lying in a Cartesian-like coordinate system.
Now recall the general equation for a circle...
( x − h ) 2 + ( y − k ) 2 = r 2 (x - h)^2 + (y - k)^2 = r^2 (x−h)2+(y−k)2=r2where ( h , k ) (h, k) (h,k) represents the circle's center and r r r represents its radius.
In this case, we want the radius to be 500 and we want the center at (499.5, 499.5). And since we want to activate points inside the circle, we want to use the inequality
( x − h ) 2 + ( y − k ) 2 < r 2 (x - h)^2 + (y - k)^2 < r^2 (x−h)2+(y−k)2<r2Once we identify and activate pixels in the circle interior, we can easily make the upper half purple and the lower half pink.