Skip to content

Commit

Permalink
Add Vector2.randomInCircle()
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Feb 5, 2024
1 parent 8cfb4c9 commit 004445d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/vector2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Circle } from './index.js'
import { jitter, random } from './random.js'

export class Vector2 {
Expand Down Expand Up @@ -107,7 +108,7 @@ export class Vector2 {
}

/**
*
* Returns a random point in the given bounds.
* @param {number} xMin
* @param {number} xMax
* @param {number} yMin
Expand All @@ -119,6 +120,18 @@ export class Vector2 {
return vec2(random(xMin, xMax, rng), random(yMin, yMax, rng));
}

/**
* Returns a random point within the given circle.
* @param {Circle} circle
* @param {import('./random.js').Rng} rng
* @returns {Vector2}
*/
static randomInCircle(circle, rng) {
const angle = random(0, Math.PI * 2, rng)
const radius = random(0, circle.radius, rng)
return circle.center.add(Vector2.fromAngle(angle).scale(radius))
}

/**
* Constructs a Vector2 instance from the given angle, in Radians.
* @param {Radians} angle
Expand Down

0 comments on commit 004445d

Please sign in to comment.