problem07
7. Simple animation of a shape. Draw a picture of some object (a face, a house, whatever), and make it move around on the screen in a smooth and interesting way. No distortions. Just motions and rotations
In file ./simple_animation.jl:
using GLMakie
# Create a figure
= Figure(resolution = (800, 600))
fig = GLMakie.Axis(fig[1, 1], aspect = DataAspect())
ax limits!(ax, -5, 5, -5, 5)
# Define the house vertices (centered at origin)
= [
house_vertices # Main square
Point2f(-1, -1), Point2f(1, -1), Point2f(1, 1), Point2f(-1, 1), Point2f(-1, -1),
# Roof
Point2f(-1, 1), Point2f(0, 2), Point2f(1, 1)
]
# Create initial lines object
= lines!(ax, house_vertices, color = :blue, linewidth = 2)
house_lines
# Animation parameters
= 300
frames = 2π/frames # Complete one rotation
angular_speed = 4/frames # Total drift distance
drift_speed
# Create the animation
record(fig, "rotating_house.gif", 1:frames; framerate = 30) do frame
# Calculate rotation and drift
= angular_speed * frame
angle = drift_speed * frame - 2 # Start from left side
drift_x = sin(2π * frame/frames) # Add some vertical oscillation
drift_y
# Apply transformation to each point
= [
transformed_points Point2f(
* cos(angle) - y * sin(angle) + drift_x, # Rotation + horizontal drift
x * sin(angle) + y * cos(angle) + drift_y # Rotation + vertical oscillation
x
)in [(p[1], p[2]) for p in house_vertices]
for (x, y)
]
# Update the lines object
1] = transformed_points
house_lines[end
Produces this animation: