def ellipse(a, b, rotate=0, fade=False): """ Draw an ellipse using pixels :param a (int): major axis :param b (int): minor axis :param fade (bool): should the color of the ellipse fade """ img = bitmap.PNG() for angle in numpy.arange(0, 360, 0.1): rangle = math.radians(angle) for i in numpy.arange(0, 255): # (0,0,0) -> (255,255,255) greyscale # => 255 ellipse rings each smaller with darker colour x = (a * i / 255) * math.cos(rangle) y = (b * i / 255) * math.sin(rangle) if rotate: rrotate = math.radians(rotate) x = x * math.cos(rrotate) - y * math.sin(rrotate) y = x * math.sin(rrotate) + y * math.cos(rrotate) if fade: img.draw_pixel(bitmap.Point(x + 200, y + 200), color=(i, i, i)) # + 200 for image center else: img.draw_pixel(bitmap.Point(x + 200, y + 200)) img.show()
def grid(): img = bitmap.PNG() pixels = [] grid_size = 20 img_size = 220 # grid for i in range(img_size): for j in range(img_size): if (i // grid_size) % 2 == (j // grid_size) % 2: pixels.append([i, j, False]) # False for black else: pixels.append([i, j, True]) # True for white center = (img_size // 2, img_size // 2) # create circles and invert grid color inside them for radius in (45, 85, 125): for pixel in pixels: # if pixel is in the circle, change its color if math.sqrt((pixel[0] - center[0])**2 + (pixel[1] - center[1])**2) <= radius: pixel[2] = not pixel[2] # draw the pisels for pixel in pixels: if pixel[2]: color = "white" else: color = "black" img.draw_pixel(bitmap.Point(pixel[0], pixel[1]), color=color) img.show()
def spiral(a, b, size, color=False): """ Draw a spiral using pixels :param a (int): x coordinate for spiral start :param b (int): y coordinate for spiral start :param size (int): size of the spiral :param color (bool): should be the spiral colored """ img = bitmap.PNG() for angle in numpy.arange(0, 360 * size, 0.1): rangle = math.radians(angle) r = 4 * rangle x = r * math.cos(rangle) + a y = r * math.sin(rangle) + b if color: img.draw_pixel(bitmap.Point(x, y), color=colors[int((angle + 45) / 90) % len(colors)]) else: img.draw_pixel(bitmap.Point(x, y)) img.show()
def chaos_game(n=3, p=1 / 2, iterations=100000, regular=True): img = bitmap.PNG(800, 800) # regular or random starting points? if regular: points = [ bitmap.Point( 400 + 300 * math.cos( 2 * math.pi * i / n ), # + 400 is to be sure we don't have negative coordinates 400 + 300 * math.sin(2 * math.pi * i / n)) for i in range(1, n + 1) ] else: points = [ bitmap.Point(400 + 300 * math.cos(2 * math.pi * random.random()), 400 + 300 * math.sin(2 * math.pi * random.random())) for i in range(n) ] # draw the initial points for i in points: img.draw_pixel(i) x = bitmap.Point(random.uniform(0, 600), random.uniform(0, 600)) for i in range(iterations): r = points[random.randint(0, len(points) - 1)] x = bitmap.Point((x.x + r.x) * p, (x.y + r.y) * p) if i > 100: img.draw_pixel(x) img.show()
def bitmap_img(): img = bitmap.PNG() for i in range(255): for j in range(255): img.draw_pixel(bitmap.Point(i, 255 - j), color=(i, 0, 255 - j)) img.show()
def newton(x_range=(-2, 2), y_range=(-2, 2), resolution=800, tolerance=0.0001, timing=False): # lengths of the intervals (ranges) x_length = x_range[1] - x_range[0] y_length = y_range[1] - y_range[0] # if the range is not 1:1 recalculate 'y-resolution' x_resolution = resolution y_resolution = int((y_length / x_length) * resolution) img = bitmap.PNG(x_resolution, y_resolution, img_color=(0, 0, 0)) # steps for x and y axis so we produce 'enough pixels' to fill the image x_step = x_length / x_resolution y_step = y_length / y_resolution for x in numpy.arange(x_range[0], x_range[1], x_step): for y in numpy.arange(y_range[0], y_range[1], y_step): z = complex(x, y) if z == 0: continue i = 0 while i < 100: # pixel coordinates -- make sure to start with 0 and end with x/y-resoltion if x_range[0] <= 0: px = (x + abs(x_range[0])) * (x_resolution / x_length) else: px = (x - abs(x_range[0])) * (x_resolution / x_length) if y_range[0] <= 0: py = (y + abs(y_range[0])) * (y_resolution / y_length) else: py = (y - abs(y_range[0])) * (y_resolution / y_length) if abs(z - roots[0]) < tolerance: img.draw_pixel(bitmap.Point(px, py), color=(255 - i * 10, 0, 0)) break elif abs(z - roots[1]) < tolerance: img.draw_pixel(bitmap.Point(px, py), color=(0, 255 - i * 10, 0)) break elif abs(z - roots[2]) < tolerance: img.draw_pixel(bitmap.Point(px, py), color=(0, 0, 255 - i * 10)) break z = znext(z) i += 1 # do not show image when measuring time if not timing: img.show()
def julia( x_range=(-1.5, 1.5), y_range=(-1.5, 1.5), resolution=800, color=False): # lengths of the intervals (ranges) x_length = x_range[1] - x_range[0] y_length = y_range[1] - y_range[0] # if the range is not 1:1 recalculate 'y-resolution' x_resolution = resolution y_resolution = int((y_length / x_length) * resolution) img = bitmap.PNG(x_resolution, y_resolution) # steps for x and y axis so we produce 'enough pixels' to fill the image x_step = x_length / x_resolution y_step = y_length / y_resolution for x in numpy.arange(x_range[0], x_range[1], x_step): print(x) for y in numpy.arange(y_range[0], y_range[1], y_step): c = complex(-0.13, 0.75) z = complex(x, y) i = 0 while i < 30: z = znext(z, c) if abs(z) > 2: break i += 1 # pixel coordinates -- make sure to start with 0 and end with x/y-resoltion if x_range[0] <= 0: px = (x + abs(x_range[0])) * (x_resolution / x_length) else: px = (x - abs(x_range[0])) * (x_resolution / x_length) if y_range[0] <= 0: py = (y + abs(y_range[0])) * (y_resolution / y_length) else: py = (y - abs(y_range[0])) * (y_resolution / y_length) # color or greyscale if color: hsv_color = (1 - (i / 30), 0.75, 0.75) rgb_color = [ int(255 * j) for j in colorsys.hsv_to_rgb(*hsv_color) ] else: rgb_color = [255 - i * 10, 255 - i * 10, 255 - i * 10] img.draw_pixel(bitmap.Point(px, py), color=(rgb_color[0], rgb_color[1], rgb_color[2])) img.show()
def newton_main(x_range=(-2, 2), y_range=(-2, 2), resolution=800, tolerance=0.0001, num_workers=4): # lengths of the intervals (ranges) x_length = x_range[1] - x_range[0] y_length = y_range[1] - y_range[0] # if the range is not 1:1 recalculate 'y-resolution' x_resolution = resolution y_resolution = int((y_length / x_length) * resolution) img = bitmap.PNG(x_resolution, y_resolution, img_color=(0, 0, 0)) # steps for x and y axis so we produce 'enough pixels' to fill the image x_step = x_length / x_resolution y_step = y_length / y_resolution workers = [] files = [] for i in range(num_workers): x_part_range = (x_range[0] + i * (x_length / num_workers), x_range[0] + (i + 1) * (x_length / num_workers)) p = Process(target=newton_worker, args=(i, x_part_range, y_range, x_step, y_step, tolerance)) files.append("res%d" % i) p.start() workers.append(p) for worker in workers: worker.join() for fname in files: with open(fname, "r") as f: for line in f: x, y, color1, color2, color3 = line.split(";") x = float(x) y = float(y) color = (int(color1), int(color2), int(color3)) if x_range[0] <= 0: px = (x + abs(x_range[0])) * (x_resolution / x_length) else: px = (x - abs(x_range[0])) * (x_resolution / x_length) if y_range[0] <= 0: py = (y + abs(y_range[0])) * (y_resolution / y_length) else: py = (y - abs(y_range[0])) * (y_resolution / y_length) img.draw_pixel(bitmap.Point(px, py), color=color) img.show()
def draw(self): x_0 = min([v.x for v in self.vertices]) x_1 = max([v.x for v in self.vertices]) y_0 = min([v.y for v in self.vertices]) y_1 = max([v.y for v in self.vertices]) img = bitmap.PNG() for i in numpy.arange(x_0, x_1, 1): for j in numpy.arange(y_0, y_1, 1): p = bitmap.Point(i, j) if self.is_in(p): img.draw_pixel(p) # img.draw_line(bitmap.Point(0, 70), bitmap.Point(150, 100), color="red") # img.draw_line(bitmap.Point(0, 70), bitmap.Point(120, 150), color="blue") img.show()
def triangle(a=255, color=True): """ Draw a triangle using pixels :param a (int): side lenght :param color (bool): should be the triangle colored """ img = bitmap.PNG() i = 0 # level shift = 0.5 * math.sqrt(2) / 2 for y in numpy.arange(0, a / math.sqrt(2), 0.5): for x in numpy.arange(0 + shift * i, a - shift * i, 0.5): img.draw_pixel(bitmap.Point(x, y), color=(int(255 - x), int(x), int( (y * math.sqrt(2))))) i += 1 img.show()
def circle(a, b, r, fill=False): """ Draw a circle using pixels :param a (int): x coordinate for circle center :param b (int): y coordinate for circle center :param r (int): radius of the circle :param fill (bool): should be the circle filled """ img = bitmap.PNG() for angle in numpy.arange(0, 360, 0.1): rangle = math.radians(angle) x = r * math.cos(rangle) y = r * math.sin(rangle) img.draw_pixel(bitmap.Point(x + a, y + b)) if fill: for i in numpy.arange(0, r, 0.1): x = i * math.cos(rangle) y = i * math.sin(rangle) img.draw_pixel(bitmap.Point(x + a, y + b)) img.show()