in libs/screen/sim/image.ts [443:525]
export function drawLine(img: RefImage, x0: number, y0: number, x1: number, y1: number, c: number) {
x0 |= 0
y0 |= 0
x1 |= 0
y1 |= 0
if (x1 < x0) {
drawLine(img, x1, y1, x0, y0, c);
return;
}
let w = x1 - x0;
let h = y1 - y0;
if (h == 0) {
if (w == 0)
setPixel(img, x0, y0, c);
else
fillRect(img, x0, y0, w + 1, 1, c);
return;
}
if (w == 0) {
if (h > 0)
fillRect(img, x0, y0, 1, h + 1, c);
else
fillRect(img, x0, y1, 1, -h + 1, c);
return;
}
if (x1 < 0 || x0 >= img._width)
return;
if (x0 < 0) {
y0 -= (h * x0 / w) | 0;
x0 = 0;
}
if (x1 >= img._width) {
let d = (img._width - 1) - x1;
y1 += (h * d / w) | 0;
x1 = img._width - 1
}
if (y0 < y1) {
if (y0 >= img._height || y1 < 0)
return;
if (y0 < 0) {
x0 -= (w * y0 / h) | 0;
y0 = 0;
}
if (y1 >= img._height) {
let d = (img._height - 1) - y1;
x1 += (w * d / h) | 0;
y1 = img._height
}
} else {
if (y1 >= img._height || y0 < 0)
return;
if (y1 < 0) {
x1 -= (w * y1 / h) | 0;
y1 = 0;
}
if (y0 >= img._height) {
let d = (img._height - 1) - y0;
x0 += (w * d / h) | 0;
y0 = img._height
}
}
img.makeWritable()
if (h < 0) {
h = -h;
if (h < w)
drawLineLow(img, x0, y0, x1, y1, c);
else
drawLineHigh(img, x1, y1, x0, y0, c);
} else {
if (h < w)
drawLineLow(img, x0, y0, x1, y1, c);
else
drawLineHigh(img, x0, y0, x1, y1, c);
}
}