Sphere trianglutaion
The first method - using latitude and longitude
Compute points of the sphere using latitude and longitude.
If sphere centre is [a, b, c] and radius is r:
x = a + r * cos(θ) * cos(φ)
y = b + r * sin(φ)
z = c + r * sin(θ) * cos(φ)
θ ∈ <0, 2*π)
φ ∈ <0, π)
Pseudocode
a,b,c = sphere's centre
r = sphere radius
angle = angle of one step in degrees
r2 := 360 / angle
p2 := 180 / angle
for y := -r2 to r2 do
cy := cos(y*angle)
cy1 := cos((y+1)*angle)
sy := sin(y*angle)
sy1 := sin((y+1)*angle)
for i := -p2 to p2 do
ci := cos(i*angle)
si := sin(i*angle)
trianglePoint(a+r * ci*cy, b+r*sy, c+r * si*cy))
trianglePoint(a+r * ci*cy1, b+r*sy1, c+r * si*cy1)
forend
forend
Download executable, source code and project file for Code::Blocks
The second method - using subdivision
Pseudocode is for sphere with [0, 0, 0] centre. If you want your sphere to have different centre, just move the sphere you get from pseudocode.
T = set of cube's triangles
steps = subdivision steps
for i := 1 to steps do
Tnew = {}
for each t ∈ T do
midab = (t.a + t.b)/2;
midab = midab * length(t.a) / length(midab)
tnew1 = triangle(t.a, t.c, midab)
tnew2 = triangle(t.b, t.c, midab)
add tnew1 to Tnew
add tnew2 to Tnew
forend
T = Tnew
forend
Advantage of this method is a better distribution of triangles around the sphere's poles.
Download executable, source code and project file for Code::Blocks
You must press "s" multiple times in the beggining to get a sphere from a cube
Controls:
use mouse to rotate the sphere
press "w" - render wireframe model
press "f" - render solid model
press "s" - subdivide
Discussion: