class Star extends Decoration{ raysCount: number triangles: p5.Vector[][] innerRadius: number constructor(properties: object){ super(properties); this.createTriangles(); } get properties(): object{ let obj = super.properties; obj['raysCount'] = this.raysCount; obj['innerRadius'] = this.innerRadius; return obj; } createTriangles(){ this.triangles = []; let increment = p.TWO_PI / this.raysCount / 2, firstOuter = true; for (let i = 0; i < p.TWO_PI; i += increment){ let x1 = p.sin(i), y1 = -p.cos(i), x2 = p.sin(i + increment), y2 = -p.cos(i + increment); if (firstOuter){ x1 *= this.radius, y1 *= this.radius, x2 *= this.innerRadius, y2 *= this.innerRadius; } else { x2 *= this.radius, y2 *= this.radius, x1 *= this.innerRadius, y1 *= this.innerRadius; } firstOuter = !firstOuter; this.triangles.push([ p.createVector(x1, y1), p.createVector(x2, y2), p.createVector(0, 0) ]); } } brightness(value: number) { this.triangles.forEach((t, i) => { p.fill(0, 0, 0, value); p.triangle(t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y); }); } display(pos?: p5.Vector, dim?: p5.Vector){ super.display(pos, dim); p.noStroke(); this.triangles.forEach((t, i) => { let color = this.colors[i % this.colors.length]; p.fill(color); p.triangle(t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y); }); if (this.mouseIsOver){ this.brightness(70); } p.pop(); } update(){ super.update(); } toJSON(index){ let list = [ "raysCount", "innerRadius" ] let obj = super.toJSON(index); for (let item of list){ obj[item] = this[item]; } return obj; } }