Skip to content

Instantly share code, notes, and snippets.

@jkrumbiegel
Created April 2, 2025 14:15
Show Gist options
  • Save jkrumbiegel/fe03529ba72a2c3ea309b592ec3f2df6 to your computer and use it in GitHub Desktop.
Save jkrumbiegel/fe03529ba72a2c3ea309b592ec3f2df6 to your computer and use it in GitHub Desktop.
Makie slopeline length in screen space
using LinearAlgebra: normalize
f = Figure()
ax1 = Axis(f[1, 1])
slope1 = 2
data1 = [1 0; slope1 1] * randn(2, 500)
scatter!(ax1, data1, alpha = 0.1)
ax2 = Axis(f[1, 2])
slope2 = -0.5
data2 = [1 0; slope2 1] * randn(2, 500)
scatter!(ax2, data2, alpha = 0.1)
linkyaxes!(ax1, ax2)
function slopeline!(ax, pos, slope; length = 50, kwargs...)
points = lift(ax.scene.viewport, ax.scene.camera.projectionview) do _, _
pos_screen = Makie.project(ax.scene, pos)
pos_delta = Makie.project(ax.scene, pos + Point(1, slope))
d = normalize(pos_delta - pos_screen)
offset = d * length / 2
[pos_screen - offset, pos_screen + offset]
end
lines!(ax, points; space = :pixel, kwargs...)
end
slopeline!(ax1, Point2(0, 0), slope1, linewidth = 2)
slopeline!(ax2, Point2(0, 0), slope2, linewidth = 2)
f
@jkrumbiegel
Copy link
Author

image

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