• egg

Note: this egg has not been put in the repo yet, I just wanted the docs done before I did that. If you come back and see this message gone, the egg is available to chicken-install!

## vectr

Andrew Mack

• coops

### Description

vectr provides types and operations relevant to vectors. These vectors are of the linear-algebraic variety, not the container types found in Chicken's base module. This is the reason for the lack of "o" in the name. Throughout the rest of the documentation, they will be referred to by the name "vectr".

The module provides two data types: a vectr (obviously), and a point. The types are defined using coops define-primitive-class procedure, and most functions are defined using coops define-method procedure. This way each structure can be used as a primitive type if so desired.

Currently only 2d vectrs are provided, but 3d vectrs will be provided in the near future.

### API

#### Types

[type] <vectr>
[procedure] (define-vectr tx <number> ty <number> hx <number> hy <number>) => <vectr>
[procedure] (define-vectr tail <point> head <point>) => <vectr>
[procedure] (vectr? obj) => <bool>

The main data type, vectr. Vectrs are records containing two slots, a head and a tail. The tail point is the vectr's origin and the head point is where the vectr stops. Vectrs can be created using the define-vectr procedure. This procedure can take arguments of either 4 numbers or 2 points. You may test whether or not an arbitrary piece of data is a vector by passing it to the vectr? procedure.

[type] <point>
[procedure] (point? obj)

The observant reader may notice there is no procedure for creating a point. This is because a point is just a pair whose car and cdr both evaluate to #t when passed to the number? predicate. All that is required to create a point is to cons two numbers together. Note: it is possible to use any type which returns #t for the number? predicate in a pair, however the author only plans to support integers, rationals, and real at this time. The option is left for the adventurous to use other numeric types at their own risk.

#### Procedures

[procedure] (get-head v <vectr>) => <point>
[procedure] (get-tail v <vectr>) => <point>
[procedure] (set-tail! v <vectr>)

Getters and setters for the head and tail of v.

[procedure] (vectr-add v1 <vectr> v2 <vectr>) => <vectr>

Returns the vector whose tail is the tail of v2 and whose head is the head of the vectr of the same length and angle as v1 and whose tail is the head of v2, as shown here.

[procedure] (vectr-magnitude v <vectr>) => <number>
[procedure] (vectr-length v <vectr>) => <number>

Returns the magnitude of v. For contexts where "length" has a clearer meaning than "magnitude", use vectr-length as it just calls vectr-magnitude.

[procedure] (vectr-scalar v <vectr> s <number>) => <vectr>

Returns a vectr equivalent to v scaled by s, as shown here.

[procedure] (vectr-subtract v1 <vectr> v2 <vectr>) => <vectr>

Returns a the result of (vectr-add (vectr-scalar v1 -1) v2).

[procedure] (pointing-up? v <vectr>) => <bool>
[procedure] (pointing-down? v <vectr>) => <bool>
[procedure] (pointing-left? v <vectr>) => <bool>
[procedure] (pointing-right? v <vectr>) => <bool>
[procedure] (horizontal? v <vectr>) => <bool>
[procedure] (vertical? v <vectr>) => <bool>

Procedures returning booleans based on the "direction" of v as if it were to be drawn moving from its head to its tail on a standard Cartesian coordinate graph where positive x grows to the left and positive y grows up.

### Repository

The source can be found here. Please feel free to share any issues or contribute, or even check my math :)