Skip to content

Instantly share code, notes, and snippets.

@jkrumbiegel
Created January 20, 2025 19:27
Show Gist options
  • Save jkrumbiegel/c34e7400545b1919048a1c57e44c0f54 to your computer and use it in GitHub Desktop.
Save jkrumbiegel/c34e7400545b1919048a1c57e44c0f54 to your computer and use it in GitHub Desktop.
glmakie shift polys in discrete steps
using GLMakie
using Makie.GeometryBasics: Polygon
polys = [
Point2f[(0, 0), (3, 0), (3, 1), (2, 1), (2, 2), (1, 2), (1, 1), (0, 1)],
Point2f[(0, 0), (2, 0), (2, 2), (0, 2)] .+ Point2f(4, 4),
]
shifts = Observable([Point2f(0, 0) for _ in polys])
shifted_polys = lift(shifts) do shifts
map(polys, shifts) do poly, shift
poly .+ shift
end
end
f = Figure()
ax = Axis(f[1, 1], limits = (0, 10, 0, 10), aspect = 1)
deactivate_interaction!(ax, :rectanglezoom)
p = poly!(ax, shifted_polys, color = rand(RGBf, length(polys)))
ev = addmouseevents!(ax.scene, p)
i_drag = Ref{Union{Int,Nothing}}(nothing)
startpos = Ref{Point2f}()
startshift = Ref{Point2f}()
onmouseleftdragstart(ev) do ev
_, i = pick(ax.scene)
i_drag[] = Makie.vertexindex2poly(p[1][], i)
startpos[] = ev.data
startshift[] = shifts[][i_drag[]]
end
onmouseleftdrag(ev) do ev
delta = ev.data - startpos[]
shift = startshift[] + Point2f(round.(Int, delta))
if shifts[][i_drag[]] != shift
shifts[][i_drag[]] = shift
notify(shifts)
end
end
onmouseleftdragstop(ev) do ev
i_drag[] = nothing
end
f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment