#include "solid.h"

struct vector normal_vector(struct solid_obj *obj_ptr,
    struct facet *facet_ptr)
/*  Returns vector normal to a facet */
{
    struct vertex *vertex_ptr;
    static float x_coord[3], y_coord[3], z_coord[3];
        /* coordinates of first 3 vertices of facet */
    static int vertex_ref_index; /* index of current
        vertex for current facet */
    struct vector normal;

    for (vertex_ref_index = 0; vertex_ref_index <
        facet_ptr->vertex_count; ++vertex_ref_index) {
        vertex_ptr = obj_ptr->vertex_first +
            facet_ptr->vertex_index[vertex_ref_index];
            /* save coords of first 3 vertices */
        if (vertex_ref_index < 3) {
            x_coord[vertex_ref_index] = vertex_ptr->
                coord[0];
            y_coord[vertex_ref_index] = vertex_ptr->
                coord[1];
            z_coord[vertex_ref_index] = vertex_ptr->
                coord[2];
        }
    }

    normal.x = (y_coord[1] - y_coord[0]) * (z_coord[2]
        - z_coord[1]) - (z_coord[1] - z_coord[0]) *
        (y_coord[2] - y_coord[1]); normal.y =
        (z_coord[1] - z_coord[0]) * (x_coord[2] -
        x_coord[1]) - (x_coord[1] - x_coord[0]) *
        (z_coord[2] - z_coord[1]);
    normal.z = (x_coord[1] - x_coord[0]) * (y_coord[2]
        - y_coord[1]) - (y_coord[1] - y_coord[0]) *
        (x_coord[2] - x_coord[1]);
    return(normal);
}
