# maths

### Description

These functions are optimised for 3D graphics, and the collision of computer science and maths is apparent here, so scheme vectors representing maths vectors are in this context taken to be 3 elements long, quaternions are vectors of length 4, and matrices are vectors of 16 elements long.

## (vmulc vector number)

Returns result-vector

Multiplies a vector by a number. Deprecated C version, the Scheme version is faster and more flexible.

Example

``` (vmulc (vector 1 2 3) 2)
```

## (vaddc vector vector)

Returns result-vector

Adds two vectors together. Deprecated C version, the Scheme version is faster and more flexible.

Example

``` (vaddc (vector 1 2 3) (vector 1 2 3))
```

## (vsubc vector vector)

Returns result-vector

Subtracts a vector from another. Deprecated C version, the Scheme version is faster and more flexible.

Example

``` (vsubc (vector 1 2 3) (vector 1 2 3))
```

## (vdivc vector number)

Returns result-vector

Divides a vector by a number. Deprecated C version, the Scheme version is faster and more flexible.

Example

``` (vdivc (vector 1 2 3) 2)
```

## (vtransform vector matrix)

Returns result-vector

Multiplies (transforms) a vector by a matrix

Example

``` (vtransform (vector 0 1 0) (mrotate (vector 90 0 0)))
```

## (vtransform-rot vector matrix)

Returns result-vector

Multiplies (transforms) a vector by a matrix, but leaves out the translation part. For operations involving normals.

Example

``` (vtransform-rot (vector 0 1 0) (mrotate (vector 90 0 0)))
```

## (vnormalise vector)

Returns result-vector

Returns the normalised form of the vector (length=1)

Example

``` (vnormalise (vector 3 4 5))
```

## (vdot vector vector)

Returns result-number

Returns the dot product of two vectors

Example

``` (vdot (vector 0 1 0) (vector 1 0 0))
```

## (vmag vector)

Returns result-number

Returns the magnitude, or length of the vector

Example

``` (vmag (vector 0 1 1))
```

## (vreflect vector vector)

Returns result-vector

Returns the reflection of one vector against another.

Example

``` (vreflect (vector 0 1 1) (vector 1 0 1))
```

## (vdist vector vector)

Returns result-number

Treating the vectors as points, returns the distance between them.

Example

``` (vdist (vector 100 100 0) (vector 0 0 100))
```

## (vdist-sq vector vector)

Returns result-number

Treating the vectors as points, returns the squared distance between them. Faster than vdist.

Example

``` (vdist-sq (vector 100 100 0) (vector 0 0 100))
```

## (vcross vector vector)

Returns result-vector

Returns the cross product of two vectors, resulting in a vector that is perpendicular to the crossed ones.

Example

``` (vcross (vector 100 100 0) (vector 0 0 100))
```

## (mmul matrix-vector matrix-vector)

Returns matrix-vector

Multiplies two matrices together

Example

``` (mmul (mtranslate (vector 1 0 0)) (mrotate (vector 0 90 0)))
```

## (madd matrix-vector matrix-vector)

Returns matrix-vector

Adds two matrices together

Example

``` (madd (mtranslate (vector 1 0 0)) (mrotate (vector 0 90 0)))
```

## (msub matrix-vector matrix-vector)

Returns matrix-vector

Subtracts a matrix from another.

Example

``` (msub (mtranslate (vector 1 0 0)) (mrotate (vector 0 90 0)))
```

## (mdiv matrix-vector matrix-vector)

Returns matrix-vector

Divides a matrix by another

Example

``` (mdiv (mtranslate (vector 1 0 0)) (mrotate (vector 0 90 0)))
```

## (mident)

Returns matrix-vector

Returns the identity matrix

Example

``` (mident)
```

## (mtranslate vector)

Returns matrix-vector

Returns a matrix representing the specified transform

Example

``` (mtranslate (vector 100 0 0))
```

## (mrotate vector)

Returns matrix-vector

Returns a matrix representing the specified rotation. Accepts a vector of euler angles, or a quaternion.

Example

``` (mrotate (vector 0 45 0))
```

## (mscale vector)

Returns matrix-vector

Returns a matrix representing the specified scaling.

Example

``` (mscale (vector 0.5 2 0.5))
```

## (mtranspose matrix-vector)

Returns matrix-vector

Returns the transpose of the input vector

Example

``` (mtranspose (mident))
```

## (minverse matrix-vector)

Returns matrix-vector

Returns the inverse of the input vector.

Example

``` (minverse (mscale (vector 0.5 2 0.5)))
```

## (maim aim-vector up-vector)

Returns matrix-vector

Returns a matrix representing an aiming rotation so that the x axis points down the aim direction, and the y axis points up the up vector. Probably suffers from gimbal lock.

Example

``` (maim (vector 0 0 1) (vector 0 1 0))
```

## (matrix->euler vector-matriz)

Returns vector

Returns the euler angles extracted from the matrix.

Example

``` (matrix->euler (mrotate (vector 15 0 0)))
```

## (qaxisangle axis-vector angle-number)

Returns quaternion-vector

Returns the quaternion representing rotation of angle degrees about the specified axis.

Example

``` (qaxisangle (vector 0 1 0) 45)
```

## (qmul quaternion-vector quaternion-vector)

Returns quaternion-vector

Multiplies two quaternions together.

Example

``` (qmul (qaxisangle (vector 0 1 0) 45) (qaxisangle (vector 0 0 1) 180))
```

## (qnormalise quaternion-vector)

Returns quaternion-vector

Normalises a quaternion.

Example

``` (qnormalise (qaxisangle (vector 0 19 0) 45))
```

## (qtomatrix quaternion-vector)

Returns matrix-vector

Converts a quaternion into a rotation matrix.

Example

``` (qtomatrix (qaxisangle (vector 0 1 0) 45))
```

## (qconjugate quaternion-vector)

Returns quaternion-vector

Conjugatea a quaternion.

Example

``` (qconjugate (qaxisangle (vector 0 1 0) 45))
```

## (fmod numerator-number denominator-number)

Returns real-number

Returns the floating-point remainder of numerator/denominator.

Example

``` (fmod 14.4 10)
```

## (snoise real-number ...)

Returns real-number

Returns 1D/2D/3D/4D Simplex Noise in the range -1->1 depending on the number of parameters.

Example

``` (snoise 1.0 2.0) ; 2D noise
(snoise 6.1 2.4 .5 1.3) ; 4D noise

; example on a pixel prim
(clear)
(with-primitive (build-pixels 100 100)
(pdata-index-map!
(lambda (i c)
(snoise (* 0.1 (modulo i (pixels-width)))
(* 0.1 (quotient i (pixels-height)))))
"c")
```

## (noise real-number ...)

Returns real-number

Returns the Perlin Noise value at specified coordinates.

Example

``` (noise 1.0 2.0) ; 2D noise
(noise 6.1 2.4 .5) ; 3D noise

; example on a pixel prim
(clear)
(with-primitive (build-pixels 100 100)
(pdata-index-map!
(lambda (i c)
(noise (* 0.1 (modulo i (pixels-width)))
(* 0.1 (quotient i (pixels-height)))))
"c")
```

## (noise-seed unsigned-number)

Returns void

Sets the seed value for noise.

Example

``` (noise-seed 1)
```

## (noise-detail octaves-number falloff-number)

Returns void

Adjusts the character and level of detail produced by the Perlin noise function.

Example

``` (noise-detail 4) ; noise with 4 octaves
(noise-detail 4 .5) ; noise with 4 octaves and .5 falloff
```