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

[core:math/linalg] Add linalg.clamp_length(vector, max_length) -> clamped_vector #4548

Merged
merged 1 commit into from
Dec 4, 2024

Conversation

Bazzagibbs
Copy link
Contributor

@Bazzagibbs Bazzagibbs commented Dec 2, 2024

Occasionally vectors need to be clamped to a maximum length while preserving smaller values, for example when processing joystick input from an unreliable source.

larger         := [2]f32 {1.0, 1.0}
larger_clamped := linalg.clamp_length(larger, 1.0)
// larger_clamped is approximately {0.707, 0.707}

smaller         := [2]f32 {0.2, 0}
smaller_clamped := linalg.clamp_length(smaller, 1.0)
// smaller_clamped == smaller

Divide-by-zero safety:

  • a is explicitly checked to be > 0
  • m2 is always 0+ because it's squared.
  • a*a is always 0+ because it's squared.
  • if m2 != 0, it's not divide-by-zero.
  • if m2 == 0, then m2 <= a*a which returns the original vector.

@gingerBill gingerBill merged commit c79466a into odin-lang:master Dec 4, 2024
7 checks passed
@Bazzagibbs Bazzagibbs deleted the linalg-clamp-length branch December 4, 2024 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants