Skip to content

Commit

Permalink
Fix vec2.angle to return degrees
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Feb 16, 2024
1 parent 4f5c283 commit 9d48e1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/vec2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ test('rotate', () => {
})

describe('angle', () => {
it('shoud work in the case of 0°', () => {
expect(vec2.angle([1, 0])).toEqual(0)
})

it('shoud work in the case of 90°', () => {
expect(vec2.angle([0, 1])).toEqual(90)
})

it('should work in the case of [-120°, 120]', () => {
const x = 1 / 2
const y = Math.sqrt(3) / 2
Expand Down
2 changes: 1 addition & 1 deletion src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export namespace vec2 {
* Get the angle between two 2D vectors. If the second argument is omitted, it returns a signed angle relative to x axis.
*/
export function angle(a: vec2, b?: vec2) {
if (!b) return Math.atan2(a[1], a[0])
if (!b) return Math.atan2(a[1], a[0]) * Common.RAD2DEG

const [x1, y1] = a
const [x2, y2] = b
Expand Down

0 comments on commit 9d48e1e

Please sign in to comment.