cube

A rotating 3D wireframe cube rendered in the terminal
git clone https://github.com/michaelneuper/cube.git
README | Files | Log | Refs | LICENSE

cube/src/cube.h (1721B)


/**
 * @file    cube.h
 * @author  Michael Neuper (michael@michaelneuper.com)
 *
 * Public API for the cube: types, constants, and functions for
 * creating, rotating, drawing, and destroying a 3D wireframe cube.
 */

#ifndef CUBE_H_
#define CUBE_H_

#define CUBE_VERTICES 8
#define CUBE_EDGES    12

/** The three cardinal axes used to specify a rotation direction. */
typedef enum {
    X,
    Y,
    Z,
} Axis3D;

/**
 * Represents a point in 3D-space with double-precision coordinates.
 */
typedef struct {
    double x, y, z;
} Point3D;

/**
 * Represents an edge by the indices of the two vertices
 * that defines it.
 */
typedef struct {
    const int v1, v2;
} Edge;

/**
 * Represents a cube by an array of its vertices
 * and an array of its edges.
 */
typedef struct {
    Point3D vertices[CUBE_VERTICES];
    Edge    edges[CUBE_EDGES];
} Cube;

/**
 * Allocates and initialises a unit cube centred at the origin.
 *
 * @return  pointer to the new Cube, or NULL on allocation failure
 */
Cube *create_cube();

/**
 * Frees the cube and tears down the ncurses canvas.
 *
 * @param[in]  cube
 *     cube to destroy (must not be NULL)
 */
void destroy_cube(Cube *cube);

/**
 * Renders all edges of the cube to stdscr.
 * Call refresh() after to make the frame visible.
 *
 * @param[in]  cube
 *     cube to draw (must not be NULL)
 */
void draw_cube(const Cube *cube);

/**
 * Rotates all vertices of the cube in-place around the given axis.
 *
 * @param[in,out]  cube
 *     cube to rotate (must not be NULL).
 * @param[in]      axis
 *     axis of rotation (X, Y, or Z).
 * @param[in]      angle
 *     rotation angle in radians.
 */
void rotate_cube(Cube *cube, Axis3D axis, double angle);

#endif // CUBE_H_