math
math
¶
Random numbers, noise, curves, triangulation and small geometry helpers.
This is pure Nim with no SDL or renderer dependency. It carries a seeded generator you can keep alongside the global one, normally distributed random, value/Perlin/simplex noise mapped to the 0-to-1 range, Bezier curves, ear-clipping polygon triangulation, and helpers like distance, angle, lerp and gamma to linear color conversion. The triangulation is what lets the shape drawing fill concave polygons.
The standalone transform here is the same Transform the renderer uses for
the transform stack, so newTransform and transformPoint build on it
rather than introducing a second matrix type.
vec2 ¶
func vec2(x, y: float): Vec2
+ ¶
func +(a, b: Vec2): Vec2
- ¶
func -(a, b: Vec2): Vec2
- ¶
func -(a: Vec2): Vec2
The vector pointing the opposite way.
Parameters
-
a(Vec2)
Returns
Vec2
* ¶
func *(a: Vec2; s: float): Vec2
A vector scaled by a number.
Parameters
-
a(Vec2) -
s(float)
Returns
Vec2
* ¶
func *(s: float; a: Vec2): Vec2
A vector scaled by a number.
Parameters
-
s(float) -
a(Vec2)
Returns
Vec2
/ ¶
func /(a: Vec2; s: float): Vec2
A vector divided by a number.
Parameters
-
a(Vec2) -
s(float)
Returns
Vec2
+= ¶
proc +=(a: var Vec2; b: Vec2)
-= ¶
proc -=(a: var Vec2; b: Vec2)
*= ¶
proc *=(a: var Vec2; s: float)
dot ¶
func dot(a, b: Vec2): float
lengthSq ¶
func lengthSq(a: Vec2): float
The squared length, cheaper than length for comparisons.
Parameters
-
a(Vec2)
Returns
float
length ¶
func length(a: Vec2): float
normalized ¶
func normalized(a: Vec2): Vec2
The unit vector pointing the same way as a, or (0, 0) if a has no length.
Parameters
-
a(Vec2)
Returns
Vec2
lerp ¶
func lerp(a, b: Vec2; t: float): Vec2
Linear interpolation from a to b by t.
Parameters
-
a(Vec2) -
b(Vec2) -
t(float)
Returns
Vec2
rotated ¶
func rotated(a: Vec2; angle: float): Vec2
a turned by angle radians, which appears clockwise on screen since y
points down.
Parameters
-
a(Vec2) -
angle(float)
Returns
Vec2
fromAngle ¶
func fromAngle(angle: float; magnitude = 1.0): Vec2
A vector of the given length pointing at angle radians.
Parameters
-
angle(float) -
magnitude(auto)
Returns
Vec2
Rng
¶
type Rng = object
A seeded pseudo-random generator. It is a PCG32: a 64-bit linear
congruential state with a permuted 32-bit output, small and fast, and it gives the same sequence from the same seed on every platform.
nextUint ¶
proc nextUint(rng: var Rng): uint32
The next raw 32-bit value from the generator.
Parameters
-
rng(var Rng)
Returns
uint32
setSeed ¶
proc setSeed(rng: var Rng; seed: uint64; seq: uint64 = defaultSeq)
Reseed an existing generator in place.
Parameters
-
rng(var Rng) -
seed(uint64) -
seq(uint64)
newRng ¶
proc newRng(seed: uint64 = defaultSeed; seq: uint64 = defaultSeq): Rng
Make a seeded generator. The same seed and stream give the same sequence.
Parameters
-
seed(uint64) -
seq(uint64)
Returns
Rng
random ¶
proc random(rng: var Rng): float
A uniform float from 0 up to 1.
Parameters
-
rng(var Rng)
Returns
float
random ¶
proc random(rng: var Rng; max: float): float
A uniform float from 0 up to max.
Parameters
-
rng(var Rng) -
max(float)
Returns
float
random ¶
proc random(rng: var Rng; min, max: float): float
A uniform float from min up to max.
Parameters
-
rng(var Rng) -
min(float) -
max(float)
Returns
float
randomInt ¶
proc randomInt(rng: var Rng; min, max: int): int
A uniform integer from min to max, both ends included. The span from min to
max has to fit in 32 bits.
Parameters
-
rng(var Rng) -
min(int) -
max(int)
Returns
int
randomNormal ¶
proc randomNormal(rng: var Rng; mean = 0.0; stddev = 1.0): float
A normally distributed (gaussian) sample with the given mean and standard
deviation, by the polar Box-Muller method.
Parameters
-
rng(var Rng) -
mean(auto) -
stddev(auto)
Returns
float
setRandomSeed ¶
proc setRandomSeed(seed: uint64; seq: uint64 = defaultSeq)
Reseed the shared global generator.
Parameters
-
seed(uint64) -
seq(uint64)
random ¶
proc random(): float
A uniform float from 0 up to 1 from the shared global generator.
Returns
float
random ¶
proc random(max: float): float
A uniform float from 0 up to max from the global generator.
Parameters
-
max(float)
Returns
float
random ¶
proc random(min, max: float): float
A uniform float from min up to max from the global generator.
Parameters
-
min(float) -
max(float)
Returns
float
randomInt ¶
proc randomInt(min, max: int): int
A uniform integer from min to max inclusive, from the global generator. The
span from min to max has to fit in 32 bits.
Parameters
-
min(int) -
max(int)
Returns
int
randomNormal ¶
proc randomNormal(mean = 0.0; stddev = 1.0): float
A normally distributed sample from the global generator.
Parameters
-
mean(auto) -
stddev(auto)
Returns
float
noise ¶
proc noise(x: float): float
1D value noise from 0 up to 1, smooth within each integer cell.
Parameters
-
x(float)
Returns
float
noise ¶
proc noise(x, y, z: float): float
3D Perlin noise mapped to the 0-to-1 range.
Parameters
-
x(float) -
y(float) -
z(float)
Returns
float
noise ¶
proc noise(x, y: float): float
2D Perlin noise mapped to the 0-to-1 range.
Parameters
-
x(float) -
y(float)
Returns
float
simplexNoise ¶
proc simplexNoise(x, y: float): float
2D simplex noise mapped to the 0-to-1 range. Fewer directional artifacts than Perlin.
Parameters
-
x(float) -
y(float)
Returns
float
simplexNoise ¶
proc simplexNoise(x, y, z: float): float
3D simplex noise mapped to the 0-to-1 range.
Parameters
-
x(float) -
y(float) -
z(float)
Returns
float
newBezierCurve ¶
proc newBezierCurve(points: openArray[Vec2]): BezierCurve
A Bezier curve through the given control points (degree = points.len - 1).
Parameters
-
points(openArray[Vec2])
Returns
BezierCurve
evaluate ¶
proc evaluate(curve: BezierCurve; t: float): Vec2
The point on the curve at parameter t from 0 to 1, by de Casteljau.
Parameters
-
curve(BezierCurve) -
t(float)
Returns
Vec2
derivative ¶
proc derivative(curve: BezierCurve): BezierCurve
The curve whose evaluation gives this curve's tangent vector.
Parameters
-
curve(BezierCurve)
Returns
BezierCurve
render ¶
proc render(curve: BezierCurve; segments = 30): seq[Vec2]
Sample the curve into a polyline of segments+1 points, ready for line.
Parameters
-
curve(BezierCurve) -
segments(auto)
Returns
seq[Vec2]
isConvex ¶
proc isConvex(points: openArray[Vec2]): bool
True when the polygon outline is convex (a triangle or fewer points counts
as convex).
Parameters
-
points(openArray[Vec2])
Returns
bool
triangulate ¶
proc triangulate(points: openArray[Vec2]): seq[uint32]
Ear-clip a simple polygon into triangles, returning indices into points,
three per triangle. Works for convex and concave outlines without holes; raises ValueError on fewer than 3 points or input it cannot triangulate.
Parameters
-
points(openArray[Vec2])
Returns
seq[uint32]
newTransform ¶
proc newTransform(): Transform
An identity standalone transform you can translate, rotate and scale.
Returns
Transform
transformPoint ¶
proc transformPoint(t: Transform; x, y: float): Vec2
Map a point through a standalone transform.
Parameters
-
t(Transform) -
x(float) -
y(float)
Returns
Vec2
distance ¶
proc distance(x1, y1, x2, y2: float): float
Euclidean distance between two points.
Parameters
-
x1(float) -
y1(float) -
x2(float) -
y2(float)
Returns
float
distance ¶
proc distance(a, b: Vec2): float
Euclidean distance between two points.
Parameters
-
a(Vec2) -
b(Vec2)
Returns
float
angle ¶
proc angle(x1, y1, x2, y2: float): float
Angle in radians from the first point to the second.
Parameters
-
x1(float) -
y1(float) -
x2(float) -
y2(float)
Returns
float
angle ¶
proc angle(a, b: Vec2): float
Angle in radians from a to b.
Parameters
-
a(Vec2) -
b(Vec2)
Returns
float
lerp ¶
proc lerp(a, b, t: float): float
Linear interpolation from a to b by t.
Parameters
-
a(float) -
b(float) -
t(float)
Returns
float
gammaToLinear ¶
proc gammaToLinear(c: float): float
Convert one sRGB gamma-space channel, 0 to 1, into linear space.
Parameters
-
c(float)
Returns
float
linearToGamma ¶
proc linearToGamma(c: float): float
Convert one linear channel, 0 to 1, into sRGB gamma space.
Parameters
-
c(float)
Returns
float
gammaToLinear ¶
proc gammaToLinear(c: Color): Color
Convert an sRGB color to linear space, leaving alpha unchanged.
Parameters
-
c(Color)
Returns
Color
linearToGamma ¶
proc linearToGamma(c: Color): Color
Convert a linear color to sRGB gamma space, leaving alpha unchanged.
Parameters
-
c(Color)
Returns
Color