Katkat

Render Overlapping Rectangles on an ASCII Canvas

AmperitySoftware Engineer · Onsite

Last updated

Render bordered and filled rectangles onto a fixed ASCII canvas, clipping safely and honoring input-order overwrites. Handle negative coordinates, fully hidden shapes, one-cell dimensions, trailing spaces, overlap behavior, and a canvas of up to one million cells.

Render Overlapping Rectangles on an ASCII Canvas

Implement render_canvas(width, height, rectangles). The canvas initially contains spaces and is returned as a list of height strings, each of length width.

Each rectangle is (x, y, w, h, border_char, fill_char), where (x, y) is its upper-left corner, w is its width, and h is its height. Draw the outermost cells with border_char and interior cells with fill_char. Coordinates may place part or all of a rectangle outside the canvas; clip safely. Rectangles are drawn in input order, so later rectangles overwrite earlier pixels wherever they overlap.

Constraints

  • 0 <= width, height <= 1000
  • 1 <= w, h <= 1000
  • x and y may be negative.
  • border_char and fill_char are single printable characters.
  • The total canvas area is at most 10^6 cells.

Example

For a 5 by 4 canvas and rectangle (1, 1, 3, 2, '#', '.'), return:

 
 ### 
 ### 
 

A one-cell-wide or one-cell-high rectangle consists entirely of border cells.

Clarifications

Canvas coordinates use zero-based columns and rows. Preserve trailing spaces in every returned row. Explain how clipping prevents invalid indexing without changing whether a visible cell is a border or interior cell of the original rectangle.

Hints

Intersect each rectangle's coordinate range with the canvas before visiting pixels, but compute border status in the rectangle's original coordinates.

Extensions

  • Return only changed regions for a very large sparse canvas.
  • Support transparency or z-indexes.
  • Add tests for total clipping, corner clipping, thin rectangles, and overwrite order.