Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2D Vector rotation by radians operation #49

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions rotateRad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package govec

import (
"math"
)

// RotateRad returns the vector obtained by rotating
// the specified 2D vector counterclockwise by the angle
// specified in radians.
func (v V2F[T]) RotateRad(angleRad float64) V2F[T] {
return V2F[T]{
X: T(float64(v.X)*math.Cos(angleRad) - float64(v.Y)*math.Sin(angleRad)),
Y: T(float64(v.X)*math.Sin(angleRad) + float64(v.Y)*math.Cos(angleRad)),
}
}

// RotateRad returns the vector obtained by rotating
// the specified 2D vector counterclockwise by the angle
// specified in radians.
func (v V2I[T]) RotateRad(angleRad float64) V2F[float64] {
return V2F[float64]{
X: float64(v.X)*math.Cos(angleRad) - float64(v.Y)*math.Sin(angleRad),
Y: float64(v.X)*math.Sin(angleRad) + float64(v.Y)*math.Cos(angleRad),
}
}