Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a Python script that demonstrates how you would draw a circle without using multiplication. It uses the formula x<sup>2</sup> + y<sup>2</sup> > r<sup>2</sup> to check whether the current pixel position (x, y) is outside the circle.</p> <p>One can maintain a running computation of the squared values by using <strong>addition</strong>, since <a href="http://www.wolframalpha.com/input/?i=derivative+of+%28%28x+%2B+1%29%5E2+-x%5E2%29" rel="nofollow noreferrer">the rate of change of the difference between the squares of adjacent pixel indices is 2</a>. As you iterate through pixels, you'll keep adding a number (call that number <code>z</code>) to your running "squares" computations, and for each pixel, you will also add 2 to <code>z</code>.</p> <p>The <code>__init__</code> function below does contain some multiplication operations. However, if the image dimensions are constant, the <strong>compiler</strong> will perform the multiplication and only load the <strong>result</strong> 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.</p> <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python class CircularMaskGenerator: '''Generates a circular "mask" for an image. Maintains a running computation of squared values to eliminate the need for multiplication''' def __init__(self, w, h): self.image_width = w self.image_height = h # Precompute some important values. # The embedded device doesn't actaully have to do this math; the # compiler does it before loading the code onto the device. self.half_image_width = self.image_width >> 1 self.half_image_height = self.image_height >> 1 self.radius = self.half_image_height self.squared_half_image_width = self.half_image_width*self.half_image_width self.squared_half_image_height = self.half_image_height*self.half_image_height self.squared_radius = self.radius*self.radius # The "deltas" are the difference between the squared values of adjacent pixel indices. self.initial_x_squared_delta = 1 - self.image_width self.initial_y_squared_delta = 1 - self.image_height self.resetFrame() def resetFrame(self): # Start with a white binary image import Image self.image = Image.new("1", (self.image_width, self.image_height), 1) self.pixels = self.image.load() # Reset indices self.resetColumnIndex() self.resetRowIndex() def processPixel(self): # Write a black pixel if we're outside the circle if self.current_x_squared + self.current_y_squared > self.squared_radius: self.pixels[(self.current_column_index, self.current_row_index)] = 0 self.updateIndices() def updateIndices(self): '''Update the indices and squares values''' self.incrementColumnIndex() # Wrap to the next row if necessary if self.current_column_index == self.image_width: self.incrementRowIndex() # Wrap to the next frame if necessary if self.current_row_index == self.image_height: self.writeImage() self.resetFrame() def incrementColumnIndex(self): self.current_column_index += 1 self.current_x_squared += self.current_x_squared_delta self.current_x_squared_delta += 2 def resetColumnIndex(self): self.current_column_index = 0 self.current_x_squared = self.squared_half_image_width self.current_x_squared_delta = self.initial_x_squared_delta def incrementRowIndex(self): '''The row increment has to send the column index back to the left.''' self.resetColumnIndex() self.current_row_index += 1 self.current_y_squared += self.current_y_squared_delta self.current_y_squared_delta += 2 def resetRowIndex(self): self.current_row_index = 0 self.current_y_squared = self.squared_half_image_height self.current_y_squared_delta = self.initial_y_squared_delta def writeImage(self): '''Save the image in PNG format in the current directory''' self.image.save("output.png", "PNG") # ============================================================================= def simulate_system(): image_width = 800 image_height = 600 # initialize the system circle_processor = CircularMaskGenerator(image_width, image_height) # supply a pixel stream to drive the system for i in xrange(image_width*image_height): circle_processor.processPixel() # ============================================================================= if __name__ == "__main__": simulate_system()</code></pre> <p>This code creates a centered circle like so:</p> <p><img src="https://i.stack.imgur.com/jofAB.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload