Skip to content

Commit

Permalink
feat: circle no longer constant
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Dec 19, 2021
1 parent 49a52e0 commit abf7631
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { World, Scene, Circle } from '../mod.ts';


class Game extends Scene {
public test = new Circle(100, 100, 200, "#00ff00");
public test = new Circle(300, 300, 40, "#00ff00");

public setup() {
this.addChild(this.test);
Expand Down
1 change: 1 addition & 0 deletions src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Renderer {
);
this.world.fillRect(entity.x, entity.y, entity.width, entity.height);
} else if (entity instanceof Circle) {
entity.update();
this.world.setDrawColor(
entity.fill[0],
entity.fill[1],
Expand Down
19 changes: 13 additions & 6 deletions src/entities/geometry/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ export class Circle extends Entity {
public fill: RGBA;
public radius: number;
public points: Array<Point> = [];
public prevX = Infinity;
public prevY = Infinity;
constructor(x: number, y: number, radius: number, fill: RGBA | string) {
super(x, y);
this.fill = typeof fill === 'string' ? hexToRGBA(fill) : fill;
this.radius = radius;

for (let w = 0; w < radius * 2; w++) {
for (let h = 0; h < radius * 2; h++) {
const dx = radius - w;
const dy = radius - h;
if ((dx * dx + dy * dy) <= (radius * radius)) {
}
public update() {
if (this.prevX === this.x && this.prevY === this.y) return;
this.points = [];
for (let w = 0; w < this.radius * 2; w++) {
for (let h = 0; h < this.radius * 2; h++) {
const dx = this.radius - w;
const dy = this.radius - h;
if ((dx * dx + dy * dy) <= (this.radius * this.radius)) {
this.points.push(new Point(this.x + dx, this.y + dy));
}
}
}
this.prevX = this.x;
this.prevY = this.y;
}
}

0 comments on commit abf7631

Please sign in to comment.