random() fixes and liskov port

This commit is contained in:
James Turk 2024-07-07 00:38:41 -05:00
parent 6b8b20d286
commit 627ce43e4f
4 changed files with 57 additions and 9 deletions

View File

@ -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;

View File

@ -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))];
}
}

View File

@ -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);

31
examples/liskov.html Normal file
View File

@ -0,0 +1,31 @@
<html>
<head> </head>
<body>
<canvas id="mainCanvas" width="500" height="500"></canvas>
<script type="module">
import {
artworld,
Color,
Pico8,
Group,
Rect,
Arc,
Line,
Circle,
Random,
Vector2,
degToRad,
} from "../artworld/index.js";
artworld.bindCanvas("mainCanvas");
window.artworld = artworld;
let types = [Rect, Line, Circle, Arc];
for (let i = 0; i < 100; i++) {
let Cls = Random.choice(types);
new Cls().random().strokeWeight(2);
}
artworld.draw();
</script>
</body>
</html>