Add ellipse drawing
This commit is contained in:
parent
6dc5127926
commit
d7fec98714
@ -5,6 +5,8 @@ import {
|
||||
cameraPos,
|
||||
fillCircle,
|
||||
outlineCircle,
|
||||
fillEllipse,
|
||||
outlineEllipse,
|
||||
} from "./window.ts";
|
||||
import { Font, font } from "./font.ts";
|
||||
import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
|
||||
@ -96,6 +98,8 @@ const faux = {
|
||||
rect: outlineRect,
|
||||
circfill: fillCircle,
|
||||
circ: outlineCircle,
|
||||
ovalfill: fillEllipse,
|
||||
oval: outlineEllipse,
|
||||
map: (mapSheet: number, tileX: number, tileY: number, screenX: number, screenY: number, tileW: number, tileH: number) => {
|
||||
const originalSpritesheet = getSpritesheet() ?? 0;
|
||||
getMapSheet(mapSheet).forEach(([sprSheet, spr], i) => {
|
||||
|
1
carts/tmp/oval.fx
Normal file
1
carts/tmp/oval.fx
Normal 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}]
|
@ -29,8 +29,8 @@
|
||||
- [x] camera
|
||||
- [x] circ
|
||||
- [x] circfill
|
||||
- [ ] oval
|
||||
- [ ] ovalfill
|
||||
- [x] oval
|
||||
- [x] ovalfill
|
||||
- [ ] line
|
||||
- [x] rect
|
||||
- [x] rectfill
|
||||
|
38
window.ts
38
window.ts
@ -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));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user