Skip to content

Instantly share code, notes, and snippets.

@jasonbot
Created April 13, 2022 18:05
Show Gist options
  • Save jasonbot/1f8fa24aa569bd2f3cab991401fe1238 to your computer and use it in GitHub Desktop.
Save jasonbot/1f8fa24aa569bd2f3cab991401fe1238 to your computer and use it in GitHub Desktop.
func RotateAround(pt, origin image.Point, angle float64) (int, int) {
x1, y1 := float64(pt.X-origin.X), float64(pt.Y-origin.Y)
magnitude := math.Hypot(x1, y1)
rotationAngle := math.Atan2(y1, x1) + angle
return origin.X + (int(math.Cos(rotationAngle) * magnitude)), origin.Y + (int(math.Sin(rotationAngle) * magnitude))
}
func RotatedImage(i *ebiten.Image, rotation float64) *ebiten.Image {
if i == nil {
return i
}
w, h := i.Size()
cx, cy := w/2, h/2
maxDim := int(math.Hypot(float64(w), float64(h)))
center := maxDim / 2
newimage := ebiten.NewImage(maxDim, maxDim)
rx, ry := RotateAround(
image.Point{center - cx, center - cy},
image.Point{center, center},
rotation,
)
op := ebiten.DrawImageOptions{}
op.GeoM.Rotate(rotation)
op.GeoM.Translate(float64(rx), float64(ry))
newimage.DrawImage(i, &op)
return newimage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment