SimpleGFX  1.0
Simple Graphics Library for C Programming Education
m-circle.c File Reference

Demonstrating the Pythagorean theorem through circle drawing. More...

#include "gfx/simplegfx.h"
#include <math.h>

Functions

int main ()
 

Detailed Description

Demonstrating the Pythagorean theorem through circle drawing.

Author
Sukesh Ashok Kumar

This example visualizes one of the most famous theorems in mathematics - the Pythagorean theorem - by using it to draw a perfect circle. A circle is simply the set of all points at a fixed distance (radius) from a center point.

Mathematical Concepts:

  • Circle equation: x² + y² = r²
  • Pythagorean theorem: a² + b² = c²
  • Distance formula: d = √(x² + y²)
  • Radius: constant distance from center to any point on circle
  • Center point: (centerX, centerY)
  • Cartesian to polar relationship

Mathematical Derivation: For a circle centered at origin with radius r:

  • Any point (x,y) on the circle satisfies: distance = r
  • Distance = √(x² + y²)
  • Therefore: √(x² + y²) = r
  • Squaring both sides: x² + y² = r²

Programming Concepts:

  • Nested loops for 2D scanning
  • Distance calculation using sqrt() from math.h
  • Tolerance range for floating-point comparison
  • Relative coordinates vs absolute screen coordinates
  • Bounding box optimization

What you'll learn:

  • How the Pythagorean theorem defines circles
  • Using the distance formula in graphics
  • Why we need tolerance (±1) when comparing floating-point values
  • Converting between relative and absolute coordinates
  • Efficient circle drawing by limiting search space
  • Including and linking the math library (-lm flag)

Algorithm Explanation:

  1. Search only within a square bounding box (-radius to +radius)
  2. For each point (x,y), calculate distance from origin
  3. If distance ≈ radius (within tolerance), it's on the circle
  4. Translate relative coordinates to screen position

Why Tolerance? We check if distance is between (radius-1) and (radius+1) because:

  • Pixels are discrete (integers), but math uses continuous (floating-point)
  • Rounding can cause exact matches to miss
  • Tolerance creates a thicker, more visible circle outline

Compile:

gcc m-circle.c gfx/simplegfx.c -o output/m-circle -lX11 -lm

Note: -lm links the math library for sqrt() function

Run:

./output/m-circle

Function Documentation

◆ main()

int main ( )