From 627ce43e4fde26d2c4a1975ee4a8ca69edac4382 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 7 Jul 2024 00:38:41 -0500 Subject: [PATCH] random() fixes and liskov port --- artworld/drawable.js | 6 ------ artworld/random.js | 4 ++++ artworld/shapes.js | 25 ++++++++++++++++++++++--- examples/liskov.html | 31 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 examples/liskov.html diff --git a/artworld/drawable.js b/artworld/drawable.js index 42d0928..3ab659f 100644 --- a/artworld/drawable.js +++ b/artworld/drawable.js @@ -3,12 +3,6 @@ import { Vector2 } from "./math.js"; import { Color } from "./color.js"; import { Random } from "./random.js"; -function cloneObject(obj) { - for (let [k, v] of Object.entries(obj)) { - console.log(k, v); - } -} - export class Drawable { constructor(parent) { this._parent = parent; diff --git a/artworld/random.js b/artworld/random.js index 6d77338..8599377 100644 --- a/artworld/random.js +++ b/artworld/random.js @@ -16,4 +16,8 @@ export class Random { static radians() { return Math.random() * Math.PI * 2; } + + static choice(array) { + return array[Math.floor(Random.under(array.length))]; + } } diff --git a/artworld/shapes.js b/artworld/shapes.js index 2bf7efb..4021fa1 100644 --- a/artworld/shapes.js +++ b/artworld/shapes.js @@ -24,9 +24,14 @@ export class Line extends Drawable { return makeCopy(Line, this, overrides); } + random(range = 100) { + super.random(); + this._offsetVec = Vector2.random(range, range); + return this; + } + draw() { - artworld.setStrokeColor(this._stroke); - artworld.setStrokeWeight(this._strokeWeight); + artworld.prepareDraw(this); artworld.drawLine(this.worldPos, this.worldPos.add(this._offsetVec)); } @@ -48,10 +53,18 @@ export class Arc extends Drawable { this._startAngle = 0; this._endAngle = 180; } + copy(overrides) { return makeCopy(Arc, this, overrides); } + random(range = 100) { + super.random(); + this._r = Random.between(20, range); + this._startAngle = Random.radians(); + this._endAngle = Random.radians(); + return this; + } draw() { artworld.prepareDraw(this); artworld.drawArc(this.worldPos, this._r, this._startAngle, this._endAngle); @@ -80,6 +93,12 @@ export class Circle extends Drawable { return makeCopy(Circle, this, overrides); } + random(range = 100) { + super.random(); + this._r = Random.between(20, range); + return this; + } + draw() { artworld.prepareDraw(this); artworld.drawArc(this.worldPos, this._r, 0, 2 * Math.PI); @@ -98,7 +117,7 @@ export class Rect extends Drawable { this._height = 50; } - random(range) { + random(range = 100) { super.random(); this._width = Random.between(20, range); this._height = Random.between(20, range); diff --git a/examples/liskov.html b/examples/liskov.html new file mode 100644 index 0000000..875a350 --- /dev/null +++ b/examples/liskov.html @@ -0,0 +1,31 @@ + + + + + + +