Approximating sphere-ish area

I’m making a code to show how the volume of a sphere is found but, I want to make an equation that gives the area of the exact area of the object even when it’s at level 3 or 4

function setup()
    view = 2
    parameter.integer("level",3,65,12,setup1)
    AngleX = 90
    AngleY = 140
    AngleZ = 90
end

function setup1() 
    r =10  
    tab={}
    M,N=level,level
    for n=0,N do
        tab[n]={}
        for m=0,M do
            x= r * math.sin(math.pi * m/M) * math.cos(2*math.pi * n/N)
            y= r  * math.sin(math.pi * m/M) * math.sin(2*math.pi * n/N)
            z= r * math.cos(math.pi * m/M)
            tab[n][m]=vec3(x,y,z)
        end
    end
    sph={}
    for n=0,N-1 do
        for m=0,M-1 do
            table.insert(sph,tab[n][m])
            table.insert(sph,tab[n][m+1])
            table.insert(sph,tab[n+1][m+1])  
            table.insert(sph,tab[n][m])
            table.insert(sph,tab[n+1][m])
            table.insert(sph,tab[n+1][m+1])
        end
    end
    cols={}
    for z=1,#sph,6 do
        col=vec4(math.random(),math.random(),math.random(),1)
        for q=1,6 do
            table.insert(cols,col)
        end
    end
    sphere=mesh()
    sphere.vertices=sph
    sphere.colors=cols  
    approx = (4/3)*(math.pi)*(r^3)
    fontSize(25)
end
function dpoint(x,y,z)
    strokeWidth(15)
    pointd = mesh()
    pointd.vertices = {x,y,z}
    
end
function draw()  
    background(40, 40, 50)
    fill(255)
    text(approx,WIDTH/2+200,700) 
    perspective(view,WIDTH/HEIGHT)
    camera(2000,0,0,0,0,0,0,1,0)
    dpoint(1,1,1)
    pointd:draw()
    rotate(AngleX,1,0,0)
    rotate(AngleY,0,1,0)
    rotate(AngleZ,0,0,1)
    sphere:draw()
    
    text(r,100,100)

end

@LoopSpace I’m not good with the dot product stuff that’s why I’m asking this. The volume of a pyramid is 1/3AH where A is the area of the triangular base times the height of the pyramid. The height of the pyramid is the distance from the point of the pyramid to the center of the base. Each point of the triangle is on the surface of the sphere which means the length of each side is the radius of the sphere. Since the height is from a center point of the triangle, the height is going to be slightly less than the radius of the sphere. Is that being accounted for in your calculations above.

@dave1707

The height of the pyramid is the distance from the point of the pyramid to the center of the base

Not quite. It is the perpendicular height of the point of the pyramid. So imagine the pyramid being on a horizontal surface, then the height is the vertical height of the apex. That could well be different to the distance from the middle of the base.

Nevertheless,

the height is going to be slightly less than the radius of the sphere

is true. The answer to

Is that being accounted for in your calculations above.

is “Yes”. The n . w part calculates the correct distance (of course, that later gets subsumed into the triple product but the point is that it starts from the correct formula here).

@LoopSpace Thanks for the answer. When I looked at an example of calculating the area of a pyramid, it did show a perpendicular and I just assumed it was the center.

Why don’t you use the craft extension?

How?

@dmoeller To calculate the exact volume of a sphere based on the points that make up the sphere isn’t easy. At level 3 or 4 it’s easier to see what needs to be calculated. You would need to calculate the volume at each layer. Near the top you have angled triangles that enclose an area and near the middle you have angled rectangles that enclose an area.

By triangulating the surface of the sphere you are dividing the sphere into triangular-based prisms (with apex at the origin). The volume of a prism is 1/3 times its base area times its height (taken perpendicular to the base area).

Let’s suppose that the vertices of the triangle are at u, v, and w. We also need the unit normal to the plane that these make, let’s call that n.

The base area is the area of the triangle. We’ll figure this out in a moment, but for now let’s call it A.

To find the perpendicular height, we use the fact that the dot product of two vectors is related to the perpendicular projection of one of the vectors along the other. So the absolute value of n . w is the perpendicular distance of the triangle from the origin (it doesn’t matter which of u, v, or w is chosen here).

Thus the volume of the prism is (n . w) A /3.

Now return to the area of the triangle. It turns out that this is related to the cross product, in that up to sign, (u - w) x (v - w) = 2 A n.

Putting these together, the volume of the prism is (up to sign):

((u - w) x (v - w)) . w / 6

Now it turns out that after expanding out the brackets involving the cross product, there are a lot of terms that disappear upon dotting with w, so the simpler formula is:

(u x v) . w /6

In lua code:

math.abs( u:cross(v):dot(w)/6)

so using that formula you could add up the areas of all the triangles in your mesh to get the actual volume of your spherical approximation.