Created
February 13, 2015 20:13
-
-
Save Exodus111/0d1718e55a85d85ed131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from PIL import Image, ImageDraw | |
from vec2d import vec2d | |
from random import randint as rai | |
class Poly(object): | |
"""This generates a polygon in a numpy Array""" | |
def __init__(self, size, sides): | |
self.size = size | |
self.sides = sides | |
self.array = np.zeros((size, size), dtype=np.float32) | |
self.point = (size/2, size/2) | |
self.minmax = (size/4, size/2) | |
self.step = 360/sides | |
self.generate() | |
self.dlines = self.fix_list() | |
self.collided = 0 | |
def generate(self): | |
"""Here we generate the polygon | |
This method is called in the __init__ | |
""" | |
self.poly_list = [] | |
degrees = 90 | |
for side in xrange(self.sides): | |
side = vec2d(self.point) | |
side.angle = degrees | |
side.length = rai(int(self.minmax[0]), int(self.minmax[1])) | |
degrees += self.step | |
x = self.point + side | |
self.poly_list.append(x.inttup()) | |
def fix_list(self): | |
"""We need the generated vertexes to also include the points in between | |
as well as our starting and ending points, to generate edges(lines) | |
This method has to be called in code. | |
""" | |
lines = zip(self.poly_list[0::2], self.poly_list[1::2]) | |
fixed = [] | |
x = 0 | |
for t in lines: | |
if x != 0: | |
f1 = [x[1], t[0]] | |
fixed.append(f1) | |
f2 = [t[0], t[1]] | |
fixed.append(f2) | |
x = t | |
f3 = [lines[-1][1], lines[0][0]] | |
fixed.append(f3) | |
return fixed | |
def make_line(self, a, b): | |
"""Here we generate our original edges | |
This method is called from our draw2array method | |
""" | |
av = vec2d(a) | |
bv = vec2d(b) | |
line = [] | |
between = bv - av | |
endl = between.length | |
l = 1 | |
while l < endl: | |
between.length = l | |
x = av + between | |
line.append(x.inttup()) | |
l += 1 | |
return line | |
def draw2array(self): | |
"""This method makes an array with our current information, | |
and flattens our edge information to just a collection and point tuples (x,y)""" | |
lines = [] | |
for li in self.dlines: | |
line = self.make_line(li[0], li[1]) | |
lines.append(line) | |
self.lines = [item for sublist in lines for item in sublist] | |
for x,y in self.lines: | |
self.array[x,y] = 1 | |
def bnw(self): | |
"""This is optional, but here we color our polygon. | |
Warning: It's slow. | |
""" | |
temparray = np.zeros((self.size, self.size), dtype=np.float32) | |
for y in xrange(self.size): | |
for x in xrange(self.size): | |
if self.array[x,y] != 1: | |
if self.intersects(x, y): | |
temparray[x,y] = 0 | |
else: | |
temparray[x,y] = -1 | |
else: | |
temparray[x,y] = 1 | |
self.array = temparray | |
def intersects(self, x, y): | |
inter = 0 | |
for i in xrange(y): | |
if self.array[x, y-i] == 1: | |
inter += 1 | |
if inter % 2 == 0: | |
return False | |
else: | |
return True | |
def draw_image(size, array): | |
"""Here we draw our array to a .png image.""" | |
img = Image.new("I", (size, size)) | |
draw = ImageDraw.Draw(img) | |
x = y = 0 | |
for x in xrange(size): | |
for y in xrange(size): | |
va = array[x,y] | |
va = (va + 1)/2 * 0xFFFF | |
va = int(round(va)) | |
draw.point((x, y), va) | |
img.save("poly.png", "PNG") | |
if __name__ == "__main__": | |
mypoly = Poly(512, 6) | |
mypoly.draw2array() | |
mypoly.bnw() | |
draw_image(mypoly.size, mypoly.array) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment