2024-12-08 19:53:06 -05:00
|
|
|
/**
|
|
|
|
* ImageData is an array that contains
|
|
|
|
* four elements per pixel (one for each
|
|
|
|
* red, green, blue, and alpha value).
|
|
|
|
* This maps from pixel coordinates
|
|
|
|
* to the array index
|
|
|
|
*/
|
|
|
|
function imageIndex(
|
|
|
|
width: number,
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
) {
|
|
|
|
return y * (width * 4) + x * 4;
|
|
|
|
}
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
export function plot(
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
img: ImageData
|
|
|
|
) {
|
|
|
|
// Translate (x,y) coordinates
|
|
|
|
// to pixel coordinates.
|
|
|
|
// Also known as a "camera" function.
|
|
|
|
//
|
|
|
|
// The display range is:
|
|
|
|
// x=[0, 1]
|
|
|
|
// y=[0, 1]
|
|
|
|
let pixelX = Math.floor(x * img.width);
|
|
|
|
let pixelY = Math.floor(y * img.height);
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
const index = imageIndex(
|
2024-12-08 22:50:46 -05:00
|
|
|
img.width,
|
|
|
|
pixelX,
|
|
|
|
pixelY
|
2024-12-08 19:53:06 -05:00
|
|
|
);
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
// Skip pixels outside the display range
|
|
|
|
if (
|
2024-12-08 22:50:46 -05:00
|
|
|
index < 0 ||
|
|
|
|
index > img.data.length
|
2024-12-08 19:53:06 -05:00
|
|
|
) {
|
2024-12-08 22:50:46 -05:00
|
|
|
return;
|
2024-12-08 19:53:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the pixel to black by writing 0
|
|
|
|
// to the first three elements,
|
|
|
|
// and 255 to the last element
|
|
|
|
img.data[index] = 0;
|
|
|
|
img.data[index + 1] = 0;
|
|
|
|
img.data[index + 2] = 0;
|
|
|
|
img.data[index + 3] = 0xff;
|
2024-11-17 17:30:07 -05:00
|
|
|
}
|