Skip to content

Commit aab5213

Browse files
committed
Very slightly improve intersector performance
It just avoid useless computations.
1 parent 30fdf16 commit aab5213

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/core/intersector.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ class Intersector {
150150

151151
#minX;
152152

153+
#maxX;
154+
153155
#minY;
154156

157+
#maxY;
158+
155159
#invXRatio;
156160

157161
#invYRatio;
@@ -175,9 +179,13 @@ class Intersector {
175179
}
176180
this.#minX = minX;
177181
this.#minY = minY;
182+
this.#maxX = maxX;
183+
this.#maxY = maxY;
178184
this.#invXRatio = (STEPS - 1) / (maxX - minX);
179185
this.#invYRatio = (STEPS - 1) / (maxY - minY);
180186
for (const intersector of intersectors) {
187+
// TODO: instead of using the intersector bounds, we could iterate over
188+
// the grid cells that the quad points intersect.
181189
const iMin = this.#getGridIndex(intersector.minX, intersector.minY);
182190
const iMax = this.#getGridIndex(intersector.maxX, intersector.maxY);
183191
const w = (iMax - iMin) % STEPS;
@@ -197,17 +205,16 @@ class Intersector {
197205
#getGridIndex(x, y) {
198206
const i = Math.floor((x - this.#minX) * this.#invXRatio);
199207
const j = Math.floor((y - this.#minY) * this.#invYRatio);
200-
return i >= 0 && i < STEPS && j >= 0 && j < STEPS ? i + j * STEPS : -1;
208+
return i + j * STEPS;
201209
}
202210

203211
addGlyph(transform, width, height, glyph) {
204212
const x = transform[4] + width / 2;
205213
const y = transform[5] + height / 2;
206-
const index = this.#getGridIndex(x, y);
207-
if (index < 0) {
214+
if (x < this.#minX || y < this.#minY || x > this.#maxX || y > this.#maxY) {
208215
return;
209216
}
210-
const intersectors = this.#grid[index];
217+
const intersectors = this.#grid[this.#getGridIndex(x, y)];
211218
if (!intersectors) {
212219
return;
213220
}

0 commit comments

Comments
 (0)