c++ - Algorithm for a geodesic sphere -


i have make sphere out of smaller uniformely distributed balls. think optimal way build triangle-based geodesic sphere , use vertices middle points of balls. fail write algorithm generating vertices. answer in c++ or pseudo-code better.

example of geodesic sphere: http://i.stack.imgur.com/inqfp.png

using link @muckle_ewe gave me, able code following algorithm: outside main()

class vector3d {  // pretty standard vector class public:     double x, y, z;     ... }  void subdivide(const vector3d &v1, const vector3d &v2, const vector3d &v3, vector<vector3d> &sphere_points, const unsigned int depth) {     if(depth == 0) {         sphere_points.push_back(v1);         sphere_points.push_back(v2);         sphere_points.push_back(v3);         return;     }     const vector3d v12 = (v1 + v2).norm();     const vector3d v23 = (v2 + v3).norm();     const vector3d v31 = (v3 + v1).norm();     subdivide(v1, v12, v31, sphere_points, depth - 1);     subdivide(v2, v23, v12, sphere_points, depth - 1);     subdivide(v3, v31, v23, sphere_points, depth - 1);     subdivide(v12, v23, v31, sphere_points, depth - 1); }  void initialize_sphere(vector<vector3d> &sphere_points, const unsigned int depth) {     const double x = 0.525731112119133606;     const double z = 0.850650808352039932;     const vector3d vdata[12] = {         {-x, 0.0, z}, { x, 0.0, z }, { -x, 0.0, -z }, { x, 0.0, -z },         { 0.0, z, x }, { 0.0, z, -x }, { 0.0, -z, x }, { 0.0, -z, -x },         { z, x, 0.0 }, { -z, x, 0.0 }, { z, -x, 0.0 }, { -z, -x, 0.0 }     };     int tindices[20][3] = {         {0, 4, 1}, { 0, 9, 4 }, { 9, 5, 4 }, { 4, 5, 8 }, { 4, 8, 1 },         { 8, 10, 1 }, { 8, 3, 10 }, { 5, 3, 8 }, { 5, 2, 3 }, { 2, 7, 3 },         { 7, 10, 3 }, { 7, 6, 10 }, { 7, 11, 6 }, { 11, 0, 6 }, { 0, 1, 6 },         { 6, 1, 10 }, { 9, 0, 11 }, { 9, 11, 2 }, { 9, 2, 5 }, { 7, 2, 11 }     };     for(int = 0; < 20; i++)         subdivide(vdata[tindices[i][0]], vdata[tindices[i][1]], vdata[tindices[i][2]], sphere_points, depth); } 

then in main():

vector<vector3d> sphere_points; initialize_sphere(sphere_points, depth);  // depth should subdivision depth for(const vector3d &point : sphere_points)     const vector3d point_tmp = point * radius + center;  // sphere want draw,  iterate on precomputed sphere points , linear function translate sphere center , chose proper radius 

you need use initialize_sphere() once , use result every sphere want draw.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -