Skip to content

Instantly share code, notes, and snippets.

@jkrumbiegel
Last active November 22, 2024 13:49
Show Gist options
  • Save jkrumbiegel/55317b996506eba3fed1b0700920899d to your computer and use it in GitHub Desktop.
Save jkrumbiegel/55317b996506eba3fed1b0700920899d to your computer and use it in GitHub Desktop.
Striped line Makie
using LinearAlgebra
@recipe(Stripy) do scene
Attributes(;
Makie.default_theme(scene, Lines)...,
n = 3,
offset = 10,
)
end
Makie.conversion_trait(::Type{<:Stripy}) = PointBased()
function Makie.plot!(p::Stripy)
scene = Makie.parent_scene(p)
function _normal_and_scalar(p1, p2)
vec = LinearAlgebra.normalize(p2 - p1)
norm = Point2(-vec[2], vec[1])
return norm, 1.0
end
function _normal_and_scalar(p1, p2, p3)
d1 = LinearAlgebra.normalize(p2 - p1)
vec = LinearAlgebra.normalize(p3 - p2) + d1
norm = LinearAlgebra.normalize(Point2(-vec[2], vec[1]))
return norm, dot(norm, Point2(-d1[2], d1[1]))
end
screenpoints = lift(p, Makie.f32_conversion_obs(scene), scene.viewport, scene.camera.projectionview, p[1], p.n, p.offset, ignore_equal_values = true
) do f32c, vp, pv, points, n, offset
newpoints = [Point2(NaN) for _ in 1:(length(points) * n + (n - 1))]
projected = [Point2f(Makie.project(scene, :data, :pixel, point)) for point in points]
for i in eachindex(projected)
if i == 1
normal, scalar = _normal_and_scalar(projected[1], projected[2])
elseif i == length(projected)
normal, scalar = _normal_and_scalar(projected[end-1], projected[end])
else
normal, scalar = _normal_and_scalar(projected[i-1], projected[i], projected[i+1])
end
for j in 1:n
idx = i + (j - 1) * (length(points) + 1)
newpoints[idx] = projected[i] + (1 / scalar) * normal * ((j - 1) / (n - 1) * n * offset)
end
end
newpoints
end
attr = copy(p.attributes)
delete!(attr, :n)
delete!(attr, :offset)
lines!(p, screenpoints; attr..., space = :pixel)
end
Makie.data_limits(p::Stripy) = Rect3f(p[1][])
Makie.boundingbox(p::Stripy, space::Symbol = :data) = Makie.apply_transform_and_model(p, Makie.data_limits(p))
stripy(cumsum(randn(20)); n = 5, color = :red, offset = 3)
@jkrumbiegel
Copy link
Author

grafik

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