# I Have PlotlyJS Codes to Plot Inverted Cone and Cylinder, How to Combine them into One?

Hi all,

I have code to plot each of the cylinder and cone from this discourse. How to combine them into one, so the inverted cone will be inside the cylinder? The color needs to be adjusted as well, can we make a grid too? Tikz Latex can make grids with `foreach` command…

this is the codes for cylinder:

``````using Plots
plotlyjs()

# If x, y, z are vectors then it won't generate a surface
# for a parameterized surface x,y,z should be matrices:
# Check for: typeof(X), typeof(Y), typeof(Z)

r = 5
h = 3
m, n =200, 150
u = range(0, 2pi, length=n)
v = range(0, h, length=m)

us = ones(m)*u'
vs = v*ones(n)'
#Surface parameterization
X = r*cos.(us)
Y = r*sin.(us)
Z = vs
Plots.surface(X, Y, Z, size=(600,600), cbar=:none, legend=false)
``````

for inverted cone:

``````using Plots; plotlyjs()

X(r,theta) = r * cos(theta)
Y(r,theta) = r * sin(theta)
Z(r,theta) = r

rs = range(0, 2, length=50)
ts = range(0, 2pi, length=50)

surface(X.(rs',ts), Y.(rs', ts), Z.(rs', ts))
``````

In the cone parameterization the radius is variable, and with your rs definition, the cone height, h=maximum(rs)=2. Hence we have to draw of cylinder of radius 2, and the same height, h=2. Since the cylinder radius is constant, it is not recommended to use r as parameter. I’m using u, v, that are “classical” variables for a surface parameterization, and thus avoid confusion between variable and constant radius for the two surfaces:

``````function cyl_cone(; h =2, m=50, n=50)
#cone parameterizarion u∈[0, 2π], v∈[0, h];
X(u,v) = v*cos(u)
Y(u,v) = v*sin(u)
Z(u,v) = v
#cylinder circumscribed to the above cone; u∈[0, 2π], v∈[0, h]
x(u, v) = h*cos(u)
y(u, v) = h*sin(u)
z(u, v) = v
u=range(0, 2pi, length=m)
v = range(0, h, length=n)
plt =surface(x.(u, v'), y.(u, v'), ones(m) *v')
surface!(plt, (X.(u, v'), Y.(u, v'), Z.(u, v')))
return plt
end
plt =cyl_cone(;)
``````
1 Like ``````using Plots; plotlyjs()

function cyl_cone(; h =2, m=50, n=50, fillalpha=0.6)
#cone parameterizarion u∈[0, 2π], v∈[0, h];
X(u,v) = v*cos(u)
Y(u,v) = v*sin(u)
Z(u,v) = v
#cylinder circumscribed to the above cone; u∈[0, 2π], v∈[0, h]
x(u, v) = h*cos(u)
y(u, v) = h*sin(u)
z(u, v) = v
u=range(0, 2pi, length=m)
v = range(0, h, length=n)
plt =surface(x.(u, v'), y.(u, v'), ones(m) *v', fillalpha=0.7)
surface!(plt, (X.(u, v'), Y.(u, v'), Z.(u, v')))
wireframe!(plt, (X.(u, v'), Y.(u, v'), Z.(u, v')))
return plt
end
plt =cyl_cone(;)
``````

To get the right wireframe you should plot a number p, of cone generatrices, and a number q of circles.
I explain how to get these lines, because just now I have no access to a computer to write the corresponding code:

`````` p, q = 12, 10
u= range(0, 2pi, length=p)
v = range( 0, h, length=q)
``````

For each uw= u[k], k =1,…,p

``````x=v*cos(uw)
y=v*sin(uw)
z=v
``````

give the points on a cone generatrice, while
for each vw=v[j], j=1,…,q

``````X= vw*cos(u)
Y=vw*sin(u)
Z= vw
``````

we get the points on a cone circle.

1 Like