Demonstrating ellipses using parametric equations.
- Author
- Sukesh Ashok Kumar
This example visualizes an ellipse - a stretched or compressed circle that appears in planetary orbits, optical systems, and architectural designs. An ellipse is the set of all points where the sum of distances to two focal points is constant.
Mathematical Concepts:
- Standard ellipse equation: (x/a)² + (y/b)² = 1
- Parametric form: x = a·cos(t), y = b·sin(t) where 0 ≤ t < 2π
- Semi-major axis (a): half the length along the longer dimension
- Semi-minor axis (b): half the length along the shorter dimension
- Eccentricity: e = √(1 - b²/a²) measures how "stretched" the ellipse is
- Circle is special case: when a = b, ellipse becomes a circle
- Ratio: a/b determines how elongated the ellipse appears
In This Example:
- a = 200 (horizontal radius, semi-major axis)
- b = 100 (vertical radius, semi-minor axis)
- Center: (320, 240) - middle of window
- Aspect ratio: 2:1 (twice as wide as it is tall)
- Eccentricity: e = √(1 - 100²/200²) = √(1 - 0.25) ≈ 0.866
Parametric Equations Explained: As parameter t varies from 0 to 2π:
- x = centerX + a·cos(t) sweeps horizontally
- y = centerY + b·sin(t) sweeps vertically
- When t=0: (centerX+a, centerY) = (520, 240) - rightmost point
- When t=π/2: (centerX, centerY-b) = (320, 140) - top point
- When t=π: (centerX-a, centerY) = (120, 240) - leftmost point
- When t=3π/2: (centerX, centerY+b) = (320, 340) - bottom point
Programming Concepts:
- Parametric representation of curves
- Using trigonometric functions (sin, cos) together
- Floating-point iteration with small increments
- Converting from parametric to Cartesian coordinates
- The constant π (pi) ≈ 3.14159
What you'll learn:
- How ellipses generalize circles
- Parametric equations for smooth curve generation
- The relationship between cos and sin in curve drawing
- Why parametric form is often easier than standard form
- Converting continuous parameter to discrete pixels
- Major and minor axes and their geometric meaning
Why Parametric Form?
- Easier to compute: no square roots needed
- Guarantees smooth, evenly-spaced points
- Natural parameter t represents angle
- Simple to implement: just evaluate cos and sin
- Alternative to standard form (x/a)² + (y/b)² = 1
Experiment Ideas:
- Set a = b = 150: creates a perfect circle
- Set a = 250, b = 50: very stretched ellipse
- Swap a and b: vertical ellipse
- Change t increment to 0.001: smoother curve
Compile:
gcc m-ellipse.c gfx/simplegfx.c -o output/m-ellipse -lX11 -lm
Note: -lm links the math library for cos() and sin() functions
Run: