@slugbugblue/trax 0.3.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/point.md ADDED
@@ -0,0 +1,134 @@
1
+ # point.js API documentation
2
+
3
+ This module provides a class structure to work with `x`, `y` locations as a
4
+ single point.
5
+
6
+ Since I don't need it to be super fancy, there isn't a lot of extra
7
+ functionality here.
8
+
9
+ ## Example usage
10
+
11
+ ```javascript
12
+ import { Point } from '@slugbugblue/trax/point.js'
13
+
14
+ const zero = new Point(0, 0)
15
+
16
+ const up = zero.up
17
+ console.log('up:', up.x, up.y) // up: 0 -1
18
+
19
+ const rt = zero.right
20
+ console.log('rt:', rt) // rt: Point(1,0)
21
+
22
+ const up2 = zero.dir('up')
23
+ console.log('up === up2 ?', up.eq(up2)) // up === up2 ? true
24
+
25
+ // distances
26
+ console.log(zero.distX(up), zero.distX(rt)) // 0 1
27
+ console.log(up.distX(rt), up.distY(rt)) // 0 0
28
+ console.log(zero.distance(up), zero.distance(rt)) // 1 1
29
+ console.log(up.distance(rt)) // 1.4142135623730951
30
+ ```
31
+
32
+ ## API
33
+
34
+ ### Point class
35
+
36
+ A Point instance is a representation of an immutable point in two dimensional
37
+ space, represented by an `x` and a `y` component. Since this implementation was
38
+ created for game boards, we assume that all points are integers, though that is
39
+ not a requirement, and the math will work equally well on non-integers.
40
+
41
+ #### constructor
42
+
43
+ ##### `new Point(fromPoint)`
44
+
45
+ ##### `new Point(x, y)`
46
+
47
+ You can create a point from either a point-like object (ie, any object with both
48
+ an `x` and a `y` key with numeric values), or from individual `x` and `y`
49
+ numbers. Once a point is created, it cannot be changed; however, you can use its
50
+ functions to return other points relative to it.
51
+
52
+ ##### `x` and `y` immutable properties
53
+
54
+ You can return the individual `x` or `y` property of a point by accessing it
55
+ directly. Attempts to change these properties will fail.
56
+
57
+ ##### `up`, `down`, `left`, `right` calculated properties
58
+
59
+ Since points are immutable, to move a point, you need to create a new point.
60
+ These calculated properties provide quick access to cardinal integer neighboring
61
+ points.
62
+
63
+ ##### `around` calculated property
64
+
65
+ Or, get an array of all four cardinal points around the existing point.
66
+
67
+ ##### `dir(direction)`
68
+
69
+ Pass in a string named direction (ie, one of `up`, `down`, `left`, or `right`)
70
+ to get a point one integer away from the existing point in the direction
71
+ indicated. This does the exact same thing as the calculated properties with
72
+ these names, but it can be done using a variable instead.
73
+
74
+ ##### `distX(from)`, `distY(from)`
75
+
76
+ Returns the difference between the `x` or `y` properties of two points. This
77
+ will always be represented by zero or a positive number.
78
+
79
+ ##### `distance(from)`
80
+
81
+ Returns the distance between two points. This will never be a negative number.
82
+
83
+ ##### `eq(to)`
84
+
85
+ Returns `true` if the given point or point-like object is at the same location
86
+ as the existing point.
87
+
88
+ #### Plays well with others
89
+
90
+ The Point class also includes functionality to provide useful default results
91
+ when used in other contexts.
92
+
93
+ ##### `toString()`
94
+
95
+ Returns a string representation of the point in the format `Point(x,y)`. This
96
+ allows simple logging calls with `console.log(point)`.
97
+
98
+ ##### `toJSON()`
99
+
100
+ JSON cannot encode special classes, but this class can be stored as a bare
101
+ object with `x` and `y` keys when converted to JSON. Note, though, that when
102
+ parsing the resultant JSON string, all Point functionality is not restored by
103
+ default. For example:
104
+
105
+ ```javascript
106
+ const zero = new Point(0, 0)
107
+ const json = JSON.stringify(zero)
108
+ const jsonzero = JSON.parse(json)
109
+
110
+ console.log(zero.eq(jsonzero)) // true
111
+ jsonzero.eq(zero) // TypeError: jsonzero.eq is not a function
112
+ const reconstituted = new Point(jsonzero)
113
+ console.log(reconstituted.eq(zero)) // true
114
+ ```
115
+
116
+ ##### `util.inspect.custom()`
117
+
118
+ When running in the node command line interpreter, a point will be
119
+ pretty-printed using the magic of `util.inspect`.
120
+
121
+ ## License
122
+
123
+ Copyright 2019-2022 Chad Transtrum
124
+
125
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
126
+ the files in this project except in compliance with the License. You may obtain
127
+ a copy of the License at
128
+
129
+ http://www.apache.org/licenses/LICENSE-2.0
130
+
131
+ Unless required by applicable law or agreed to in writing, software distributed
132
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
133
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
134
+ specific language governing permissions and limitations under the License.