Latitude Point Distribution
C++. This is for symmetrical distribution of points in each latitude on a sphere. As an input, different number of points for each latitude can be assigned.
Top and bottom coordinates, of course, are the easy ones. Here, basic principle is to divide the sphere into two hemispheres. After doing calculations for the north hemisphere, reversing z axis as a mirror effect would be enough for south hemisphere.
Latitude heights are calculated by radius*cos(latitude*arcAngle) and angle value is calculated by dividing 180 degrees by total number of arcs. Then, latitude radius is calculated by radius*sin(latitude*arcAngle) for determining (x,y) points where z axis is the height.
As a final step, point distribution algorithm with rotation will calculate the coordinates of sphere points on each latitude.
#include <iostream> #include <cmath> #include <vector> #include <numeric> #include <iomanip> struct SpherePoint { SpherePoint(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {} float x, y, z; }; void LatitudePointDistribution ( float const _radius, float const _verticalAngle, int const _latitude, int const _numUnits, int & _refIndex, float const _refAngle, std::vector<SpherePoint> & _dataSet ); float RotationalAngleCalculation ( int const _numUnitsOfPreviousLatitude, int const _numUnitOfCurrentLatitude ); int main() { std::vector<int> latitudePoints = {4, 6, 7, 9, 12, 13, 16}; // north hemisphere from top to middle std::vector<SpherePoint> sphereDataSet; if(latitudePoints.empty()) return 0; // Sphere points initialising for top, bottom, and two hemispheres int const totalPoints = 2 * (1 + std::accumulate(latitudePoints.begin(), latitudePoints.end(), 0)); for(int i = 0; i < totalPoints; ++i) { sphereDataSet.push_back(SpherePoint(0.0f, 0.0f, 0.0f)); } // COORDINATE CALCULATIONS // // Firstly, calculations for north hemispere will be done. There are seven latitudes plus top unit itself. // Latitude heights are calculated by radius*cos(latitude*arcAngle) where radius is 1. // Angle value is calculated by dividing 180 degrees by total number of arcs. // Latitude radius is calculated by radius*sin(latitude*arcAngle) for determining (x,y) points. z axis is the height. // Then, point distribution algorithm will calculate the coordinates of sphere points on each latitude. float const radius = 1.0f; int refIndex = 0; // Top point coordinates sphereDataSet[0] = SpherePoint(0.0f, 0.0f, 1.0f); refIndex++; // Latitude distribution calculation with its points for north hemisphere int const numLatitudes = latitudePoints.size(); float const arcAngle = 180.0f / (2 * numLatitudes + 1); float refAngle = 0.0f; for (int i = 0; i < numLatitudes; ++i) { LatitudePointDistribution(radius, arcAngle, i+1, latitudePoints[i], refIndex, refAngle, sphereDataSet); if(i >= numLatitudes - 1) break; refAngle += RotationalAngleCalculation(latitudePoints[i], latitudePoints[i+1]); } // Coordinate calculations for south hemisphere excluding bottom point for (int i = --refIndex; i > 0; --i) { // Reversing the z axis for mirror effect sphereDataSet[++refIndex] = SpherePoint(sphereDataSet[i].x, sphereDataSet[i].y, -sphereDataSet[i].z); } // Bottom point coordinates refIndex++; sphereDataSet[refIndex] = SpherePoint(0.0f, 0.0f, -1.0f); // PRINTING RESULTS std::cout << "The Coordinates of Sphere Points \n" << std::endl; std::cout << std::fixed; std::cout << std::setprecision(4); for(int i = 0; i < totalPoints; ++i) { std::cout << i+1 <<". (" << sphereDataSet[i].x << "," << sphereDataSet[i].y << "," << sphereDataSet[i].z << ")" << std::endl; } return 0; } //////////////////////////////////////////////////////////////////////////////// void LatitudePointDistribution ( float const _radius, float const _verticalAngle, int const _latitude, int const _numUnits, int & _refIndex, float const _refAngle, std::vector<SpherePoint> & _dataSet ) { float const degToRadian = 0.0174533f; // (3.1416f / 180.0f) float const z = _radius * cosf(_latitude * (_verticalAngle * degToRadian)); float const latitudeRadius = _radius * sinf(_latitude * (_verticalAngle * degToRadian)); float const horizontalAngle = 360.0f / _numUnits; for (int i = 0; i < _numUnits; ++i) { float const x = latitudeRadius * cosf((i * horizontalAngle + _refAngle) * degToRadian); float const y = latitudeRadius * sinf((i * horizontalAngle + _refAngle) * degToRadian); _dataSet[i + _refIndex] = SpherePoint(x, y, z); } _refIndex += _numUnits; } //////////////////////////////////////////////////////////////////////////////// float RotationalAngleCalculation ( int const _numUnitsOfPreviousLatitude, int const _numUnitOfCurrentLatitude ) { // Rotational angle return 180.0f * ((1.0f / _numUnitsOfPreviousLatitude) - (1.0f / _numUnitOfCurrentLatitude)); }