xxxxxxxxxx
56
let pg;
function setup() {
createCanvas(400, 400);
pg = createGraphics(width, height);
}
function draw() {
background(220);
const ast = new Astroid(100);
ast.draw(pg);
image(pg,0,0);
}
class Astroid {
/**
* アステロイドクラスのコンストラクタ
* @constructor
* @param {number} p - p5.jsのインスタンス
* @param {number} r - 円の半径
*/
constructor(r) {
this.r = r;
this.a = 3;
this.points = [];
this.init();
}
/**
* アステロイドを描画する
*/
init() {
for (let i = 0; i < 120; i++) {
const angle = radians(map(i, 0, 120, 0, 360));
const x = (this.r / 4) * this.a * pow(cos(angle), 3);
const y = (this.r / 4) * this.a * pow(sin(angle), 3);
this.points.push(createVector(x, y));
}
}
draw(pg) {
const points = this.points;
pg.push();
pg.beginShape();
pg.noFill();
pg.stroke(255);
pg.strokeWeight(3);
pg.translate(pg.width / 2, pg.height / 2);
for (let i = 0; i < points.length; i++) {
pg.vertex(points[i].x, points[i].y);
}
pg.endShape(CLOSE);
pg.pop();
}
}