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

@ -5,6 +5,8 @@ import {
cameraPos, cameraPos,
fillCircle, fillCircle,
outlineCircle, outlineCircle,
fillEllipse,
outlineEllipse,
} from "./window.ts"; } from "./window.ts";
import { Font, font } from "./font.ts"; import { Font, font } from "./font.ts";
import { keyDown, keyPressed, keyReleased } from "./keyboard.ts"; import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
@ -96,6 +98,8 @@ const faux = {
rect: outlineRect, rect: outlineRect,
circfill: fillCircle, circfill: fillCircle,
circ: outlineCircle, circ: outlineCircle,
ovalfill: fillEllipse,
oval: outlineEllipse,
map: (mapSheet: number, tileX: number, tileY: number, screenX: number, screenY: number, tileW: number, tileH: number) => { map: (mapSheet: number, tileX: number, tileY: number, screenX: number, screenY: number, tileW: number, tileH: number) => {
const originalSpritesheet = getSpritesheet() ?? 0; const originalSpritesheet = getSpritesheet() ?? 0;
getMapSheet(mapSheet).forEach(([sprSheet, spr], i) => { getMapSheet(mapSheet).forEach(([sprSheet, spr], i) => {

1
carts/tmp/oval.fx Normal file
View File

@ -0,0 +1 @@
[{"sheet_type":"code","value":"// Sample\n\nlet r = 0;\nlet d = 1;\nreturn {\n\tinit() {},\n\tupdate() {\n\t\tif (r >= 20) {\n\t\t\td = -1;\n\t\t} else if (r < 0) {\n\t\t\td = 1;\n\t\t}\n\t\tr += d*0.1;\t\n\t},\n\tdraw() {\n\t\tcls();\n\t\trectfill(50,50,46,21,6);\n\t\toval(50, 50, 95, 70, 17);\n\t\ttxt(10,10,\"Hello, World!\");\n\t}\n}"},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null},{"sheet_type":"none","value":null}]

View File

@ -29,8 +29,8 @@
- [x] camera - [x] camera
- [x] circ - [x] circ
- [x] circfill - [x] circfill
- [ ] oval - [x] oval
- [ ] ovalfill - [x] ovalfill
- [ ] line - [ ] line
- [x] rect - [x] rect
- [x] rectfill - [x] rectfill

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) => { export const fillRectRaw = (x: number, y: number, w: number, h: number, color: number) => {
setPixelsInRectRaw(x, y, w, Array(w*h).fill(color)); setPixelsInRectRaw(x, y, w, Array(w*h).fill(color));
} }