Skip to content

Commit a1cb28f

Browse files
committed
fix incArea out of bounds
1 parent e4046a1 commit a1cb28f

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/model/Area.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,31 @@ export class Area {
121121
this.area[i + j * this.width] = v;
122122
}
123123

124-
incArea(area: Area, factor: number) {
125-
if (area.width <= 0 || area.height <= 0 || factor === 0) {
124+
incArea(sub: Area, factor: number) {
125+
if (sub.width <= 0 || sub.height <= 0 || factor === 0) {
126126
return;
127127
}
128128
// assume it is within the bounds
129129
const w = this.width;
130-
const aw = area.width;
131-
const base = area.i + area.j * w;
132-
for (let j = 0; j < area.height; j++) {
133-
const sRow = base + j * w;
134-
const tRow = j * aw;
135-
for (let i = 0; i < area.width; i++) {
136-
const v = area.area[i + tRow];
130+
const aw = sub.width;
131+
const i1 = Math.max(0, sub.i);
132+
const j1 = Math.max(0, sub.j);
133+
const i2 = Math.min(sub.i + sub.width, w);
134+
const j2 = Math.min(sub.j + sub.height, this.height);
135+
136+
if (j2 <= 0 || i2 <= 0 || i1 >= w || j2 >= this.height) {
137+
return;
138+
}
139+
140+
for (let j = j1; j < j2; j++) {
141+
const subRow = (j - sub.j) * aw;
142+
const row = j * w;
143+
for (let i = i1; i < i2; i++) {
144+
const v = sub.area[i - sub.i + subRow];
137145
if (v === 0) {
138146
continue;
139147
}
140-
this.area[i + sRow] += factor * v;
148+
this.area[i + row] += factor * v;
141149
}
142150
}
143151
}

0 commit comments

Comments
 (0)