# Generating STL files in C

Generating STL files in C

make a c program to generate stl for a boat shape.
Add a mast on top .
Add a railing and 6 cannons.

make a c program to generate stl for a boat shape.
Add a mast on top .
Add a railing and 6 cannons.

c

``````
#include <stdio.h>
#include <stdint.h>

typedef struct {
double x;
double y;
double z;
} Vector3d;

typedef struct {
Vector3d v1;
Vector3d v2;
Vector3d v3;
} Triangle;

Vector3d calculate_normal(Triangle triangle) {
Vector3d u = {triangle.v2.x - triangle.v1.x, triangle.v2.y - triangle.v1.y, triangle.v2.z - triangle.v1.z};
Vector3d v = {triangle.v3.x - triangle.v1.x, triangle.v3.y - triangle.v1.y, triangle.v3.z - triangle.v1.z};
Vector3d normal = {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x};
double length = sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
normal.x /= length;
normal.y /= length;
normal.z /= length;
return normal;
}

int main() {
// Define boat vertices
Vector3d boat_vertices[24] = {
{-1, 0, -1},
{-1, 0, 1},
{1, 0, 1},
{1, 0, -1},
{-1, 1, -1},
{-1, 1, 1},
{1, 1, 1},
{1, 1, -1},
{-0.5, 1, -1},
{-0.5, 1, 1},
{0.5, 1, 1},
{0.5, 1, -1},
{-0.5, 1.5, -0.5},
{-0.5, 1.5, 0.5},
{0.5, 1.5, 0.5},
{0.5, 1.5, -0.5},
{-0.5, 2, -0.5},
{-0.5, 2, 0.5},
{0.5, 2, 0.5},
{0.5, 2, -0.5},
{-0.3, 2.1, -0.3},
{-0.3, 2.1, 0.3},
{0.3, 2.1, 0.3},
{0.3, 2.1, -0.3}
};

// Define boat faces
Triangle boat_faces[24] = {
{boat_vertices[0], boat_vertices[1], boat_vertices[4]},
{boat_vertices[1], boat_vertices[5], boat_vertices[4]},
{boat_vertices[1], boat_vertices[2], boat_vertices[5]},
{boat_vertices[2], boat_vertices[6], boat_vertices[5]},
{boat_vertices[2], boat_vertices[3], boat_vertices[6]},
{boat_vertices[3], boat_vertices[7], boat_vertices[6]},
{boat_vertices[3], boat_vertices[0], boat_vertices[7]},
//    {boat_vertices[0], boat_vertices[
{boat_vertices[4], boat_vertices[5], boat_vertices[8]},
{boat_vertices[5], boat_vertices[9], boat_vertices[8]},
{boat_vertices[5], boat_vertices[6], boat_vertices[9]},
{boat_vertices[6], boat_vertices[10], boat_vertices[9]},
{boat_vertices[6], boat_vertices[7], boat_vertices[10]},
{boat_vertices[7], boat_vertices[11], boat_vertices[10]},
{boat_vertices[7], boat_vertices[4], boat_vertices[11]},
{boat_vertices[4], boat_vertices[8], boat_vertices[11]},
{boat_vertices[8], boat_vertices[9], boat_vertices[12]},
{boat_vertices[9], boat_vertices[13], boat_vertices[12]},
{boat_vertices[9], boat_vertices[10], boat_vertices[13]},
{boat_vertices[10], boat_vertices[14], boat_vertices[13]},
{boat_vertices[10], boat_vertices[11], boat_vertices[14]},
{boat_vertices[11], boat_vertices[15], boat_vertices[14]},
{boat_vertices[11], boat_vertices[8], boat_vertices[15]},
{boat_vertices[8], boat_vertices[12], boat_vertices[15]},
{boat_vertices[16], boat_vertices[17], boat_vertices[20]},
{boat_vertices[17], boat_vertices[21], boat_vertices[20]},
{boat_vertices[17], boat_vertices[18], boat_vertices[21]},
{boat_vertices[18], boat_vertices[22], boat_vertices[21]},
{boat_vertices[18], boat_vertices[19], boat_vertices[22]},
{boat_vertices[19], boat_vertices[23], boat_vertices[22]},
{boat_vertices[19], boat_vertices[16], boat_vertices[23]},
{boat_vertices[16], boat_vertices[20], boat_vertices[23]},
{boat_vertices[4], boat_vertices[8], boat_vertices[16]},
{boat_vertices[8], boat_vertices[20], boat_vertices[16]},
{boat_vertices[5], boat_vertices[9], boat_vertices[17]},
{boat_vertices[9], boat_vertices[21], boat_vertices[17]},
{boat_vertices[6], boat_vertices[10], boat_vertices[18]},
{boat_vertices[10], boat_vertices[22], boat_vertices[18]},
{boat_vertices[7], boat_vertices[11], boat_vertices[19]},
{boat_vertices[11], boat_vertices[23], boat_vertices[19]}
};

// Define mast vertices
Vector3d mast_vertices[8] = {
{-0.1, 2.1, -0.1},
{-0.1, 2.1, 0.1},
{0.1, 2.1, 0.1},
{0.1, 2.1, -0.1},
{-0.1, 6, -0.1},
{-0.1, 6, 0.1},
{0.1, 6, 0.1},
{0.1, 6, -0.1}
};

// Define mast faces
Triangle mast_faces[12] = {
{mast_vertices[0], mast_vertices[1], mast_vertices[4]},
{mast_vertices[1], mast_vertices[5], mast_vertices[4]},
{mast_vertices[1], mast_vertices[2], mast_vertices[5]},
{mast_vertices[2], mast_vertices[6], mast_vertices[5]},
{mast_vertices[2], mast_vertices[3], mast_vertices[6]},
{mast_vertices[3], mast_vertices[7], mast_vertices[6]},
{mast_vertices[3], mast_vertices[0], mast_vertices[7]},
{mast_vertices[0], mast_vertices[4], mast_vertices[7]},
{mast_vertices[0], mast_vertices[1], mast_vertices[2]},
{mast_vertices[0], mast_vertices[2], mast_vertices[3]},
{mast_vertices[4], mast_vertices[5], mast_vertices[6]},
{mast_vertices[4], mast_vertices[6], mast_vertices[7]}
};

// Define front pole vertices
Vector3d front_pole_vertices[8] = {
{-0.05, 1.1, -0.05},
{-0.05, 1.1, 0.05},
{0.05, 1.1, 0.05},
{0.05, 1.1, -0.05},
{-0.05, 2.5, -0.05},
{-0.05, 2.5, 0.05},
{0.05, 2.5, 0.05},
{0.05, 2.5, -0.05}
};

// Define front pole faces
Triangle front_pole_faces[12] = {
{front_pole_vertices[0], front_pole_vertices[1], front_pole_vertices[4]},
{front_pole_vertices[1], front_pole_vertices[5], front_pole_vertices[4]},
{front_pole_vertices[1], front_pole_vertices[2], front_pole_vertices[5]},
{front_pole_vertices[2], front_pole_vertices[6], front_pole_vertices[5]},
{front_pole_vertices[2], front_pole_vertices[3], front_pole_vertices[6]},
{front_pole_vertices[3], front_pole_vertices[7], front_pole_vertices[6]},
{front_pole_vertices[3], front_pole_vertices[0], front_pole_vertices[7]},
{front_pole_vertices[0], front_pole_vertices[4], front_pole_vertices[7]},
{front_pole_vertices[0], front_pole_vertices[1], front_pole_vertices[2]},
{front_pole_vertices[0], front_pole_vertices[2], front_pole_vertices[3]},
{front_pole_vertices[4], front_pole_vertices[5], front_pole_vertices[6]},
{front_pole_vertices[4], front_pole_vertices[6], front_pole_vertices[7]}
};

// Define railing vertices
Vector3d railing_vertices[8] = {
{-1.2, 1.2, -0.05},
{-1.2, 1.2, 0.05},
{-1.2, 1.3, 0.05},
{-1.2, 1.3, -0.05},
{-1.2, 2.3, -0.05},
{-1.2, 2.3, 0.05},
{-1.2, 2.4, 0.05},
{-1.2, 2.4, -0.05}
};

// Define railing faces
Triangle railing_faces[12] = {
{railing_vertices[0], railing_vertices[1], railing_vertices[4]},
{railing_vertices[1], railing_vertices[5], railing_vertices[4]},
{railing_vertices[1], railing_vertices[2], railing_vertices[5]},
{railing_vertices[2], railing_vertices[6], railing_vertices[5]},
{railing_vertices[2], railing_vertices[3], railing_vertices[6]},
{railing_vertices[3], railing_vertices[7], railing_vertices[6]},
{railing_vertices[3], railing_vertices[0], railing_vertices[7]},
{railing_vertices[0], railing_vertices[4], railing_vertices[7]},
{railing_vertices[0], railing_vertices[1], railing_vertices[2]},
{railing_vertices[0], railing_vertices[2], railing_vertices[3]},
{railing_vertices[4], railing_vertices[5], railing_vertices[6]},
{railing_vertices[4], railing_vertices[6], railing_vertices[7]}
};

// Define cannon vertices
Vector3d cannon_vertices[8] = {
{1.05, 0.5, -0.05},
{1.05, 0.5, 0.05},
{1.05, 0.7, 0.05},
{1.05, 0.7, -0.05},
{1.05, 1.1, -0.05},
{1.05, 1.1, 0.05},
{1.05, 1.3, 0.05},
{1.05, 1.3, -0.05}
};

// Define cannon faces
Triangle cannon_faces[12] = {
{cannon_vertices[0], cannon_vertices[1], cannon_vertices[4]},
{cannon_vertices[1], cannon_vertices[5], cannon_vertices[4]},
{cannon_vertices[1], cannon_vertices[2], cannon_vertices[5]},
{cannon_vertices[2], cannon_vertices[6], cannon_vertices[5]},
{cannon_vertices[2], cannon_vertices[3], cannon_vertices[6]},
{cannon_vertices[3], cannon_vertices[7], cannon_vertices[6]},
{cannon_vertices[3], cannon_vertices[0], cannon_vertices[7]},
{cannon_vertices[0], cannon_vertices[4], cannon_vertices[7]},
{cannon_vertices[0], cannon_vertices[1], cannon_vertices[2]},
{cannon_vertices[0], cannon_vertices[2], cannon_vertices[3]},
{cannon_vertices[4], cannon_vertices[5], cannon_vertices[6]},
{cannon_vertices[4], cannon_vertices[6], cannon_vertices[7]}
};

/*
// Write vertices and faces to file
write_vertices_and_faces_to_file(hull_vertices, hull_faces, sizeof(hull_vertices) / sizeof(Vector3d), sizeof(hull_faces) / sizeof(Triangle), "boat.stl");
write_vertices_and_faces_to_file(mast_vertices, mast_faces, sizeof(mast_vertices) / sizeof
*/

// Function to write vertices and faces to STL file
void write_vertices_and_faces_to_file(Vector3d vertices[], Triangle faces[], int num_vertices, int num_faces, char* filename) {
// Open file for writing
FILE* fp;
fp = fopen(filename, "wb");

// Write number of triangles
fwrite(&num_faces, sizeof(int), 1, fp);

// Loop through faces and write each one to the file
int i;
for (i = 0; i < num_faces; i++) {
// Calculate face normal
Vector3d edge1 = subtract_vectors(faces[i].vertex2, faces[i].vertex1);
Vector3d edge2 = subtract_vectors(faces[i].vertex3, faces[i].vertex1);
Vector3d normal = cross_product(edge1, edge2);

// Write normal vector
fwrite(&normal, sizeof(float), 3, fp);

// Write vertices
fwrite(&faces[i], sizeof(Vector3d), 3, fp);

// Write attribute byte count (unused in this case)
unsigned short attribute_byte_count = 0;
fwrite(&attribute_byte_count, sizeof(unsigned short), 1, fp);
}

// Close file
fclose(fp);
}

// Function to subtract two vectors
Vector3d subtract_vectors(Vector3d vector1, Vector3d vector2) {
Vector3d result;
result.x = vector1.x - vector2.x;
result.y = vector1.y - vector2.y;
result.z = vector1.z - vector2.z;
return result;
}

// Function to calculate cross product of two vectors
Vector3d cross_product(Vector3d vector1, Vector3d vector2) {
Vector3d result;
result.x = vector1.y * vector2.z - vector1.z * vector2.y;
result.y = vector1.z * vector2.x - vector1.x * vector2.z;
result.z = vector1.x * vector2.y - vector1.y * vector2.x;
return result;
}

// Function to create a cylinder
void create_cylinder(Vector3d center, float radius, float height, Vector3d vertices[], Triangle faces[], int* num_vertices, int* num_faces) {
//

``````
``````
#include <stdio.h>

#define PI 3.14159265358979323846

typedef struct {
double x, y, z;
} vertex;

// Define boat dimensions
const double boat_length = 100.0;
const double boat_width = 30.0;
const double boat_height = 15.0;
const double mast_height = 60.0;
const double pole_height = 20.0;
const double railing_height = 5.0;
const double cannon_length = 5.0;

// Define boat vertices
vertex boat_verts[] = {
{0.0, 0.0, 0.0}, // 0
{boat_length, 0.0, 0.0}, // 1
{boat_length, 0.0, boat_width}, // 2
{0.0, 0.0, boat_width}, // 3
{0.0, boat_height, 0.0}, // 4
{boat_length, boat_height, 0.0}, // 5
{boat_length, boat_height, boat_width}, // 6
{0.0, boat_height, boat_width} // 7
};

// Define mast vertices
vertex mast_verts[] = {
{boat_length/2.0, boat_height, boat_width/2.0}, // 0
{boat_length/2.0, boat_height + mast_height, boat_width/2.0} // 1
};

// Define pole vertices
vertex pole_verts[] = {
{boat_length/2.0, boat_height + mast_height, boat_width/2.0}, // 0
{boat_length/2.0, boat_height + mast_height + pole_height, boat_width/2.0} // 1
};

// Define railing vertices
vertex railing_verts[] = {
{0.0, boat_height + railing_height, 0.0}, // 0
{boat_length, boat_height + railing_height, 0.0}, // 1
{boat_length, boat_height + railing_height, boat_width}, // 2
{0.0, boat_height + railing_height, boat_width} // 3
};

// Define cannon vertices
vertex cannon_verts[] = {
{0.0, boat_height + railing_height + cannon_radius, boat_width/2.0}, // 0
{cannon_length, boat_height + railing_height + cannon_radius, boat_width/2.0}, // 1
{cannon_length, boat_height + railing_height - cannon_radius, boat_width/2.0}, // 2
{0.0, boat_height + railing_height - cannon_radius, boat_width/2.0} // 3
};

// Calculate normal vector for a face
vertex calculate_normal(vertex a, vertex b, vertex c) {
vertex v1 = {b.x - a.x, b.y - a.y, b.z - a.z};
vertex v2 = {c.x - a.x, c.y - a.y, c.z - a.z};
vertex normal = {
(v1.y * v2.z) - (v1.z * v2.y),
(v1.z * v2.x) - (v1.x *

``````