Add ellipse drawing

This commit is contained in:
dylan
2023-05-10 19:58:01 -07:00
parent 6dc5127926
commit d7fec98714
4 changed files with 45 additions and 2 deletions

View File

@@ -194,6 +194,44 @@ export const outlineCircle = (x: number, y: number, r: number, color: number) =>
}
}
export const fillEllipse = (x0: number, y0: number, x1: number, y1: number, color: number) => {
const x = 0.5*(x0 + x1);
const y = 0.5*(y0 + y1);
const rx = Math.abs(x0-x1)/2;
const ry = Math.abs(y0-y1)/2;
const left = Math.floor(x-rx-1);
const top = Math.floor(y-ry-1);
for (let i = left; i <= Math.ceil(x+rx+1); i ++) {
for (let j = top; j <= Math.ceil(y+ry+1); j ++) {
if (Math.sqrt(((x-i)/rx)**2 + ((y-j)/ry)**2) <= 1+(1/(rx+ry))) {
setPixelColor(i, j, color);
}
}
}
}
export const outlineEllipse = (x0: number, y0: number, x1: number, y1: number, color: number) => {
const x = 0.5*(x0 + x1);
const y = 0.5*(y0 + y1);
const rx = Math.abs(x0-x1)/2;
const ry = Math.abs(y0-y1)/2;
const left = Math.floor(x-rx-1);
const top = Math.floor(y-ry-1);
const inR = (d: number) => d <= 1+1/(rx+ry);
for (let i = left; i <= Math.ceil(x+rx+1); i ++) {
for (let j = top; j <= Math.ceil(y+ry+1); j ++) {
const d = Math.sqrt(((x-i)/rx)**2 + ((y-j)/ry)**2);
if (inR(d)) {
const dh = Math.sqrt(((x-(i+Math.sign(i-x)))/rx)**2 + ((y-j)/ry)**2);
const dv = Math.sqrt(((x-i)/rx)**2 + ((y-(j+Math.sign(j-y)))/ry)**2);
if (!inR(dh) || !inR(dv)) {
setPixelColor(i, j, color);
}
}
}
}
}
export const fillRectRaw = (x: number, y: number, w: number, h: number, color: number) => {
setPixelsInRectRaw(x, y, w, Array(w*h).fill(color));
}