Skip to content

Instantly share code, notes, and snippets.

@adusak
Created June 2, 2015 10:49
Show Gist options
  • Save adusak/b91e983ca4530db55fea to your computer and use it in GitHub Desktop.
Save adusak/b91e983ca4530db55fea to your computer and use it in GitHub Desktop.
Polygon class
import numpy as np
from python.common.line import Line
class Polygon:
def __init__(self, points=None):
self.points = []
if points is not None:
self.points.extend(points)
def lines(self):
lines = []
if len(self.points) > 1:
points = self.points[:]
for i in range(1, len(points)):
lines.append(Line(points[i - 1][0], points[i - 1][1], points[i][0], points[i][1]))
return lines
def apply_transformation(self, transformation):
old_points = self.points[:]
self.points.clear()
for point in old_points:
point_matrix = np.identity(3)
point_matrix[0, 0] = point[0]
point_matrix[1, 0] = point[1]
point_matrix[2, 0] = 1
result = np.dot(transformation, point_matrix)
self.points.append((result[0, 0], result[1, 0]))
return self
def draw_to_svg(self, svg):
list(map(lambda x: svg.add_line(x), self.lines()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment