Creating coordinate grids using evenly spaced lines.
- Author
- Sukesh Ashok Kumar
This example demonstrates how to create a regular grid pattern, fundamental to graph paper, coordinate systems, and structured layouts. Grids help visualize relationships and organize space systematically.
Mathematical Concepts:
- Regular spacing: constant intervals between parallel lines
- Division: how many cells fit in the space (640/40 = 16 columns)
- Multiplication: spacing × n gives nth line position (40×3 = 120)
- Orthogonal lines: vertical and horizontal lines at right angles
- Coordinate system: grid creates reference framework
- Tessellation: repeating pattern that covers entire plane
In This Example:
- Spacing: 40 pixels between lines
- Vertical lines: at x = 0, 40, 80, 120, ..., 600
- Horizontal lines: at y = 0, 40, 80, 120, ..., 440
- Grid cells: 16 columns × 12 rows = 192 squares
- Each cell: 40×40 pixels
Programming Concepts:
- Nested loops for 2D iteration
- Loop increment by constant (x += spacing)
- Separating vertical and horizontal line drawing
- Efficient pattern generation
- Understanding loop step sizes
What you'll learn:
- How to create evenly spaced patterns
- Drawing complete vertical and horizontal lines
- The relationship between spacing and grid density
- Why we separate vertical and horizontal line loops
- Creating structured coordinate systems
- Loop increment shortcuts (x += 40)
Grid Properties:
- Vertical lines: constant x, varying y (parallel to y-axis)
- Horizontal lines: constant y, varying x (parallel to x-axis)
- Intersection points: form regular lattice
- Number of vertical lines: ⌈640/40⌉ = 16 lines
- Number of horizontal lines: ⌈480/40⌉ = 12 lines
Algorithm:
- Draw all vertical lines first
- For each x-position (0, 40, 80, ...)
- Draw from top to bottom (y: 0 → 480)
- Draw all horizontal lines
- For each y-position (0, 40, 80, ...)
- Draw from left to right (x: 0 → 640)
Applications:
- Graph paper for plotting functions
- Game boards (chess, checkers)
- Pixel art and sprite sheets
- Architectural floor plans
- Data visualization backgrounds
Experiment Ideas:
- Change spacing to 20: denser grid (more lines)
- Change spacing to 80: sparser grid (fewer lines)
- Add different colors for major/minor grid lines
- Create non-square grids with different horizontal/vertical spacing
Compile:
gcc m-square-grid.c gfx/simplegfx.c -o output/m-square-grid -lX11
Run: