xxxxxxxxxx
118
class saturnRings {
constructor(widthRing, heightRing) {
this.widthRing = widthRing;
this.heightRing = heightRing;
}
draw() {
//add floating "rocks" to the rings
stroke(250, 90, 32);
strokeWeight(random(1, 5));
//draw a point at a random coordinate within the area covered by rings
point(random(-300, 300), random(-50, 50));
//style the saturn rings
//choose a shade of red at random
this.randomRed = random(100, 250);
stroke(this.randomRed, 11, 10);
strokeWeight(random(0, 1));
noFill();
ellipse(0, 0, this.widthRing, this.heightRing);
}
}
class saturnBody {
constructor(offY, point1, point2) {
//Y offset
this.offY = offY;
//X_1
this.point1 = point1;
//X_2
this.point2 = point2;
}
draw() {
//style the body lines
//choose a shade of red at random
this.randomRed = random(180, 255);
stroke(this.randomRed, 40, 9);
strokeWeight(random(0, 1));
noFill();
//draw two parallel lines going in opposite directions
line(this.point1, this.offY, this.point2, this.offY);
line(this.point1, -this.offY, this.point2, -this.offY);
}
}
//use Pythagorean theorem to find point X_n
function differenceOfSquares(num1, num2) {
let diff = num1 * num1 - num2 * num2;
return sqrt(abs(diff));
}
let saturnR = [];
let saturnB = [];
let ringsQuantity = 90;
let bodyQuantity = 30;
//radius of the body
let radius = 150;
function setup() {
createCanvas(500, 500);
ellipseMode(CENTER);
frameRate(8);
//build Saturn rings
for (let i = 0; i < ringsQuantity; i++) {
saturnR.push(new saturnRings(400 + i * 5, 30 + i));
}
//build Saturn body lines
for (let k = 0; k < bodyQuantity; k++) {
saturnB.push(
new saturnBody(
k * 5,
differenceOfSquares(radius, k * 5),
-differenceOfSquares(radius, k * 5)
)
);
}
}
function draw() {
background(26, 19, 82);
//rotate everything by 25 degrees
translate(width / 2, height / 2);
rotate(0.436332);
//draw stars at random coordinates within the limits of canvas
for (let b = 0; b < 50; b++) {
stroke(50, 53, 227);
strokeWeight(random(1, 3));
point(random(-300, 300), random(-300, 300));
}
//draw Saturn body
fill(237, 40, 9);
noStroke();
circle(0, 0, radius * 2);
//draw Saturn rings
for (i = 0; i < ringsQuantity; i++) {
saturnR[i].draw();
}
//draw upper half of Saturn body to cover the back part of rings
fill(237, 40, 9);
noStroke();
arc(0, 0, radius * 2, radius * 2, PI, 0, OPEN);
//draw Saturn body rings
for (i = 0; i < bodyQuantity; i++) {
saturnB[i].draw();
}
}