Skip to content

Instantly share code, notes, and snippets.

@MatthewBlanchard
Created May 29, 2011 04:06
Show Gist options
  • Save MatthewBlanchard/997453 to your computer and use it in GitHub Desktop.
Save MatthewBlanchard/997453 to your computer and use it in GitHub Desktop.
Generating some stuff
#include "math.h"
#include "stdlib.h"
#include "geometry.h"
//Inputs:
//verts: The higher the number the more vertices and the smoother the cylinder, number of verts on each side.
//size: distance of the vertices from the center.
//width: the thickness of the cylinder
//return: pointer to memory on heap, contains points in size verts*2
mesh generateCylinder(int verts, float size, float width)
{
point *vertData = malloc(sizeof(point)+sizeof(point)*verts*2);
if(vertData == NULL) exit(0);
float angIncrement = (2*M_PI)/verts;
int i;
for(i = 0; i < verts; i++)
{
float curang = angIncrement*i;
point vertPoint = {cos(curang), sin(curang), 0.0f};
vertData[i*2] = vertPoint;
vertPoint.z += width;
vertData[i*2+1] = vertPoint;
}
vertData[verts*2+1] = vertData[0];
mesh cylinderMesh;
cylinderMesh.vertCount = verts*2+1;
cylinderMesh.vertices = vertData;
return cylinderMesh; //we're good.
}
typedef struct {
float x;
float y;
float z;
} point;
typedef struct {
int vertCount;
point* vertices;
} mesh;
mesh generateCylinder(int verts, float size, float width);
#include "stdio.h"
#include "geometry.h"
int main()
{
FILE *vertFile = fopen("verts.txt", "w");
if(vertFile == NULL)
return 1;
mesh cylinder = generateCylinder(3, 2, 1);
point* verts = cylinder.vertices;
int i;
for(i = 0; i < cylinder.vertCount; i++)
{
fprintf(vertFile, "%lf %lf %lf\n", verts[i].x, verts[i].y, verts[i].z);
}
fclose(vertFile);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment