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

Creating star shapes using polar coordinates and trigonometry. More...

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

Macros

#define PI   3.14159265359
 

Functions

int main ()
 

Detailed Description

Creating star shapes using polar coordinates and trigonometry.

Author
Sukesh Ashok Kumar

This example demonstrates how to create a perfect star by combining polar coordinates, trigonometry, and symmetry. Stars are excellent for learning angle division, coordinate conversion, and line drawing algorithms.

Mathematical Concepts:

  • Polar coordinates: (radius, angle) instead of (x, y)
  • Angle division: 360°/n creates n evenly-spaced points
  • Conversion: x = r·cos(θ), y = r·sin(θ) (polar to Cartesian)
  • Radians: 2π radians = 360°, π/5 radians = 36°
  • Rotational symmetry: star has 5-fold rotational symmetry
  • Alternating pattern: outer points alternate with inner points

In This Example:

  • 5-pointed star (classic pentagram shape)
  • Outer radius: 150 pixels (tips of star)
  • Inner radius: 60 pixels (indentations between tips)
  • Total vertices: 10 (5 outer + 5 inner, alternating)
  • Angular spacing: π/5 radians = 36° between consecutive vertices

Star Construction Algorithm:

  1. Place points on two concentric circles (outer and inner)
  2. Alternate between outer and inner radius
  3. Evenly distribute 10 points around the circles
  4. Connect consecutive points with straight lines
  5. This creates the characteristic star shape

Programming Concepts:

  • Polar to Cartesian coordinate conversion
  • Modulo operator (%) for alternating patterns
  • Ternary operator: condition ? valueIfTrue : valueIfFalse
  • Line drawing using linear interpolation
  • abs() function from stdlib.h
  • Using define for constants (PI)

What you'll learn:

  • How to work with polar coordinates in graphics
  • Converting between coordinate systems
  • Creating symmetric shapes using trigonometry
  • The relationship between angles and circle division
  • Drawing lines by interpolating between endpoints
  • Using modulo for alternating patterns
  • Why polar coordinates simplify rotational patterns

Polar Coordinates Explained: Instead of (x,y) position, use (radius, angle):

  • Radius: distance from center
  • Angle: direction from center (measured from right, counterclockwise)
  • To plot: x = centerX + radius·cos(angle) y = centerY + radius·sin(angle)

Line Drawing Algorithm: To draw line from (x1,y1) to (x2,y2):

  1. Calculate differences: dx = x2-x1, dy = y2-y1
  2. Determine steps: max of |dx| or |dy|
  3. For each step, interpolate: x = x1 + dx·step/steps
  4. This ensures continuous line without gaps

Experiment Ideas:

  • Change points to 6: creates 6-pointed star (Star of David)
  • Change points to 8: creates 8-pointed star
  • Adjust innerRadius to 100: changes star sharpness
  • Try innerRadius = outerRadius: creates regular polygon

Compile:

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

Note: -lm links the math library for cos() and sin() functions

Run:

./output/m-star

Macro Definition Documentation

◆ PI

#define PI   3.14159265359

Function Documentation

◆ main()

int main ( )