|
1 | | -# aa-line-segment-js |
2 | | -Axis-Aligned Line Segment library for Node apps |
| 1 | +# Axis-Aligned Line Segment |
| 2 | + |
| 3 | +[](https://github.com/dimensionalpocket/aa-line-segment-js/actions/workflows/node.js.yml) [](https://lgtm.com/projects/g/dimensionalpocket/aa-line-segment-js/alerts/) [](https://lgtm.com/projects/g/dimensionalpocket/aa-line-segment-js/context:javascript) |
| 4 | + |
| 5 | +A Node library for line segment manipulations. Features include: |
| 6 | + |
| 7 | +* positioning, |
| 8 | +* nesting (add segments inside segments), |
| 9 | +* flipping around its position, |
| 10 | +* global positioning, and |
| 11 | +* collision detection. |
| 12 | + |
| 13 | +It is tailored for usage in games that require a box management system, for handling things such as scrolling stages, hitboxes/hurtboxes, etc. |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```js |
| 18 | +var segment = new AALineSegment(-1, 5) |
| 19 | + |
| 20 | +/* |
| 21 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 22 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 23 | + A──P──────────────B |
| 24 | + ↑ |
| 25 | + Position starts at 0 |
| 26 | +*/ |
| 27 | + |
| 28 | +segment.a // <= -1 |
| 29 | +segment.b // <= 5 |
| 30 | +``` |
| 31 | + |
| 32 | +### Positioning |
| 33 | + |
| 34 | +The segment can be moved along the axis: |
| 35 | + |
| 36 | +```js |
| 37 | +segment.position = 2 |
| 38 | + |
| 39 | +/* |
| 40 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 41 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 42 | + A──P──────────────B |
| 43 | + ↑ |
| 44 | + New position |
| 45 | +*/ |
| 46 | + |
| 47 | +segment.a // <= 1 |
| 48 | +segment.b // <= 7 |
| 49 | +``` |
| 50 | + |
| 51 | +### Flipping |
| 52 | + |
| 53 | +Segments can be flipped around their positions: |
| 54 | + |
| 55 | +```js |
| 56 | +segment.flip(true) |
| 57 | + |
| 58 | +/* |
| 59 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 60 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 61 | + A──────────────P──B |
| 62 | +*/ |
| 63 | + |
| 64 | +segment.a // <= -3 |
| 65 | +segment.b // <= 3 |
| 66 | + |
| 67 | +segment.flip(false) // unflip |
| 68 | +``` |
| 69 | + |
| 70 | +### Nested Segments |
| 71 | + |
| 72 | +Segments can be added to other segments, inheriting their positions and flip states. |
| 73 | + |
| 74 | +```js |
| 75 | +var child = new AALineSegment(1, 2) |
| 76 | + |
| 77 | +/* |
| 78 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 79 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 80 | + A──P──────────────B |
| 81 | +
|
| 82 | + P A──B |
| 83 | + ↑ |
| 84 | + Child without parent, position 0 |
| 85 | +
|
| 86 | +*/ |
| 87 | + |
| 88 | +child.a // <= 1 |
| 89 | +child.b // <= 2 |
| 90 | +``` |
| 91 | + |
| 92 | +```js |
| 93 | +segment.add(child) |
| 94 | + |
| 95 | +/* |
| 96 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 97 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 98 | + A──P──────────────B |
| 99 | + P A──B |
| 100 | + ↑ |
| 101 | + Child with parent, inherits position |
| 102 | +*/ |
| 103 | + |
| 104 | +child.a // <= 3 |
| 105 | +child.b // <= 4 |
| 106 | +``` |
| 107 | + |
| 108 | +```js |
| 109 | +child.flip(true) |
| 110 | + |
| 111 | +/* |
| 112 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 113 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 114 | + A──P──────────────B |
| 115 | + A──B P |
| 116 | + ↑ |
| 117 | + Flipped child |
| 118 | +*/ |
| 119 | + |
| 120 | +child.a // <= 0 |
| 121 | +child.b // <= 1 |
| 122 | + |
| 123 | +child.flip(false) // unflip |
| 124 | +``` |
| 125 | + |
| 126 | +Flipping the parent also flips its children: |
| 127 | + |
| 128 | +```js |
| 129 | +segment.flip(true) |
| 130 | + |
| 131 | +/* |
| 132 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 133 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 134 | + A──────────────P──B |
| 135 | + A──B P |
| 136 | +*/ |
| 137 | + |
| 138 | +child.a // <= 0 |
| 139 | +child.b // <= 1 |
| 140 | +``` |
| 141 | + |
| 142 | +If any child is already flipped, their global flip state is toggled: |
| 143 | + |
| 144 | +```js |
| 145 | +child.flip(true) |
| 146 | + |
| 147 | +/* |
| 148 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 149 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 150 | + A──────────────P──B |
| 151 | + P A──B |
| 152 | + ↑ |
| 153 | + Child is flipped, |
| 154 | + but appears unflipped |
| 155 | + because parent is flipped |
| 156 | +*/ |
| 157 | + |
| 158 | +child.a // <= 3 |
| 159 | +child.b // <= 4 |
| 160 | +``` |
| 161 | + |
| 162 | +### Collision Detection |
| 163 | + |
| 164 | +Two segments collide if they touch or intersect with each other. |
| 165 | + |
| 166 | +```js |
| 167 | +var s1 = new AALineSegment(-1, 0) |
| 168 | +var s2 = new AALineSegment(0, 1) |
| 169 | +/* |
| 170 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 171 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 172 | + A──B (s1) |
| 173 | + A──B (s2) |
| 174 | +*/ |
| 175 | + |
| 176 | +s1.collidesWith(s2) // <= true |
| 177 | + |
| 178 | +s2.position = 1 |
| 179 | +/* |
| 180 | +-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 |
| 181 | + ├──┬──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┬──┤ |
| 182 | + A──B (s1) |
| 183 | + A──B (s2, repositioned) |
| 184 | +*/ |
| 185 | + |
| 186 | +s1.collidesWith(s2) // <= false |
| 187 | +``` |
| 188 | + |
| 189 | +## License |
| 190 | + |
| 191 | +MIT |
0 commit comments