Say that you are in a constrained computing environment where the multiplication operation is expensive or unavailable. You have access to a stream of pixels for an image of known dimensions. How would you draw a circle overlay onto the image?
Say that you are in a constrained computing environment where the multiplication operation is
Share
Here is a Python script that demonstrates how you would draw a circle without using multiplication. It uses the formula x2 + y2 > r2 to check whether the current pixel position (x, y) is outside the circle.
One can maintain a running computation of the squared values by using addition, since the rate of change of the difference between the squares of adjacent pixel indices is 2. As you iterate through pixels, you’ll keep adding a number (call that number
z) to your running “squares” computations, and for each pixel, you will also add 2 toz.The
__init__function below does contain some multiplication operations. However, if the image dimensions are constant, the compiler will perform the multiplication and only load the result onto the device. If the image dimensions are not constant, a “slow” multiplication need only be performed once upon startup, as opposed to once per pixel.This code creates a centered circle like so: