Skip to content

Latest commit

 

History

History
107 lines (78 loc) · 2.83 KB

File metadata and controls

107 lines (78 loc) · 2.83 KB

Computer Graphics Algorithms

A simple Python project that demonstrates classic 2D scan-conversion and rasterization algorithms using a custom Canvas class and matplotlib for visualization.

Implemented Algorithms

1. Scan Conversion of a Point

  • Function: draw_point(canvas, x, y, single_point=False)
  • Description: Rounds a floating-point coordinate to the nearest pixel and plots it.
  • Notes: Uses floor(value + 0.5) style rounding.

2. DDA Line Algorithm

  • Function: dda_line(canvas, x1, y1, x2, y2)
  • Description: Draws a line by incrementing either x or y depending on slope magnitude.
  • Handles:
    • |slope| <= 1 using x-step iteration
    • |slope| > 1 using y-step iteration
    • Special base cases via shared helper

3. Bresenham Line Algorithm

  • Function: bresenham_line(canvas, x1, y1, x2, y2)
  • Description: Integer-based incremental line drawing with case handling for different slope ranges.
  • Handles:
    • 0 <= m <= 1
    • m > 1
    • -1 <= m < 0
    • m < -1
    • Vertical/horizontal/diagonal base cases

4. Bresenham Circle Algorithm

  • Function: bresenham_circle(canvas, xc, yc, r)
  • Description: Uses 8-way symmetry and a decision parameter to rasterize circles efficiently.

5. Mid-Point Circle Algorithm

  • Function: midPoint_circle(canvas, xc, yc, r)
  • Description: Draws circles using midpoint decision logic with 8-way symmetry.

6. Mid-Point Ellipse Algorithm

  • Function: midPoint_ellipse(canvas, xc, yc, a, b)
  • Description: Draws ellipses using region-based midpoint updates.
  • Notes: Splits drawing into region 1 and region 2 based on slope transition.

Shared Helpers

  • Line_Base_Cases(canvas, x1, y1, x2, y2)
    • Handles vertical, horizontal, and 45-degree diagonal lines.
  • circle_draw_step(...)
    • Plots all 8 symmetric circle points.
  • ellipse_draw_step(...)
    • Plots all 4 symmetric ellipse points.

Project Structure

Computer Graphics/
|-- algorithms.py
|-- canvas.py
|-- main.py
|-- HelloWorld/

Requirements

  • Python 3.x
  • matplotlib

Install dependency:

pip install matplotlib

How to Run

Run:

python main.py

In main.py, test one algorithm at a time by uncommenting a single line inside main().

Example:

# draw_point(canvas, 10.31, 10.47, True)
# dda_line(canvas, 5, 5, 40, 30)
# bresenham_line(canvas, 15, 10, 10, 50)
# bresenham_circle(canvas, 20, 30, 10)
# midPoint_circle(canvas, 20, 30, 10)
midPoint_ellipse(canvas, 25, 25, 15, 10)

Current Default Test

The current main.py runs:

  • midPoint_ellipse(canvas, 25, 25, 15, 10)

Output

Each algorithm plots generated pixels on a 2D grid with equal axis scaling using matplotlib.

Notes

  • Coordinate system is displayed in a standard Cartesian-style plot window.
  • Pixel plotting is done by storing points and then rendering with Canvas.show(title).