Skip to content

Commit e5657f9

Browse files
authored
Refactor setDot and related methods to use optional z
1 parent aeedddf commit e5657f9

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

class.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,17 @@ class polyview extends polybase {
266266
return Math.map(z, this.near, this.far, 0x00, 0xff) >> 0;
267267
}
268268

269-
setDot(x: number, y: number, z: number, c: number) {
269+
setDot(x: number, y: number, c: number, z?: number) {
270270
if (this.isOutOfArea(x, y)) return;
271271
const i = this.pos2idx(x, this.height, y);
272-
const zUint8 = this.distToUint8(z);
273-
if (z <= 0x00 || z >= 0xff) return;
274-
if (this.zBuffer[i] > zUint8) return;
272+
const zUint8 = z ? this.distToUint8(z) : 0;
273+
if (z && (zUint8 <= 0x00 || zUint8 >= 0xff)) return;
274+
if (z && (this.zBuffer[i] > zUint8)) return;
275+
if (z) this.zBuffer[i] = zUint8;
275276
this.cBuffer[i] = c;
276-
this.zBuffer[i] = zUint8;
277277
}
278278

279-
drawLine(x0: number, y0: number, x1: number, y1: number, z: number, color: number, idx?: number) {
279+
drawLine(x0: number, y0: number, x1: number, y1: number, color: number, z?: number) {
280280
color &= 0xF;
281281
if (x0 === x1 && y0 === y1) { this.setDot(fxpic, x0, y0, z, color); return; }
282282
const w = this.width;
@@ -294,7 +294,7 @@ class polyview extends polybase {
294294
while (1) {
295295
if (((sx < 0 && x0 < 0) || (sx > 0 && x0 >= w) && sx !== 0) ||
296296
((sy < 0 && y0 < 0) || (sy > 0 && y0 >= h) && sy !== 0)) break;
297-
this.setDot(fxpic, x0, y0, z, color);
297+
this.setDot(fxpic, x0, y0, color, z);
298298

299299
// ตรวจทิศทาง + เกินจุดหมายหรือยัง (ป้องกัน overflow)
300300
if (((sx > 0 && x0 >= x1) || (sx < 0 && x0 <= x1) && sx !== 0) ||
@@ -306,15 +306,15 @@ class polyview extends polybase {
306306
}
307307
}
308308

309-
setRows(x: number, src: Buffer, z: number) {
310-
const zUint8 = this.distToUint8(z);
311-
if (zUint8 <= 0x00 || zUint8 >= 0xff) return;
309+
setRows(x: number, src: Buffer, z?: number) {
310+
const zUint8 = z ? this.distToUint8(z) : 0;
311+
if (z && (zUint8 <= 0x00 || zUint8 >= 0xff)) return;
312312
const n = x * this.height;
313313
const m = Math.min(this.height, src.length)
314314
for (let i = 0; i < m; i++) {
315315
const i_n = i + n;
316-
if (this.zBuffer[i_n] > zUint8) continue;
317-
this.zBuffer[i_n] = zUint8;
316+
if (z && (this.zBuffer[i_n] > zUint8)) continue;
317+
if (z) this.zBuffer[i_n] = zUint8;
318318
this.cBuffer[i_n] = src[i];
319319
}
320320
}
@@ -385,20 +385,19 @@ class polyview extends polybase {
385385
x0: number, y0: number,
386386
x1: number, y1: number,
387387
x2: number, y2: number,
388-
z: number, color: number
388+
color: number, z?: number
389389
) {
390-
this.drawLine(fxpic, x1, y1, x0, y0, z, color, idx);
391-
this.drawLine(fxpic, x2, y2, x1, y1, z, color, idx);
392-
this.drawLine(fxpic, x0, y0, x2, y2, z, color, idx);
390+
this.drawLine(fxpic, x1, y1, x0, y0, color, z);
391+
this.drawLine(fxpic, x2, y2, x1, y1, color, z);
392+
this.drawLine(fxpic, x0, y0, x2, y2, color, z);
393393
}
394394

395395
fillTriangle(
396396
x0: number, y0: number,
397397
x1: number, y1: number,
398398
x2: number, y2: number,
399-
z: number, color: number
399+
color: number, z?: number
400400
) {
401-
402401
color &= 0xF;
403402
const w = this.width;
404403
const h = this.height;
@@ -419,7 +418,7 @@ class polyview extends polybase {
419418
const rowBuf = pins.createBuffer(h);
420419

421420
for (let x = Math.max(0, minX | 0); x <= Math.min(w - 1, maxX | 0); x++) {
422-
this.getRows(idx + x, rowBuf);
421+
this.getRows(idx + x, this.buf);
423422

424423
// หา y range สำหรับ x นี้ (intersect กับ 3 ขอบ)
425424
let yStart = h;
@@ -456,8 +455,8 @@ class polyview extends polybase {
456455
if (yStart <= yEnd) {
457456
const clipYStart = Math.max(minY, Math.ceil(yStart));
458457
const clipYEnd = Math.min(maxY, Math.floor(yEnd));
459-
for (let y = clipYStart; y <= clipYEnd; y++) if (rowBuf[y] !== color) rowBuf[y] = color;
460-
this.setRows(x, rowBuf);
458+
for (let y = clipYStart; y <= clipYEnd; y++) if (this.buf[y] !== color) this.buf[y] = color;
459+
this.setRows(x, this.buf, z);
461460
}
462461
}
463462
}

0 commit comments

Comments
 (0)