Skip to content

Commit

Permalink
randomXY
Browse files Browse the repository at this point in the history
  • Loading branch information
rhazarian committed May 1, 2024
1 parent cc63edd commit c59dd14
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/engine/random.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { MAXIMUM_INTEGER } from "../math"
import { MAXIMUM_INTEGER, PI } from "../math"

const getRandomInt = GetRandomInt
const getRandomReal = GetRandomReal

const cos = math.cos
const sin = math.sin
const sqrt = math.sqrt

export const randomAngle = (): number => getRandomReal(0, 360)

export const randomInteger: {
Expand All @@ -16,3 +20,13 @@ export const randomFloat: {
(lowerBound: number, upperBound: number): number
} = (m?: number, n?: number): number =>
n != undefined ? getRandomReal(m!, n) : getRandomReal(0, m ?? MAXIMUM_INTEGER)

export const randomXY = (
centerX: number,
centerY: number,
range: number
): LuaMultiReturn<[x: number, y: number]> => {
const r = range * sqrt(getRandomReal(0, 1))
const t = getRandomReal(0, 1) * 2 * PI
return $multi(centerX + r * cos(t), centerY + r * sin(t))
}

0 comments on commit c59dd14

Please sign in to comment.