Skip to content

Instantly share code, notes, and snippets.

# jungchris/Tetrahedron.m Last active Feb 5, 2016

Objective-C Method to Create a Four Sided 3-D Polyhedron
 // Method to calc a four sided polyhedron - (void)createTetrahedron { // constants & vars based on four faces (triangles) int trianglesAroundZ = 3; // arbitrary choice of z axis float seedPointAngle = 90.0; // first point P1 vertex angle from x-y plane float distanceToOrigin = 1.0; float angleBetweenPoints = 360/trianglesAroundZ; // determine cone aspects float angleAlpha = angleBetweenPoints - seedPointAngle; float coneHeight = sinf(angleAlpha * M_PI / 180) * distanceToOrigin; // based on 30 degrees float coneRadius = sinf((seedPointAngle-angleAlpha) * M_PI / 180) * distanceToOrigin; // first point is arbitrary PyramidVertex P0 = {0,0,distanceToOrigin}; // remaining three are based on cone float point1X = coneRadius * sinf((0 * angleBetweenPoints) * M_PI / 180); // zero degrees relative to y float point1Y = coneRadius * cosf((0 * angleBetweenPoints) * M_PI / 180); PyramidVertex P1 = { point1X, point1Y, -coneHeight }; float point2X = coneRadius * sinf((1 * angleBetweenPoints) * M_PI / 180); // 120 degrees float point2Y = coneRadius * cosf((1 * angleBetweenPoints) * M_PI / 180); PyramidVertex P2 = { point2X, point2Y, -coneHeight }; float point3X = coneRadius * sinf((2 * angleBetweenPoints) * M_PI / 180); // 240 degrees float point3Y = coneRadius * cosf((2 * angleBetweenPoints) * M_PI / 180); PyramidVertex P3 = { point3X, point3Y, -coneHeight }; // make four traingles [self makeTriangle:0 withPointA:P0 pointB:P1 pointC:P2]; [self makeTriangle:1 withPointA:P0 pointB:P2 pointC:P3]; [self makeTriangle:2 withPointA:P0 pointB:P3 pointC:P1]; [self makeTriangle:3 withPointA:P1 pointB:P2 pointC:P3]; // bottom triangle } // helper method to take three points and make a populate array of triangles // uses x,y,z points - (void)makeTriangle:(int)isoIndex withPointA:(PyramidVertex)A pointB:(PyramidVertex)B pointC:(PyramidVertex)C { // the stride (offset) is 3, for each point of a triangle int offset = isoIndex * 3; vertices[offset].positionCoords = A.positionCoords; vertices[offset+1].positionCoords = B.positionCoords; vertices[offset+2].positionCoords = C.positionCoords; // used to calc distance float xd = vertices[offset].positionCoords.x; float yd = vertices[offset].positionCoords.y; float zd = vertices[offset].positionCoords.z; // check distance to origin of each point NSLog(@"distance from origin x: %f", sqrt(xd*xd + yd*yd + zd*zd) ); }
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.