Plots circle in 3D space

Hi,
I’m trying to draw a circle in 3D space.

Example code is:

using Plots
using LinearAlgebra
using Transducers



function _test()
    fig = plot()
    p = zeros(3)
    l = 0.3
    body_U = [0, 0, 1]
    plot!(fig, circle_shape(p + l*[1, 0, 0], l/2.5, body_U);
          st=:surface, opacity=0.25, c=:green,
         )
end

"""
c: centre
r: radius
h: direction of normal vector
"""
function circle_shape(c, r, h)
    normal_vec1 = nothing
    while true
        rand_vec = rand(3)
        normal_vec1 = cross(h, rand_vec)
        if norm(normal_vec1) > 0
            normal_vec1 = normal_vec1 / norm(normal_vec1)
            break
        end
    end
    normal_vec2 = cross(h, normal_vec1)
    normal_vec2 = normal_vec2 / norm(normal_vec2)
    θs = LinRange(0, 2*π-1e-2, 500)
    circle = θs |> Map(θ -> c + r*cos(θ)*normal_vec1 + r*sin(θ)*normal_vec2) |> collect
    circle_E = circle |> Map(p -> p[2]) |> collect
    circle_N = circle |> Map(p -> p[1]) |> collect
    circle_U = circle |> Map(p -> -p[3]) |> collect
    circle_E, circle_N, circle_U
end

But it looks like a cylinder as
image

What did I do something wrong?

[Note] the result when I don’t specify the seriestype:
image

Results seem to look good when specifying st=:line?
Plots_gr_oriented_disk

1 Like

I’m gonna try it. Thanks!

EDIT: it works very well!

1 Like