Example #1
0
def main():
    print("Step-by-step transformation:")
    pt = Point2D(3, 1)
    print(pt)
    pt = Transform2D.shear(pt, 3, -2)
    print(pt)
    pt = Transform2D.translate(pt, -3, 10)
    print(pt)
    pt = Transform2D.scale(pt, 3, 3)
    print(pt)
    pt = Transform2D.rotate(pt, math.pi / 2)
    print(pt)
    pt = Transform2D.reflect(pt, ReflectionLine.origin)
    print(pt)

    print("\nCombined transformation:")
    builder = Transform2DBuilder()
    builder.shear(3, -2)\
           .translate(-3, 10)\
           .scale(3, 3)\
           .rotate(math.pi / 2)\
           .reflect(ReflectionLine.origin)
    pt2 = Point2D(3, 1)
    print(pt2)
    print(builder.apply(pt2))
Example #2
0
 def _render_mesh(self, mesh: "Mesh", tb):
   for ft in mesh.facets:
     vs = []
     for p in ft.vertices:
       p2 = tb.apply(p)
       vs += [Point2D((p2.x + 1) / 2 * 736, (p2.y + 1) / 2 * 414)]
     self.gfx_delegate.draw_triangle(vs[0], vs[1], vs[2], (1, 1, 1, 0), '#fff', 1)
Example #3
0
 def apply(self, pt: "Point2D") -> "Point2D":
   """
   :param pt: the point to which the transformation should be applied
   :return: the point after transformation
   """
   pmx = Matrix.from_point_with_padding(pt)
   return Point2D(*(self._mtx * pmx).as_list()[:2])
Example #4
0
 def rotate(pt: "Point2D", rads: SupportsFloat = 0) -> "Point2D":
   """
   Rotate a given point by the amount specified (in radians)
   :param pt: a 2D Point to rotate
   :param rads: the amount of desired rotation (in radians). Default: 0.
   :return: A point that has been rotated by the given amount
   """
   pmx = Matrix.from_point_with_padding(pt)
   tmx = Transform2D.rotation_matrix(rads)
   return Point2D(*(tmx * pmx).as_list()[:2])
Example #5
0
 def reflect(pt: "Point2D", line: "ReflectionLine") -> "Point2D":
   """
   Reflect the given point over the specified reflection line
   :param pt: a 2D point to reflect
   :param line: a ReflectionLine2D value specifying the line of reflection
   :return: A reflected point
   """
   pmx = Matrix.from_point_with_padding(pt)
   tmx = Transform2D.reflection_matrix(line)
   return Point2D(*(tmx * pmx).as_list()[:2])
Example #6
0
 def shear(pt: "Point2D", x: Number = 0, y: Number = 0) -> "Point2D":
   """
   Shear a given point by the horizontal and vertical amount specified
   :param pt: a 2D Point to shear
   :param x: The amount of horizontal shear. Default: 0.
   :param y: The amount of vertical shear. Default: 0.
   :return: A sheared point
   """
   pmx = Matrix.from_point_with_padding(pt)
   tmx = Transform2D.shearing_matrix(x, y)
   return Point2D(*(tmx * pmx).as_list()[:2])
Example #7
0
 def scale(pt: "Point2D", x: Number = 1, y: Number = 1) -> "Point2D":
   """
   Scale a given point by the x_scale and y_scale amounts
   :param pt: a 2D Point to scale
   :param x: the amount of scaling in the x direction. Default 1.
   :param y: the amount of scaling in the y direction. Default 1.
   :return: A scaled point
   """
   pmx = Matrix.from_point_with_padding(pt)
   tmx = Transform2D.scaling_matrix(x, y)
   return Point2D(*(tmx * pmx).as_list()[:2])
Example #8
0
 def translate(pt: "Point2D", dx: Number = 0, dy: Number = 0) -> "Point2D":
   """
   Translate the given point by the x and y amounts specified
   :param pt: a 2D point to translate
   :param dx: change in the x direction. Default: 0.
   :param dy: change in the y direction. Default: 0.
   :return: A translated point
   """
   pmx = Matrix.from_point_with_padding(pt)
   tmx = Transform2D.translation_matrix(dx, dy)
   return Point2D(*(tmx * pmx).as_list()[:2])
 def get_tri_center(a: "Point2D", b: "Point2D", c: "Point2D"):
   """
   Since the coordinates used to create a path are completely separate from the position of the ShapeNode housing the
   path, this convenience method provides the coordinate to which the ShapeNode must be positioned so the path ends up
   where it should be on the screen.
   :param a: The first point
   :param b: The second point
   :param c: The third point
   :return: the center of the bounding box that surrounds the three given points.
   """
   xmin = min(a.x, b.x, c.x)
   xmax = max(a.x, b.x, c.x)
   ymin = min(a.y, b.y, c.y)
   ymax = max(a.y, b.y, c.y)
   cx = (xmax - xmin)/2 + xmin
   cy = (ymax - ymin)/2 + ymin
   return Point2D(cx, cy)