2D Rendering

  • Denote an image with a single array and a pixel's index can be found by width * y + x.
  • The x coordinate is i % width, y is i / width.
  • To get a point at a distance r and angle t, relative to point (x0, y0)
  • For drawing a simple primitive, use its derivative to get the next pixel, usually the derivative is simpler
  • Ok, Fk, This stuff is sub optimal, since it kinda encourages drawing vertically, which is not optimal for the cache.

Line

  • A line's equation is

or, where

  • When drawing line, a simple for (x in x0..x1) usually leaves gaps if , and for (y in y0..y1) leaves gaps when , use both with if cases.
  • Use when when drawing lines to avoid division.

Circle

  • A circle's equation is

or,

  • Its not worth doing the derivative for a circle, just use the equation.
  • A circle can be drawn by only finding 1/8th of it, and mirroring it to the other 7 parts.

Ellipse

  • An ellipse's equation is

or,

  • Its not worth doing the derivative for a circle, just use the equation.
  • An ellipse can be drawn by only finding 1/4th of it, and mirroring it to the other 3 parts.

Rotated Rectangle

  • Find the points of upper line and the corresponding point at the bottom part at the same x coordinate.
  • Draw a vertical line between these two points.
  • Bam, you have a rotated rectangle.