Created
October 14, 2021 01:36
-
-
Save terasakisatoshi/31faf2c25749ae9330e75d34319dbb45 to your computer and use it in GitHub Desktop.
GeometryRecipes.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://www.youtube.com/watch?v=rWspPMA_4Tk&t=4265s