Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Created October 14, 2021 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terasakisatoshi/31faf2c25749ae9330e75d34319dbb45 to your computer and use it in GitHub Desktop.
Save terasakisatoshi/31faf2c25749ae9330e75d34319dbb45 to your computer and use it in GitHub Desktop.
GeometryRecipes.jl
module GeometryRecipes
using RecipesBase
using StaticArrays:FieldVector
using Random
# types
export Point2D, Point3D, Circle2D, Circle3D
struct Point2D{T} <: FieldVector{2,T}
x::T
y::T
end
struct Point3D{T} <: FieldVector{3,T}
x::T
y::T
z::T
end
struct Circle2D{T}
p::Point2D{T}
r::T
end
struct Circle3D{T}
p::Point3D{T}
r::T
end
const GeometryObj = Union{Point2D,Point3D,Circle2D,Circle3D}
Random.rand(rng::AbstractRNG, ::Random.SamplerType{Point2D}) = Point2D(rand(rng, 2))
Random.rand(rng::AbstractRNG, ::Random.SamplerType{Circle2D}) = Circle2D(Point2D(rand(rng, 2)), rand(rng))
Random.rand(rng::AbstractRNG, ::Random.SamplerType{Point3D}) = Point3D(rand(rng, 3))
Random.rand(rng::AbstractRNG, ::Random.SamplerType{Circle3D}) = Circle3D(Point3D(rand(rng, 3)), rand(rng))
# recipes
@recipe function f(pt::Point2D)
seriestype := :scatter
label --> :none
[(pt.x, pt.y)]
end
@recipe function f(pt::Point3D)
seriestype := :scatter
label --> :none
[pt.x], [pt.y], [pt.z]
end
@recipe function f(c::Circle2D)
seriestype := :shape
linecolor --> :black
aspect_ratio --> :equal
fillalpha --> 0.25
label --> :none
θ = 0:0.1:2π
c.p.x .+ c.r .* cos.(θ), c.p.y .+ c.r .* sin.(θ)
end
@recipe function f(c::Circle3D)
seriestype := :scatter
label --> :none
markersize --> 10c.r
[c.p.x], [c.p.y], [c.p.z]
end
const GeometyObj = Union{Point2D,Point3D,Circle2D,Circle3D}
@recipe function f(geomobjs::AbstractArray{<:GeometryObj})
for obj in geomobjs
@series begin
obj
end
end
end
@recipe f(geomobjs::T...) where T <: GeometyObj = collect(geomobjs)
end # module
@terasakisatoshi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment