def draw_torus(self, center_x, center_y, center_z, radius1, radius2, color=Color.BLACK(), theta_step=30, phi_step=30): """ Draws the polygons of a sphere onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: center_x: int, the x coordinate of the center of the sphere center_y: int, the y coordinate of the center of the sphere center_z: int, the z coordinate of the center of the sphere radius1: int, the radius of the circle being revolved to make the torus radius2: int, the radius of the torus itself color: Color (optional), the color of the points theta_step: int (optional), the number of steps to use when drawing the circle that is revolved to make the torus phi_step: int (optional), the number of steps to use when rotating the circles about the center point """ self.draw_polygonmatrix( Generator.get_torus_polygonmatrix(center_x, center_y, center_z, radius1, radius2, theta_step=theta_step, phi_step=phi_step), color)
def fill_polygonmatrix(self, matrix, color=Color.BLACK()): """ Draws and fills the current PolygonMatrix onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: matrix: PolygonMatrix, the matrix of triangles to draw cull_view_vector: Vector (optional), the view vector that the faces should be culled with, None if the faces should not be culled. color: Color (optional), the color to draw the matrix with """ if not isinstance(matrix, PolygonMatrix): raise TypeError("%s is not a PolygonMatrix" % matrix) matrix *= self.get_transformation() if self.view_vector: matrix = matrix.cull_faces(self.view_vector) for triangle in matrix.get_rounded(): b = min(triangle, key=lambda point: point[1]) m = sorted(triangle, key=lambda point: point[1])[1] t = max(triangle, key=lambda point: point[1]) z_depth = min(triangle, key=lambda point: point[2]) x1, x2 = b[0], b[0] dx1 = (t[0] - b[0]) / float(t[1] - b[1]) if t[1] - b[1] else 0 dx2bm = (m[0] - b[0]) / float(m[1] - b[1]) if m[1] - b[1] else 0 dx2mt = (t[0] - m[0]) / float(t[1] - m[1]) if t[1] - m[1] else 0 for y in range(b[1], m[1] + 1): self._draw_line(int(x1), y, int(x2), y, color, z_depth=z_depth) x1 += dx1 x2 += dx2bm for y in range(m[1], t[1] + 1): self._draw_line(int(x1), y, int(x2), y, color, z_depth=z_depth) x1 += dx1 x2 += dx2mt
def draw_sphere_points(self, center_x, center_y, center_z, radius, color=Color.BLACK(), theta_step=30, phi_step=30): """ Draws points representing a sphere onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: center_x: int, the x coordinate of the center of the sphere center_y: int, the y coordinate of the center of the sphere center_z: int, the z coordinate of the center of the sphere radius: int, the radius of the sphere color: Color (optional), the color of the points theta_step: int (optional), the number of steps to use when drawing the circles phi_step: int (optional), the number of steps to use when rotating the circles about the center point """ self.draw_pointmatrix( Generator.get_sphere_pointmatrix(center_x, center_y, center_z, radius, theta_step=theta_step, phi_step=phi_step), color)
def draw_polygonmatrix(self, matrix, color=Color.BLACK()): """ Draws the given PolygonMatrix onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: matrix: PolygonMatrix, the matrix of triangles to draw cull_view_vector: Vector (optional), the view vector that the faces should be culled with, None if the faces should not be culled. color: Color (optional), the color to draw the matrix with """ if not isinstance(matrix, PolygonMatrix): raise TypeError("%s is not a PolygonMatrix" % matrix) matrix *= self.get_transformation() if self.view_vector: matrix = matrix.cull_faces(self.view_vector) for triangle in matrix.get_rounded(): self._draw_line(triangle[0][0], triangle[0][1], triangle[1][0], triangle[1][1], color, z_depth=min(triangle[0][2], triangle[1][2])) self._draw_line(triangle[1][0], triangle[1][1], triangle[2][0], triangle[2][1], color, z_depth=min(triangle[1][2], triangle[2][2])) self._draw_line(triangle[2][0], triangle[2][1], triangle[0][0], triangle[0][1], color, z_depth=min(triangle[2][2], triangle[0][2]))
def __init__(self, game): """ Initialize render variables """ self.game = game self.timer = pygame.time.Clock() self.started = time.time() self.delta = time.time() - (self.started + 1) self.c_color = Color.BLACK() self.color = self.c_color self.init_size = self.game.size self.ready = [False, self.game.fps / 8, 0]
def draw_point(self, x, y, z, color=Color.BLACK()): """ Draws the given point onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: x: int, the x coordinate of the point y: int, the y coordinate of the point z: int, the z coordinate of the point color: Color (optional), the color of the line """ self.draw_pointmatrix(Matrix(matrix=[[x, y, z]]), color=color)
def draw_pointmatrix(self, matrix, color=Color.BLACK()): """ Draws the given Matrix of points onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: matrix: Matrix, the matrix of points to draw color: Color (optional), the color to draw the matrix with """ if not isinstance(matrix, Matrix): raise TypeError("%s is not a Matrix" % matrix) for point in (matrix * self.get_transformation()).get_rounded(): self._set_pixel(point[0], point[1], color)
def draw_edgematrix(self, matrix, color=Color.BLACK()): """ Draws the given EdgeMatrix onto the internal raster as lines after applying the current TransformationMatrix on the stack. Parameters: matrix: EdgeMatrix, the matrix of lines to draw color: Color (optional), the color to draw the matrix with """ if not isinstance(matrix, EdgeMatrix): raise TypeError("%s is not an EdgeMatrix" % matrix) for edge in (matrix * self.get_transformation()).get_rounded(): self._draw_line(edge[0][0], edge[0][1], edge[1][0], edge[1][1], color)
def draw_bezier_curve(self, p1, i1, i2, p2, color=Color.BLACK(), step=30): """ Draws a bezier curve onto the internal raster after applying the current TranformationMatrix on the stack. Parameters: p1: list, the first endpoint of the bezier curve i1: list, the first influence point of the bezier curve i2: list, the second influence point of the bezier curve p2: list, the second endpoint of the bezier curve color: Color (optional), the color of the curve step: int (optional), the number of steps to use for drawing the curve """ self.draw_edgematrix( Generator.get_bezier_curve_edgematrix(p1, i1, i2, p2, step=step), color)
def draw_hermite_curve(self, p1, r1, p2, r2, color=Color.BLACK(), step=30): """ Draws a hermite curve onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: p1: list, the first point of the hermite curve r1: list, the rate of change at p1 p2: list, the second point of the hermite curve r2: list, the rate of change at p2 color: Color (optional), the color of the curve step: int (optional), the number of steps to use for drawing the curve """ self.draw_edgematrix( Generator.get_hermite_curve_edgematrix(p1, r1, p2, r2, step=step), color)
def draw_line(self, x1, y1, z1, x2, y2, z2, color=Color.BLACK()): """ Draws the a line onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: x1: int, the x coordinate of the first endpoint of the line y1: int, the y coordinate of the first endpoint of the line z1: int, the z coordinate of the first endpoint of the line x1: int, the x coordinate of the second endpoint of the line y1: int, the y coordinate of the second endpoint of the line z1: int, the z coordinate of the second endpoint of the line color: Color (optional), the color of the line """ self.draw_edgematrix(EdgeMatrix([[x1, y1, z1, 1], [x2, y2, z2, 1]]), color)
def draw_box(self, x, y, z, width, height, depth, color=Color.BLACK()): """ Draws the polygons of a box onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: x: int, the x coordinate of the front left bottom of the box y: int, the y coordinate of the front left bottom of the box z: int, the z coordinate of the front left bottom of the box width: int, the width of the box height: int, the height of the box depth: int, the depth of the box color: Color (optional), the color of the points """ self.draw_polygonmatrix( Generator.get_box_polygonmatrix(x, y, z, width, height, depth), color)
def draw_circle(self, center_x, center_y, radius, color=Color.BLACK(), step=30): """ Draws a circle onto the internal raster after applying the current TransformationMatrix on the stack. Parameters: center_x: int, the x coordinate of the center of the circle center_y: int, the y coordinate of the center of the circle radius: int, the radius of the circle color: Color (optional), the color of the circle step: int (optional), the number of steps to use when drawing splines for the circle """ self.draw_edgematrix( Generator.get_circle_edgematrix(center_x, center_y, radius, step=step), color)