xxxxxxxxxx
160
function island (x, y, r, type)
{
this.r = r;
this.x = x;
this.y = y;
this.type = type;
this.show = function() {
fill(255, 50, 50);
stroke(255, 50, 50);
strokeWeight(4);
ellipse(this.x, this.y, 20, 20);
let description = "";
if (this.type < 0) {
description = "valley";
} else if (this.type < 1) {
description = "hill";
} else {
description = "mountain";
}
textFont('Helvetica');
textStyle(BOLDITALIC);
fill(255, 50, 50);
textSize(10);
noStroke();
textAlign(CENTER, TOP);
text("this is a " + description, this.x, this.y + 15);
}
}
function setup() {
createCanvas(640, 640);
boolDoRefresh = true;
anIsland = new island(100, 100);
d = pixelDensity();
Pscale = d*4;
scaling = 1200;
levels = 7;
levelsStep = 35;
}
function islandGen(num) {
//generate an island with random radius, random location
//+1 => mountain, -1 => valley
let islands = [];
for (let i = 0; i < num; i ++) {
let newIsland = new island (random(width), random(height), random(30,65), random([1,0.3,0.5,1,-0.1,-0.3]));
islands[i] = newIsland;
}
return islands;
}
function calcIslandColor(sum, l, ls) { //level, levelStep
//do it on grayscale first
for (let i = 0; i < l; i ++) {
if (i * ls > sum ) {
return color(i * ls);
}
}
return color(l*ls);
}
function draw() {
if (boolDoRefresh) {
//lala init
background(220);
let pink = color(255, 102, 204);
let wid = width*d*4;
let high = height*d*4;
let islandCount = random(6, 12);
let islands = islandGen(islandCount);
let maxsum = 0;
let colordist = color("green");
print("init");
//
loadPixels();
//Thanks to *Coding Challenge #28: Metaballs! https://www.youtube.com/watch?v=ccYLb7cLB1I
for (let y = 0; y < high; y += 4) {
for (let x = 0; x < wid; x+= 4){
let i = y * width*2 + x;
let sum = 0;
for (let j = 0; j < islandCount; j++) {
let distance = dist(x, y, islands[j].x*Pscale, islands[j].y*Pscale);
sum += scaling* islands[j].r*islands[j].type/distance;
}
let colordist = calcIslandColor(sum, levels, levelsStep);
//let colordist = color(scaling*sum);
pixels[i] = red(colordist);
pixels[i + 1] = green(colordist);
pixels[i + 2] = blue(colordist);
pixels[i + 3] = alpha(colordist);
}
}
updatePixels();
/*debugg
print(maxsum);
for (let i = 0; i < islandCount; i++) {
islands[i].show();
}
*/
//FINAL UI-----------------------------------
islands[0].show();
//grids
stroke(255);
strokeWeight(1);
for (let q = 0; q<6; q++) {
line(q*width/6, 0, q*width/6, height);
line(0, q*height/6, width, q*height/6);
}
//coordinates
textFont('Helvetica');
textStyle(BOLDITALIC);
fill(255, 50, 50);
textSize(18);
noStroke();
textAlign(LEFT, TOP);
let NS = str(int(random(90))) + '° '+ str(int(random(1000, 9999))) + "' "+ str(int(random(90))) + '" ' + random(["N", "S"]);
let EW = str(int(random(90))) + '° '+ str(int(random(1000, 9999))) + "' "+ str(int(random(90))) + '" ' + random(["E", "W"]);
text(NS, 50, 50);
text(EW, 50, 70);
//legend
strokeWeight(2);
stroke(255);
for (let i = 0; i < levels; i++) {
fill(i*levelsStep);
rect(590, 590 -i*20, 20, 20);
}
textFont('Helvetica');
textStyle(BOLD);
fill(255, 50, 50);
noStroke();
textSize(10);
textAlign(RIGHT, TOP);
text("-200", 580, 600);
textAlign(RIGHT, BOTTOM);
text("+500", 580, 480);
boolDoRefresh = false;
}
}
function mousePressed() {
boolDoRefresh = true;
}9