# 3d cylinder/sphere

Recently started looking at 3d, this is my attempt at a cylinder based on antiprism construction

``````-- cylinder
-- creates a cylinder based on an antiprismatic construction
-- variables to be reset are
-- size: the radius of the cylinder
-- poly: the number of side lengths a single lamina circle is split into
-- dz: half the height of the cylinder
-- (called dz as this code is to be further used to create a sphere)
-- p: will pinch the top if less than 1 and flare it out if greater than 1...
-- ...experiment with p values between 0 and 2 to begin with
-- NOTE place your own texture at line 70

function setup()
print("Make sure to read first piece of code regarding variables")
parameter.number("height",-300,300,0)
parameter.number("angleX",-360,360,0)
parameter.number("angleY",-360,360,0)
parameter.number("angleZ",-360,360,0)

-- cylinder variables
size=40
poly=10
dz=25
z=0    -- just leave this alone...it's basically the z plane level
r=1
p=0.25

-- radian calculations to be used in initial loop
posA=(2*math.pi)/poly
posB=(2*math.pi)/(2*poly)

-- 2 tables for the lower ring of vertices and the upper ring of vertices to placed into
Va={}
Vb={}

-- initial loop, creates 2 rings (a lower and upper) of vertex points
for  i=1,poly do
fac=(1-(z^2))^0.5
table.insert(Va,i,vec3(size*math.cos(posA*i)/fac,
size*math.sin(posA*i)/fac,
z-dz ))
table.insert(Vb,i,vec3(p*size*math.cos(((2*i)-1)*posB)/fac,
p*size*math.sin(((2*i)-1)*posB)/fac,
z+dz ))
end
Va[poly+1]=Va -- places an additional repeated vertex for following loop
Vb[poly+1]=Vb -- places an additional repeated vertex for following loop

-- 'zips' the two rings together into a single table cyl which can be used
-- to assign vertices to the cylindrical mesh

cyl={}
for i=1,poly do
table.insert(cyl,Va[i])
table.insert(cyl,Vb[i])
table.insert(cyl,Va[i+1])
table.insert(cyl,Vb[i])
table.insert(cyl,Va[i+1])
table.insert(cyl,Vb[i+1])
end

-- assigns texture co-ordinates to table cyltex so as to apply texture to mesh
cyltex={}
for i=1,2*poly do
table.insert(cyltex,vec2(0,0))
table.insert(cyltex,vec2(1,1))
table.insert(cyltex,vec2(0,1))
end

-- creates mesh
cy=mesh()
cy.vertices=cyl
cy:setColors(255,255,255,255)
cy.texCoords =cyltex

end

function draw()
background(0)
perspective(45, WIDTH/HEIGHT)
camera(0,height,200, 0,0,0, 0,1,0)
--    pushMatrix()
rotate(angleX,1,0,0)
rotate(angleY,0,1,0)
rotate(angleZ,0,0,1)
cy:draw()
--    ortho()
--    viewMatrix(matrix())
--    popMatrix()

end

``````

Next step spheres…

Above is inspired by an attempt to access a link kindly sent to me by @Ignatz

…which I couldn’t access/find (perhaps corrupted?), regarding sphere construction…so I thought I’d give it a go myself…next step is to use repeated laminae to create the sphere…

In the meantime… @Jmv38 is it still available somewhere? Would love to look at other’s methods.

Try this link, it contains all his projects

http://jmv38.comze.com/CODEAbis/server.php

You will certainly also enjoy this

http://twolivesleft.com/Codea/Talk/discussion/comment/26216

Thanks @Ignatz. More study…

If you want some inspiration

https://gist.github.com/tnlogy/5146337

it links to a blog about it also.

Thanks for that @tnlogy. Lovely example of icosahedron approaching sphericity… Have you seen this before
Dymaxion map unfolding - YouTube
Buckminster Fuller rocks!..or maybe more credit should go to the programmer!

Also @tnlogy… Do I need to sign in to find the blog link?

The link was hidden in the code Thanks again @tnlogy that’s great. I was wondering about splitting faces only yesterday. Much appreciated.

Here’s the same code converted for a sphere…has a nice geometry to it…I’ve used a grey granite like texture and looks pretty good…

``````-- sphere
-- creates a cylinder based on an antiprismatic construction
-- then shapes the cylinder wall to create a sphere
-- variables to be reset are found between lines 14 and 19
-- NOTE place your own texture at line 88
function setup()
print("Make sure to read first piece of code regarding variables")
parameter.number("height",-300,300,0)
parameter.number("angleX",-360,360,0)
parameter.number("angleY",-360,360,0)
parameter.number("angleZ",-360,360,0)

-- cylinder variables
dia = 200 --diameter of cylinder
radi = dia/2 -- radius of cylinder
poly = 8 -- the number of polygons that the cylinder ring has been constructed from
radDiv = 10 -- pertaining to the sphere and the number of slices it is to be made from
floor = -radi -- this sets the origin at the centre of the sphere
h = dia/radDiv -- will give the height of each ring module

-- radian calculations to be used in initial loop based on the division of the circle into
-- poly number of points
pos=(2*math.pi)/poly

-- tables for the lower ring of vertices and the upper ring of vertices
Va={}
Vb={}
-- tables for the storage of vertex points for mesh and texture mapping points for mesh
cyl={}
cyltex={}

for k=1,radDiv do -- radDiv defines the number of layers the sphere is to be split into
yA = -1 + (2*(k-1)/radDiv)
yB = -1 + (2*(k)/radDiv)
varA = 1-((yA)^2)
varB = 1-((yB)^2)
r = math.sqrt(varA) -- these variables define the coefficients of the radius of
p = math.sqrt(varB) -- each ring, i.e. they shape the sphere, if you just set
-- both to one you will get a cylinder constructed of radDiv
-- number of rings
-- r=1
-- p=1

-- secondary loop, creates 2 rings (a lower and upper) of vertex points
for  i=1,poly do
thetA=(i-((k-1)*0.5))
thetB=(i-(k*0.5))

table.insert(Va,i,vec3(r*(dia/2)*math.cos(pos*thetA),
r*(dia/2)*math.sin(pos*thetA),
floor))
table.insert(Vb,i,vec3(p*(dia/2)*math.cos(pos*thetB),
p*(dia/2)*math.sin(pos*thetB),
floor + h))
end
Va[poly+1]=Va -- places an additional repeated vertex for following loop
Vb[poly+1]=Vb -- places an additional repeated vertex for following loop

-- 'zips' the two rings together into a single table cyl which can be used
-- to assign vertices to the cylindrical mesh

for i=1,poly do
table.insert(cyl,Va[i])
table.insert(cyl,Vb[i])
table.insert(cyl,Va[i+1])
table.insert(cyl,Vb[i])
table.insert(cyl,Va[i+1])
table.insert(cyl,Vb[i+1])
end

-- assigns texture co-ordinates to table cyltex so as to apply texture to mesh

for i=1,2*poly do
table.insert(cyltex,vec2(0,0))
table.insert(cyltex,vec2(1,1))
table.insert(cyltex,vec2(0,1))
end

-- creates mesh
cy=mesh()
cy.vertices=cyl
cy:setColors(255,255,255,255)
cy.texCoords =cyltex

floor = floor + h
end
end

function draw()
background(0)
perspective(45, WIDTH/HEIGHT)
camera(0,height,300, 0,0,0, 0,1,0)
--    pushMatrix()
rotate(angleX,1,0,0)
rotate(angleY,0,1,0)
rotate(angleZ,0,0,1)
cy:draw()
--    ortho()
--    viewMatrix(matrix())
--    popMatrix()

end
``````