Renderer

class LiquidRed.grid.Renderer(camera, light_direction_vector, canvas_width, canvas_height)

Bases: object

Renderer for ASCII-based 3D rasterization.

This class maintains a 2D character grid and a z-buffer for depth testing, and provides methods to draw geometric primitives.

Parameters:
  • camera (Camera) – Camera used for projection and transformations.

  • light_direction_vector (np.ndarray) – Direction vector for lighting calculations.

  • canvas_width (int) – Width of the rendering grid.

  • canvas_height (int) – Height of the rendering grid.

grid

2D array of characters representing the rendered frame.

Type:

np.ndarray

z_buffer

Depth buffer storing nearest z-values per pixel.

Type:

np.ndarray

clear_grid()

Clear the rendering grid and reset the z-buffer.

draw_line(*args, **kwargs)
draw_plane(v0, v1, v2, v3, char=None)

Draw a quadrilateral plane using two triangles with Lambert shading.

Parameters:
  • v0 (Point3d) – Vertices of the plane.

  • v1 (Point3d) – Vertices of the plane.

  • v2 (Point3d) – Vertices of the plane.

  • v3 (Point3d) – Vertices of the plane.

  • char (str, optional) – Override shading character.

draw_plane_xy(x0, x1, y0, y1, z, char=None)

Draw a plane parallel to the XY plane.

draw_plane_xz(x0, x1, z0, z1, y, char=None)

Draw a plane parallel to the XZ plane.

draw_plane_yz(y0, y1, z0, z1, x, char=None)

Draw a plane parallel to the YZ plane.

draw_triangle(*args, **kwargs)
is_in_bounds(xi, yi)

Check if coordinates are within the canvas.

Parameters:
  • xi (ndarray)

  • yi (ndarray)

Return type:

bool

is_visible(xi, yi, zi, char='+')

Perform z-buffer depth test.

Parameters:
  • xi (ndarray) – x_i of points.

  • yi (ndarray) – y_i of points.

  • zi (ndarray) – z_i of points.

  • char (str, optional) – Character used for rendering. '#' bypasses z-buffer.

Returns:

True if the point is visible.

Return type:

bool

plot_point(p)

Transform a world-space point into screen-space.

Applies: - camera translation - yaw rotation - pitch rotation - perspective projection

Parameters:

p (Point3d)

Return type:

Point3d

project_3d(point)

Project a 3D point onto the 2D screen using perspective projection.

Parameters:

point (Point3d) – Input 3D point.

Returns:

Projected 2D point with depth.

Return type:

Point3d

show_grid()

Render the grid to the terminal.

LiquidRed.grid.project(func)

Decorator to project 3D input points to 2D screen space before rendering.

This applies Renderer.plot_point to all positional arguments.

Parameters:

func (callable) – Rendering function that operates on projected points.

Returns:

Wrapped function with automatic projection.

Return type:

callable