@amritk/nish-x86_64-linux 0.46.0 → 0.47.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/std/raster.ts CHANGED
@@ -90,7 +90,15 @@ export const rasterPixel = (v: f64): i32 => {
90
90
  }
91
91
 
92
92
  /** `v` clamped to a channel's 0..255. */
93
- const rasterByte = (v: i32): i32 => Math.min(Math.max(v, 0), 255)
93
+ const rasterByte = (v: i32): integer<0, 255> => {
94
+ if (v < 0) {
95
+ return 0
96
+ }
97
+ if (v > 255) {
98
+ return 255
99
+ }
100
+ return v
101
+ }
94
102
 
95
103
  /** `x` held to 0..1; NaN becomes 0. */
96
104
  const rasterUnit = (x: f64): f64 => {
@@ -117,18 +125,56 @@ export const rasterRgb = (r: i32, g: i32, b: i32): i32 =>
117
125
  * @param t - the fraction, clamped to 0..1; NaN is 0
118
126
  * @returns the weight, 0 to `RASTER_SOLID`
119
127
  */
120
- export const rasterWeight = (t: f64): i32 => {
128
+ export const rasterWeight = (t: f64): i32 => rasterWeightOf(t)
129
+
130
+ /** `rasterWeight`, typed as the 0..256 it returns, which is what lets a mix use it unchecked. */
131
+ const rasterWeightOf = (t: f64): integer<0, 256> => {
121
132
  if (!(t > 0.0)) {
122
133
  return 0
123
134
  }
124
135
  if (t >= 1.0) {
125
- return RASTER_SOLID
136
+ return 256
137
+ }
138
+ const w: i32 = toI32(t * 256.0)
139
+ // Never true: `t` is inside (0, 1), so `w` is 0..255. The test is what
140
+ // proves the result in range, which the float product cannot.
141
+ if (w < 0 || w > 256) {
142
+ return 0
126
143
  }
127
- return toI32(t * 256.0)
144
+ return w
145
+ }
146
+
147
+ /** A weight clamped to 0..256: at or below 0 is none of the new colour, at or above 256 all of it. */
148
+ const rasterClampWeight = (w: i32): integer<0, 256> => {
149
+ if (w <= 0) {
150
+ return 0
151
+ }
152
+ if (w >= 256) {
153
+ return 256
154
+ }
155
+ return w
128
156
  }
129
157
 
130
158
  /** Channel `a` moved toward `b` by `w` 256ths. */
131
- const rasterMixByte = (a: i32, b: i32, w: i32): i32 => a + (((b - a) * w) >> 8)
159
+ const rasterMixByte = (a: integer<0, 255>, b: integer<0, 255>, w: integer<0, 256>): i32 =>
160
+ a + (((b - a) * w) >> 8)
161
+
162
+ /** The channel of colour `c` that sits `shift` bits up: 16 red, 8 green, 0 blue. */
163
+ const rasterChannel = (c: i32, shift: i32): integer<0, 255> => {
164
+ const v: i32 = (c >> shift) & 255
165
+ // Never true: the mask keeps eight bits. The test is what types the result
166
+ // as a channel, which the mask alone does not.
167
+ if (v < 0 || v > 255) {
168
+ return 0
169
+ }
170
+ return v
171
+ }
172
+
173
+ /** `rasterMixWeight` for a weight already in 0..256, which a per-pixel loop holds once outside it. */
174
+ const rasterMixIn = (a: i32, b: i32, w: integer<0, 256>): i32 =>
175
+ (rasterMixByte(rasterChannel(a, 16), rasterChannel(b, 16), w) << 16) |
176
+ (rasterMixByte(rasterChannel(a, 8), rasterChannel(b, 8), w) << 8) |
177
+ rasterMixByte(rasterChannel(a, 0), rasterChannel(b, 0), w)
132
178
 
133
179
  /**
134
180
  * `a` moved toward `b` by a 0..256 weight, which a per-pixel loop computes
@@ -136,13 +182,10 @@ const rasterMixByte = (a: i32, b: i32, w: i32): i32 => a + (((b - a) * w) >> 8)
136
182
  *
137
183
  * @param a - the colour at weight 0
138
184
  * @param b - the colour at weight 256
139
- * @param w - the weight, 0 to `RASTER_SOLID`
185
+ * @param w - the weight, clamped to 0..`RASTER_SOLID`
140
186
  * @returns the mixed colour
141
187
  */
142
- export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 =>
143
- (rasterMixByte((a >> 16) & 255, (b >> 16) & 255, w) << 16) |
144
- (rasterMixByte((a >> 8) & 255, (b >> 8) & 255, w) << 8) |
145
- rasterMixByte(a & 255, b & 255, w)
188
+ export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 => rasterMixIn(a, b, rasterClampWeight(w))
146
189
 
147
190
  /**
148
191
  * `a` moved toward `b` by a fraction.
@@ -152,7 +195,7 @@ export const rasterMixWeight = (a: i32, b: i32, w: i32): i32 =>
152
195
  * @param t - how far, clamped to 0..1
153
196
  * @returns the mixed colour
154
197
  */
155
- export const rasterMix = (a: i32, b: i32, t: f64): i32 => rasterMixWeight(a, b, rasterWeight(t))
198
+ export const rasterMix = (a: i32, b: i32, t: f64): i32 => rasterMixIn(a, b, rasterWeightOf(t))
156
199
 
157
200
  /**
158
201
  * Two colours added channel by channel, each saturating at 255: how light adds.
@@ -173,7 +216,7 @@ export const rasterAdd = (a: i32, b: i32): i32 =>
173
216
  * @param k - the factor every channel is multiplied by, clamped to 0..1
174
217
  * @returns the dimmed colour
175
218
  */
176
- export const rasterDim = (c: i32, k: f64): i32 => rasterMixWeight(0, c, rasterWeight(k))
219
+ export const rasterDim = (c: i32, k: f64): i32 => rasterMixIn(0, c, rasterWeightOf(k))
177
220
 
178
221
  /**
179
222
  * The fully saturated colour at a hue, so a rainbow is
@@ -189,7 +232,7 @@ export const rasterHue = (h: f64): i32 => {
189
232
  }
190
233
  const h6: f64 = turn * 6.0
191
234
  const sector: i32 = toI32(Math.floor(h6))
192
- const rise: i32 = rasterByte(toI32((h6 - Math.floor(h6)) * 255.0))
235
+ const rise: integer<0, 255> = rasterByte(toI32((h6 - Math.floor(h6)) * 255.0))
193
236
  const fall: i32 = 255 - rise
194
237
  if (sector === 0) {
195
238
  return rasterRgb(255, rise, 0)
@@ -219,6 +262,19 @@ const rasterRows = (pixels: Int32Array, width: i32, height: i32): i32 => {
219
262
  return Math.min(height, toI32(pixels.length) / width)
220
263
  }
221
264
 
265
+ /**
266
+ * The index of pixel `(x, y)` in a `width`-wide frame of `size` pixels, or -1
267
+ * when it is before the start or past the end. One past the last pixel is an
268
+ * index, so a span that ends at the frame's end starts somewhere.
269
+ */
270
+ const rasterAt = (size: i32, width: i32, x: i32, y: i32): i32 => {
271
+ const at: i64 = toI64(y) * toI64(width) + toI64(x)
272
+ if (at < 0 || at > toI64(size)) {
273
+ return -1
274
+ }
275
+ return toI32(at)
276
+ }
277
+
222
278
  /** Pixels `x0..x1` (exclusive) of row `y` set to `colour`, or blended by `w` 256ths below 256, clipped. */
223
279
  const rasterSpan = (
224
280
  pixels: Int32Array,
@@ -228,25 +284,55 @@ const rasterSpan = (
228
284
  x1: i32,
229
285
  y: i32,
230
286
  colour: i32,
231
- w: i32
287
+ w: integer<0, 256>
232
288
  ): void => {
233
289
  if (y < 0 || y >= rows) {
234
290
  return
235
291
  }
236
292
  const from: i32 = Math.max(x0, 0)
237
293
  const to: i32 = Math.min(x1, width)
238
- const row: i32 = y * width
294
+ const size: i32 = toI32(pixels.length)
295
+ // `at` walks the row from pixel `from`: the loops end on `x`, and the row
296
+ // is inside the frame because `y < rows`, so `at < size` never ends one.
297
+ // It is what proves the index.
298
+ let at: i32 = rasterAt(size, width, from, y)
299
+ if (at < 0) {
300
+ return
301
+ }
239
302
  if (w >= RASTER_SOLID) {
240
- for (let x: i32 = from; x < to; x++) {
241
- pixels[row + x] = colour
303
+ for (let x: i32 = from; x < to && at < size; x++) {
304
+ pixels[at] = colour
305
+ at = at + 1
242
306
  }
243
307
  return
244
308
  }
245
- for (let x: i32 = from; x < to; x++) {
246
- pixels[row + x] = rasterMixWeight(pixels[row + x], colour, w)
309
+ for (let x: i32 = from; x < to && at < size; x++) {
310
+ pixels[at] = rasterMixIn(pixels[at], colour, w)
311
+ at = at + 1
247
312
  }
248
313
  }
249
314
 
315
+ /** One past pixel `last`, held to `limit`: the exclusive end of a run that ends on `last`. */
316
+ const rasterEnd = (last: i32, limit: i32): i32 => (last >= limit ? limit : last + 1)
317
+
318
+ /** The weight of row `y` of a `height`-row gradient: 0 at the top, 256 at the bottom. */
319
+ const rasterGradientWeight = (y: i32, height: i32): integer<0, 256> => {
320
+ if (height <= 1 || y <= 0) {
321
+ return 0
322
+ }
323
+ if (y >= height - 1) {
324
+ return 256
325
+ }
326
+ // In f64, where `y * 256` cannot overflow; the quotient of two exact
327
+ // integers rounds to nearest, so truncating it is the integer quotient.
328
+ const w: i32 = toI32((toF64(y) * 256.0) / toF64(height - 1))
329
+ // Never true: `y` is inside `1..height - 2`, so `w` is under 256.
330
+ if (w < 0 || w > 256) {
331
+ return 0
332
+ }
333
+ return w
334
+ }
335
+
250
336
  /**
251
337
  * Every pixel of the frame, as a vertical gradient: a background.
252
338
  *
@@ -259,8 +345,8 @@ const rasterSpan = (
259
345
  export const rasterGradient = (pixels: Int32Array, width: i32, height: i32, top: i32, bottom: i32): void => {
260
346
  const rows: i32 = rasterRows(pixels, width, height)
261
347
  for (let y: i32 = 0; y < rows; y++) {
262
- const colour: i32 = rasterMixWeight(top, bottom, height > 1 ? (y * 256) / (height - 1) : 0)
263
- rasterSpan(pixels, width, rows, 0, width, y, colour, RASTER_SOLID)
348
+ const colour: i32 = rasterMixIn(top, bottom, rasterGradientWeight(y, height))
349
+ rasterSpan(pixels, width, rows, 0, width, y, colour, 256)
264
350
  }
265
351
  }
266
352
 
@@ -292,11 +378,12 @@ export const rasterRect = (
292
378
  if (weight <= 0) {
293
379
  return
294
380
  }
381
+ const w: integer<0, 256> = rasterClampWeight(weight)
295
382
  const rows: i32 = rasterRows(pixels, width, height)
296
383
  const from: i32 = Math.max(y0, 0)
297
384
  const to: i32 = Math.min(y1, rows)
298
385
  for (let y: i32 = from; y < to; y++) {
299
- rasterSpan(pixels, width, rows, x0, x1, y, colour, weight)
386
+ rasterSpan(pixels, width, rows, x0, x1, y, colour, w)
300
387
  }
301
388
  }
302
389
 
@@ -332,22 +419,27 @@ export const rasterDisc = (
332
419
  const rows: i32 = rasterRows(pixels, width, height)
333
420
  const inner: f64 = r > 0.5 ? (r - 0.5) * (r - 0.5) : 0.0
334
421
  const outer: f64 = (r + 0.5) * (r + 0.5)
335
- const fullW: i32 = rasterWeight(a)
422
+ const fullW: integer<0, 256> = rasterWeightOf(a)
423
+ const size: i32 = toI32(pixels.length)
336
424
  const x0: i32 = Math.max(rasterPixel(cx - r - 1.0), 0)
337
- const x1: i32 = Math.min(rasterPixel(cx + r + 1.0) + 1, width)
425
+ const x1: i32 = rasterEnd(rasterPixel(cx + r + 1.0), width)
338
426
  const y0: i32 = Math.max(rasterPixel(cy - r - 1.0), 0)
339
- const y1: i32 = Math.min(rasterPixel(cy + r + 1.0) + 1, rows)
427
+ const y1: i32 = rasterEnd(rasterPixel(cy + r + 1.0), rows)
340
428
  for (let y: i32 = y0; y < y1; y++) {
341
429
  const dy: f64 = toF64(y) + 0.5 - cy
342
- const row: i32 = y * width
343
- for (let x: i32 = x0; x < x1; x++) {
430
+ // `at` walks the row as `rasterSpan`'s does.
431
+ let at: i32 = rasterAt(size, width, x0, y)
432
+ if (at < 0) {
433
+ return
434
+ }
435
+ for (let x: i32 = x0; x < x1 && at < size; x++) {
344
436
  const dx: f64 = toF64(x) + 0.5 - cx
345
437
  const d2: f64 = dx * dx + dy * dy
346
- if (d2 >= outer) {
347
- continue
438
+ if (d2 < outer) {
439
+ const w: integer<0, 256> = d2 <= inner ? fullW : rasterWeightOf((r + 0.5 - Math.sqrt(d2)) * a)
440
+ pixels[at] = w >= RASTER_SOLID ? colour : rasterMixIn(pixels[at], colour, w)
348
441
  }
349
- const w: i32 = d2 <= inner ? fullW : rasterWeight((r + 0.5 - Math.sqrt(d2)) * a)
350
- pixels[row + x] = w >= RASTER_SOLID ? colour : rasterMixWeight(pixels[row + x], colour, w)
442
+ at = at + 1
351
443
  }
352
444
  }
353
445
  }
@@ -380,25 +472,30 @@ export const rasterGlow = (
380
472
  }
381
473
  const rows: i32 = rasterRows(pixels, width, height)
382
474
  const r2: f64 = r * r
475
+ const size: i32 = toI32(pixels.length)
383
476
  const x0: i32 = Math.max(rasterPixel(cx - r), 0)
384
- const x1: i32 = Math.min(rasterPixel(cx + r) + 1, width)
477
+ const x1: i32 = rasterEnd(rasterPixel(cx + r), width)
385
478
  const y0: i32 = Math.max(rasterPixel(cy - r), 0)
386
- const y1: i32 = Math.min(rasterPixel(cy + r) + 1, rows)
479
+ const y1: i32 = rasterEnd(rasterPixel(cy + r), rows)
387
480
  for (let y: i32 = y0; y < y1; y++) {
388
481
  const dy: f64 = toF64(y) + 0.5 - cy
389
- const row: i32 = y * width
390
- for (let x: i32 = x0; x < x1; x++) {
482
+ // `at` walks the row as `rasterSpan`'s does.
483
+ let at: i32 = rasterAt(size, width, x0, y)
484
+ if (at < 0) {
485
+ return
486
+ }
487
+ for (let x: i32 = x0; x < x1 && at < size; x++) {
391
488
  const dx: f64 = toF64(x) + 0.5 - cx
392
489
  const d2: f64 = dx * dx + dy * dy
393
- if (d2 >= r2) {
394
- continue
395
- }
396
- const near: f64 = d2 / r2
397
- const fall: f64 = 1.0 - near
398
- const w: i32 = rasterWeight(fall * fall * strength)
399
- if (w > 0) {
400
- pixels[row + x] = rasterAdd(pixels[row + x], rasterMixWeight(0, colour, w))
490
+ if (d2 < r2) {
491
+ const near: f64 = d2 / r2
492
+ const fall: f64 = 1.0 - near
493
+ const w: integer<0, 256> = rasterWeightOf(fall * fall * strength)
494
+ if (w > 0) {
495
+ pixels[at] = rasterAdd(pixels[at], rasterMixIn(0, colour, w))
496
+ }
401
497
  }
498
+ at = at + 1
402
499
  }
403
500
  }
404
501
  }
@@ -428,14 +525,27 @@ const RASTER_FONT: string =
428
525
  // t to ~
429
526
  "88L889600AAAC=00AAA:400AAEE:00A:4:A00AA?1>00O248O244844244444448442448008E200"
430
527
 
431
- /** The bits of row `row` of the glyph for byte `c`; a byte outside 32..126 draws as `?`. */
432
- const rasterGlyphRow = (c: i32, row: i32): i32 => {
433
- const g: i32 = c >= 32 && c <= 126 ? c - 32 : 31
434
- const at: i32 = g * RASTER_GLYPH_H + row
435
- if (row < 0 || row >= RASTER_GLYPH_H || at < 0 || at >= toI32(RASTER_FONT.length)) {
528
+ /** The glyph number of byte `c`, its place in the font from 0 for a space; a byte outside 32..126 is `?`'s. */
529
+ const rasterGlyph = (c: i32): integer<0, 94> => {
530
+ const g: i32 = c - 32
531
+ if (g < 0 || g > 94) {
532
+ return 31
533
+ }
534
+ return g
535
+ }
536
+
537
+ /** The bits of row `row` of the glyph for byte `c` in `font`, which is `RASTER_FONT`. */
538
+ const rasterGlyphRow = (font: string, c: i32, row: i32): i32 => {
539
+ // 7 is `RASTER_GLYPH_H`, written out so the prover sees the product's bound.
540
+ if (row < 0 || row >= 7) {
541
+ return 0
542
+ }
543
+ const at: i32 = rasterGlyph(c) * 7 + row
544
+ // `at < 0` is never true, but the bound on the sum does not survive into `at`.
545
+ if (at < 0 || at >= toI32(font.length)) {
436
546
  return 0
437
547
  }
438
- return toI32(RASTER_FONT.charCodeAt(at)) - 48
548
+ return toI32(font.charCodeAt(at)) - 48
439
549
  }
440
550
 
441
551
  /**
@@ -451,7 +561,34 @@ export const rasterTextWidth = (text: string, scale: i32): i32 => {
451
561
  if (n === 0 || scale < 1) {
452
562
  return 0
453
563
  }
454
- return (n * RASTER_GLYPH_ADVANCE - 1) * Math.min(scale, RASTER_MAX_TEXT_SCALE)
564
+ // In i64: a string long enough to be wider than an `i32` saturates.
565
+ const size: integer<0, 1024> = rasterTextScale(scale)
566
+ // 6 is `RASTER_GLYPH_ADVANCE`, written out so the prover sees the bound.
567
+ const advance: i64 = toI64(size) * 6
568
+ return rasterEdge(toI64(n) * advance - toI64(size))
569
+ }
570
+
571
+ /** `v` held to the `i32` range, which keeps which side of a frame's edge it is on. */
572
+ const rasterEdge = (v: i64): i32 => {
573
+ if (v < -2147483648) {
574
+ return -2147483648
575
+ }
576
+ if (v > 2147483647) {
577
+ return 2147483647
578
+ }
579
+ return toI32(v)
580
+ }
581
+
582
+ /** A text scale held to 1..`RASTER_MAX_TEXT_SCALE`, or 0 for one under 1, which draws nothing. */
583
+ const rasterTextScale = (scale: i32): integer<0, 1024> => {
584
+ if (scale < 0) {
585
+ return 0
586
+ }
587
+ // 1024 is `RASTER_MAX_TEXT_SCALE`, written out because a range takes a literal.
588
+ if (scale > 1024) {
589
+ return 1024
590
+ }
591
+ return scale
455
592
  }
456
593
 
457
594
  /**
@@ -479,27 +616,50 @@ export const rasterText = (
479
616
  text: string,
480
617
  colour: i32
481
618
  ): void => {
482
- if (scale < 1) {
619
+ const size: integer<0, 1024> = rasterTextScale(scale)
620
+ if (size < 1) {
483
621
  return
484
622
  }
485
- const size: i32 = Math.min(scale, RASTER_MAX_TEXT_SCALE)
486
623
  const n: i32 = toI32(text.length)
487
- let gx: i32 = x
488
- for (let i: i32 = 0; i < n && gx < width; i++) {
624
+ // Positions are i64, where a glyph past either edge cannot overflow; each
625
+ // is held to the `i32` range at the draw, which keeps it past that edge.
626
+ const step: i64 = toI64(size)
627
+ // 5 and 6 are `RASTER_GLYPH_W` and `RASTER_GLYPH_ADVANCE`, written out so the
628
+ // prover sees each product's bound.
629
+ const wide: i64 = step * 5
630
+ const advance: i64 = step * 6
631
+ for (let i: i32 = 0; i < n; i++) {
632
+ // Held to the `i32` range, which changes nothing drawn: a glyph clamped
633
+ // to the top is past the right edge and ends the text, and one clamped to
634
+ // the bottom is wholly left of the frame and skipped. `top` and `left`
635
+ // are clamped the same way, so their blocks are clipped whole.
636
+ const gx: i32 = rasterEdge(toI64(x) + toI64(i) * advance)
637
+ if (gx >= width) {
638
+ return
639
+ }
489
640
  const c: i32 = toI32(text.charCodeAt(i))
490
- if (gx + RASTER_GLYPH_W * size > 0) {
641
+ if (toI64(gx) + wide > 0) {
491
642
  for (let row: i32 = 0; row < RASTER_GLYPH_H; row++) {
492
- const bits: i32 = rasterGlyphRow(c, row)
493
- const top: i32 = y + row * size
643
+ const bits: i32 = rasterGlyphRow(RASTER_FONT, c, row)
644
+ const top: i32 = rasterEdge(toI64(y) + toI64(row) * step)
494
645
  for (let col: i32 = 0; col < RASTER_GLYPH_W; col++) {
495
646
  if (((bits >> (RASTER_GLYPH_W - 1 - col)) & 1) !== 0) {
496
- const left: i32 = gx + col * size
497
- rasterRect(pixels, width, height, left, top, left + size, top + size, colour, RASTER_SOLID)
647
+ const left: i32 = rasterEdge(toI64(gx) + toI64(col) * step)
648
+ rasterRect(
649
+ pixels,
650
+ width,
651
+ height,
652
+ left,
653
+ top,
654
+ rasterEdge(toI64(left) + step),
655
+ rasterEdge(toI64(top) + step),
656
+ colour,
657
+ RASTER_SOLID
658
+ )
498
659
  }
499
660
  }
500
661
  }
501
662
  }
502
- gx = gx + RASTER_GLYPH_ADVANCE * size
503
663
  }
504
664
  }
505
665
 
@@ -550,16 +710,20 @@ export class RasterTrail {
550
710
  * @param y - the point's y
551
711
  */
552
712
  export const rasterPushTrail = (trail: RasterTrail, x: f64, y: f64): void => {
553
- const n: i32 = toI32(trail.xs.length)
554
- if (trail.head < 0 || trail.head >= n || n !== toI32(trail.ys.length)) {
713
+ const xs: f64[] = trail.xs
714
+ const ys: f64[] = trail.ys
715
+ const n: i32 = toI32(xs.length)
716
+ const head: i32 = trail.head
717
+ if (head < 0 || head >= n || n !== toI32(ys.length) || head >= toI32(ys.length)) {
555
718
  trail.head = 0
556
719
  return
557
720
  }
558
- trail.xs[trail.head] = x
559
- trail.ys[trail.head] = y
560
- trail.head = trail.head + 1 >= n ? 0 : trail.head + 1
561
- if (trail.count < n) {
562
- trail.count = trail.count + 1
721
+ xs[head] = x
722
+ ys[head] = y
723
+ trail.head = head + 1 >= n ? 0 : head + 1
724
+ const count: i32 = trail.count
725
+ if (count < n) {
726
+ trail.count = count + 1
563
727
  }
564
728
  }
565
729
 
@@ -591,22 +755,34 @@ export const rasterTrail = (
591
755
  offsetX: f64,
592
756
  offsetY: f64
593
757
  ): void => {
594
- const n: i32 = toI32(trail.xs.length)
758
+ const xs: f64[] = trail.xs
759
+ const ys: f64[] = trail.ys
760
+ const n: i32 = toI32(xs.length)
595
761
  const count: i32 = Math.min(trail.count, n)
762
+ const head: i32 = trail.head
763
+ if (count <= 0 || head < 0 || head >= n) {
764
+ return
765
+ }
766
+ // The oldest point is `count` slots behind the head, wrapping; `slot` walks
767
+ // forward from it.
768
+ let slot: i32 = head - count
769
+ if (slot < 0) {
770
+ slot = slot + n
771
+ }
596
772
  for (let i: i32 = 0; i < count; i++) {
597
- // The oldest point is `count` slots behind the head, wrapping.
598
- let slot: i32 = trail.head - count + i
599
- if (slot < 0) {
600
- slot = slot + n
773
+ // `slot` is always in the ring, so the first test never ends the walk; it
774
+ // is what proves the index.
775
+ if (slot < 0 || slot >= n) {
776
+ return
601
777
  }
602
- if (slot < 0 || slot >= n || slot >= toI32(trail.ys.length)) {
603
- continue
778
+ if (slot < toI32(ys.length)) {
779
+ const f: f64 = (toF64(i) + 1.0) / (toF64(count) + 1.0)
780
+ const radius: f64 = r * (f * 0.6 + 0.4)
781
+ const cx: f64 = offsetX + xs[slot] * scale
782
+ const cy: f64 = offsetY + ys[slot] * scale
783
+ rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
604
784
  }
605
- const f: f64 = toF64(i + 1) / toF64(count + 1)
606
- const radius: f64 = r * (f * 0.6 + 0.4)
607
- const cx: f64 = offsetX + trail.xs[slot] * scale
608
- const cy: f64 = offsetY + trail.ys[slot] * scale
609
- rasterDisc(pixels, width, height, cx, cy, radius * scale, colour, alpha * f)
785
+ slot = slot + 1 >= n ? 0 : slot + 1
610
786
  }
611
787
  }
612
788
 
@@ -689,10 +865,14 @@ export class RasterParticles {
689
865
  * @returns the live count
690
866
  */
691
867
  live(): i32 {
868
+ const lives: f64[] = this.lives
869
+ const len: i32 = toI32(lives.length)
692
870
  let n: i32 = 0
693
- for (const life of this.lives) {
694
- if (life > 0.0) {
695
- n++
871
+ for (const life of lives) {
872
+ // `n < len` is never false: it counts at most the array's elements. It
873
+ // is what proves the increment.
874
+ if (life > 0.0 && n < len) {
875
+ n = n + 1
696
876
  }
697
877
  }
698
878
  return n
@@ -752,22 +932,47 @@ export const rasterSpawnParticle = (
752
932
  size: f64,
753
933
  colour: i32
754
934
  ): void => {
935
+ const xs: f64[] = pool.xs
936
+ const ys: f64[] = pool.ys
937
+ const prevXs: f64[] = pool.prevXs
938
+ const prevYs: f64[] = pool.prevYs
939
+ const vxs: f64[] = pool.vxs
940
+ const vys: f64[] = pool.vys
941
+ const lives: f64[] = pool.lives
942
+ const spans: f64[] = pool.spans
943
+ const sizes: f64[] = pool.sizes
944
+ const colours: i32[] = pool.colours
755
945
  const i: i32 = pool.next
756
- if (!pool.has(i)) {
946
+ // `pool.has(i)` refuses a pool whose arrays differ in length; the tests
947
+ // after it restate that against each array the stores below index.
948
+ if (
949
+ !pool.has(i) ||
950
+ i < 0 ||
951
+ i >= toI32(lives.length) ||
952
+ i >= toI32(xs.length) ||
953
+ i >= toI32(ys.length) ||
954
+ i >= toI32(prevXs.length) ||
955
+ i >= toI32(prevYs.length) ||
956
+ i >= toI32(vxs.length) ||
957
+ i >= toI32(vys.length) ||
958
+ i >= toI32(spans.length) ||
959
+ i >= toI32(sizes.length) ||
960
+ i >= toI32(colours.length)
961
+ ) {
757
962
  pool.next = 0
758
963
  return
759
964
  }
760
- pool.xs[i] = x
761
- pool.ys[i] = y
762
- pool.prevXs[i] = x
763
- pool.prevYs[i] = y
764
- pool.vxs[i] = vx
765
- pool.vys[i] = vy
766
- pool.lives[i] = life > 0.0 ? life : 0.0
767
- pool.spans[i] = life > 0.0 ? life : 1.0
768
- pool.sizes[i] = size
769
- pool.colours[i] = colour
770
- pool.next = i + 1 >= pool.capacity() ? 0 : i + 1
965
+ xs[i] = x
966
+ ys[i] = y
967
+ prevXs[i] = x
968
+ prevYs[i] = y
969
+ vxs[i] = vx
970
+ vys[i] = vy
971
+ lives[i] = life > 0.0 ? life : 0.0
972
+ spans[i] = life > 0.0 ? life : 1.0
973
+ sizes[i] = size
974
+ colours[i] = colour
975
+ pool.next = i + 1 >= toI32(lives.length) ? 0 : i + 1
771
976
  }
772
977
 
773
978
  /**
@@ -781,17 +986,39 @@ export const rasterSpawnParticle = (
781
986
  export const rasterStepParticles = (pool: RasterParticles, dt: f64, gravity: f64, drag: f64): void => {
782
987
  const lost: f64 = drag * dt
783
988
  const keep: f64 = rasterUnit(1.0 - lost)
784
- for (let i: i32 = 0; i < pool.capacity(); i++) {
785
- if (!pool.has(i) || pool.lives[i] <= 0.0) {
989
+ const xs: f64[] = pool.xs
990
+ const ys: f64[] = pool.ys
991
+ const prevXs: f64[] = pool.prevXs
992
+ const prevYs: f64[] = pool.prevYs
993
+ const vxs: f64[] = pool.vxs
994
+ const vys: f64[] = pool.vys
995
+ const lives: f64[] = pool.lives
996
+ const n: i32 = toI32(lives.length)
997
+ // A pool whose arrays differ in length steps nothing, as `pool.has` decides.
998
+ if (!pool.has(toI32(0))) {
999
+ return
1000
+ }
1001
+ for (let i: i32 = 0; i < n; i++) {
1002
+ // Past the test above the arrays are one length, so this skips only dead
1003
+ // slots; the length tests are what prove each index.
1004
+ if (
1005
+ i >= toI32(xs.length) ||
1006
+ i >= toI32(ys.length) ||
1007
+ i >= toI32(prevXs.length) ||
1008
+ i >= toI32(prevYs.length) ||
1009
+ i >= toI32(vxs.length) ||
1010
+ i >= toI32(vys.length) ||
1011
+ lives[i] <= 0.0
1012
+ ) {
786
1013
  continue
787
1014
  }
788
- pool.prevXs[i] = pool.xs[i]
789
- pool.prevYs[i] = pool.ys[i]
790
- pool.vxs[i] = pool.vxs[i] * keep
791
- pool.vys[i] = (pool.vys[i] + gravity * dt) * keep
792
- pool.xs[i] = pool.xs[i] + pool.vxs[i] * dt
793
- pool.ys[i] = pool.ys[i] + pool.vys[i] * dt
794
- pool.lives[i] = pool.lives[i] - dt
1015
+ prevXs[i] = xs[i]
1016
+ prevYs[i] = ys[i]
1017
+ vxs[i] = vxs[i] * keep
1018
+ vys[i] = (vys[i] + gravity * dt) * keep
1019
+ xs[i] = xs[i] + vxs[i] * dt
1020
+ ys[i] = ys[i] + vys[i] * dt
1021
+ lives[i] = lives[i] - dt
795
1022
  }
796
1023
  }
797
1024
 
@@ -819,19 +1046,42 @@ export const rasterParticles = (
819
1046
  offsetX: f64,
820
1047
  offsetY: f64
821
1048
  ): void => {
822
- for (let i: i32 = 0; i < pool.capacity(); i++) {
823
- if (!pool.has(i) || pool.lives[i] <= 0.0) {
1049
+ const xs: f64[] = pool.xs
1050
+ const ys: f64[] = pool.ys
1051
+ const prevXs: f64[] = pool.prevXs
1052
+ const prevYs: f64[] = pool.prevYs
1053
+ const lives: f64[] = pool.lives
1054
+ const spans: f64[] = pool.spans
1055
+ const sizes: f64[] = pool.sizes
1056
+ const colours: i32[] = pool.colours
1057
+ const n: i32 = toI32(lives.length)
1058
+ // As in `rasterStepParticles`: a malformed pool draws nothing.
1059
+ if (!pool.has(toI32(0))) {
1060
+ return
1061
+ }
1062
+ for (let i: i32 = 0; i < n; i++) {
1063
+ // As in `rasterStepParticles`: only dead slots are skipped.
1064
+ if (
1065
+ i >= toI32(xs.length) ||
1066
+ i >= toI32(ys.length) ||
1067
+ i >= toI32(prevXs.length) ||
1068
+ i >= toI32(prevYs.length) ||
1069
+ i >= toI32(spans.length) ||
1070
+ i >= toI32(sizes.length) ||
1071
+ i >= toI32(colours.length) ||
1072
+ lives[i] <= 0.0
1073
+ ) {
824
1074
  continue
825
1075
  }
826
- const size: f64 = pool.sizes[i]
1076
+ const size: f64 = sizes[i]
827
1077
  const half: f64 = size / 2.0
828
- const x: f64 = pool.prevXs[i] + (pool.xs[i] - pool.prevXs[i]) * alpha - half
829
- const y: f64 = pool.prevYs[i] + (pool.ys[i] - pool.prevYs[i]) * alpha - half
1078
+ const x: f64 = prevXs[i] + (xs[i] - prevXs[i]) * alpha - half
1079
+ const y: f64 = prevYs[i] + (ys[i] - prevYs[i]) * alpha - half
830
1080
  const x0: i32 = rasterPixel(offsetX + x * scale + 0.5)
831
1081
  const y0: i32 = rasterPixel(offsetY + y * scale + 0.5)
832
1082
  const x1: i32 = rasterPixel(offsetX + (x + size) * scale + 0.5)
833
1083
  const y1: i32 = rasterPixel(offsetY + (y + size) * scale + 0.5)
834
- const weight: i32 = rasterWeight(pool.lives[i] / pool.spans[i])
835
- rasterRect(pixels, width, height, x0, y0, x1, y1, pool.colours[i], weight)
1084
+ const weight: i32 = rasterWeight(lives[i] / spans[i])
1085
+ rasterRect(pixels, width, height, x0, y0, x1, y1, colours[i], weight)
836
1086
  }
837
1087
  }