# General Plotting Code for Cone in 3D with GLMakie or Plots

**URL:** <https://discourse.julialang.org/t/general-plotting-code-for-cone-in-3d-with-glmakie-or-plots/92104>\
**Category:** General Usage\
**Tags:** plotting, math\
**Created:** [December 25, 2022, 10:15am UTC](https://discourse.julialang.org/t/general-plotting-code-for-cone-in-3d-with-glmakie-or-plots/92104 "2022-12-25T10:15:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 25, 2022, 10:15am UTC](https://discourse.julialang.org/t/general-plotting-code-for-cone-in-3d-with-glmakie-or-plots/92104/1 "2022-12-25T10:15:38Z")

</div>

Hi all,

so I want to plot a general cone of pyramid and a right circular cone, I get this formula:

![Capture d’écran_2022-12-25_17-12-30](https://global.discourse-cdn.com/julialang/original/3X/c/2/c216ec86f57a9440e34917a56b8f73a4553d0599.png)

But I think the plotting does not use the volume formula. Because of this torus code for GLMakie that I have:

```julia
using GLMakie
using ColorSchemes
cmap3 = get(colorschemes[:plasma], LinRange(0,1,100))
	cmap4 = [(cmap3[i],0.65) for i in 1:100]
	ξ = -40:1:40
	c = 20
	a = 15
	torus = [((hypot(X,Y)-c)^2+Z^2-a^2) for X in ξ, Y in ξ, Z in ξ]
	volume(torus, algorithm = :iso,isovalue =100,isorange =50, colormap=cmap4)
	current_figure()

```

For general cone of pyramid, If the based is made of a closed random curves, then we might need to plot in x-y plane the curves then plot the top of pyramid afterwards.

For the right circular cone, I believe there is a general formula already.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 25, 2022, 5:56pm UTC](https://discourse.julialang.org/t/general-plotting-code-for-cone-in-3d-with-glmakie-or-plots/92104/2 "2022-12-25T17:56:18Z")

</div>

You could conveniently define both as parametric surfaces:

```julia
using Plots; plotlyjs()

z, Θ = range(0,10,200), range(0,2π,300)

# Right-cone:
X = [u * cos(v) for u in z, v in Θ]
Y = [u * sin(v) for u in z, v in Θ]
Z = [-2u for u in z, _ in Θ]
surface(X, Y, Z, size=(600,600), cbar=:none, legend=false)

# Trefoil cone:
X = [u/3 * (sin(v) + 2*sin(2v)) for u in z, v in Θ]
Y = [u/3 * (cos(v) - 2*cos(2v)) for u in z, v in Θ]
Z = [-2u for u in z, _ in Θ]
surface(X, Y, Z, size=(600,600), cbar=:none, legend=false)

```

![Plots_plotlyjs_right_cone_parametric_surface](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a67fd1235b698c8e7474c4d71f0872b0440417b.png)  
 ![Plots_plotlyjs_trefoil_cone_parametric_surface](https://global.discourse-cdn.com/julialang/original/3X/4/4/4446d4a90712ec445849ceccfe4afc6f0a34ab1b.png)

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [December 25, 2022, 7:51pm UTC](https://discourse.julialang.org/t/general-plotting-code-for-cone-in-3d-with-glmakie-or-plots/92104/3 "2022-12-25T19:51:50Z")

</div>

From your questions posted on this forum it seems that you are teaching math. Hence you should explain to your students how is parameterized a general cone,  
defined by its vertex V, given as a 3 vector, and a closed (but not necessarily) planar curve,

```julia
x = x(u)
y = y(u)
z= b # b from base plane; usually b=0

```

u \in [\alpha, \beta]  
.  
The cone is generated by all segments of line connecting its vertex, V, with the points  
(x(u), y(u), b), on the base curve.  
Such a segment has the equations:  
\displaystyle\frac{X-V[1]}{x(u)-V[1]} = \displaystyle\frac{Y-V[2]}{y(u)-V[2]} =\displaystyle\frac{Z-V[3]}{b-V[3]}=v  
Hence:

```julia
X = v(x(u)-V[1])+V[1]
Y = v(y(u)-V[2])+V[2]
Z = v(b-V[3])+V[3]

```

u \in [\alpha, \beta], v\in[0,1]  
Example:

```julia
vert=[0, 0.75, 3] #cone vertex
base = 0 #the cone base is included within the plane z=base (here z=0)
#functions that define the cone base parameterization
x(u) = cos(u)
y(u) = sin(u)+ sin(u/2);
m, n = 72, 30
u= range(0, 2π, length=m)
v = range(0, 1, length=n)
us = ones(n)*u'
vs = v*ones(m)'
#Surface parameterization
X = @. vs* (x(us)-vert[1]) + vert[1]
Y = @. vs* (y(us)-vert[2]) + vert[2]
Z = @. vs*(base-vert[3]) + vert[3];
surface(X, Y, Z, size=(600,600), cbar=:none, legend=false) #line copied from Rafael 

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 26, 2022, 10:24am UTC](https://discourse.julialang.org/t/general-plotting-code-for-cone-in-3d-with-glmakie-or-plots/92104/4 "2022-12-26T10:24:25Z")

</div>

Yes 100 for you, I was a Mathematics teacher for Junior High School students in Papua, but it was 2 years ago. Afterwards, I still love learning Mathematics even if I no longer teach, now I am learning the undergraduate Mathematics, from Calculus, It would be lovely to teach Math again in the future.

This is the first time I learn Calculus with Julia from bottom, it is rewarding knowing the application of this is enormous.

Thanks for this I will use your code and learn the parameterization, vertex, etc.
