Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<Bucket> buckets;
- ArrayList<BucketCluster> bucketClusters;
- ArrayList<Bucket> selectedBuckets;
- ArrayList<Path> paths;
- int xOffsetInteractive;
- int yOffsetInteractive;
- float speed = 0.01f;
- long seed = 0;
- int noiseShelf = 20;
- int noiseShelfFilter = 5;
- int amountOfSampledPoints = 200;
- int level = 0;
- interface IEntity
- {
- public int getX();
- public int getY();
- }
- class Path{
- int x1;
- int y1;
- int x2;
- int y2;
- public Path(int x1, int y1, int x2, int y2)
- {
- this.x1 = x1;
- this.x2 = x2;
- this.y1 = y1;
- this.y2 = y2;
- }
- }
- class BucketCluster implements IEntity
- {
- public ArrayList<Bucket> buckets;
- public int centerX;
- public int centerY;
- public int getX(){
- return centerX;
- }
- public int getY(){
- return centerY;
- }
- public BucketCluster(ArrayList<Bucket> buckets){
- this.buckets = buckets;
- int sum_x = 0;
- int sum_y = 0;
- for (int i = 0; i < buckets.size(); i++){
- sum_x += buckets.get(i).x;
- sum_y += buckets.get(i).y;
- }
- centerX = round(sum_x / buckets.size());
- centerY = round(sum_y / buckets.size());
- }
- }
- class Bucket implements Comparable<Bucket>, IEntity{
- public int x;
- public int y;
- public float value;
- public int step;
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- public Bucket(int x, int y, float value, int step)
- {
- this.x = x;
- this.y = y;
- this.value = value;
- this.step = step;
- }
- int compareTo(Bucket other){
- if (value > other.value)
- return 1;
- else if (value < other.value)
- return -1;
- return 0;
- }
- public boolean containsPoint(int x, int y){
- boolean output = x - this.x < step
- && x - this.x >= 0
- && y - this.y < step
- && y - this.y >= 0;
- return output;
- }
- }
- void setup(){
- size(1000,1000);
- textSize(16);
- }
- void draw(){
- perlinMap();
- fill(0, 255,0);
- text("Band Width: " + noiseShelf, 10, 20);
- text("Selected Band: " + noiseShelfFilter, 10, 40);
- text("Seed: " + seed, 10, 60);
- fill(0,0,255);
- if (level >= 2)
- {
- text("Bucket Count: " + buckets.size(), 10, 80);
- }
- if (level >= 3)
- {
- text("Bucket Cluster Count: " + bucketClusters.size(), 10, 100);
- }
- }
- void keyPressed(){
- if (key == CODED)
- {
- if (keyCode == UP){
- noiseShelf++;
- }
- else if (keyCode == DOWN){
- noiseShelf--;
- }
- else if (keyCode == LEFT){
- noiseShelfFilter--;
- }
- else if (keyCode == RIGHT){
- noiseShelfFilter++;
- }
- if (keyCode == ALT){
- seed = (long)random(1200);
- }
- }
- if (key == 'w'){
- yOffsetInteractive++;
- }
- else if(key == 's')
- {
- yOffsetInteractive--;
- }
- if (key == 'a'){
- xOffsetInteractive--;
- }
- else if(key == 'd')
- {
- xOffsetInteractive++;
- }
- if (key =='e')
- {
- level++;
- }
- else if (key == 'q'){
- level--;
- }
- }
- void perlinMap(){
- noiseSeed(seed);
- baseNoise(0.01, noiseShelf);
- if (level >= 1)
- filterPaths(noiseShelfFilter);
- //smooth step
- //smoothNoise(width*height);
- if (level >= 2)
- {
- genGrid(20);
- java.util.Collections.sort(buckets);
- selectBuckets(amountOfSampledPoints);
- drawBuckets();
- }
- //noiseSeed(seed);
- //baseNoise(0.01, noiseShelf);
- ArrayList<IEntity> toDraw = new ArrayList<IEntity>();
- if (level >= 3)
- {
- mergeBuckets(3);
- toDraw.addAll(bucketClusters);
- }
- if (level >= 4)
- {
- stroke(color(0,255,0));
- drawConnections(toDraw, 200.0);
- }
- }
- void drawConnections(ArrayList<IEntity> entities, float dist)
- {
- for (int i = 0; i < entities.size(); i++){
- IEntity e1 = entities.get(i);
- for (int j = 0; j < entities.size(); j++){
- if (e1 == entities.get(j))
- continue;
- IEntity e2= entities.get(j);
- if(distance(e1, e2) > dist)
- continue;
- line(e1.getX(), e1.getY(), e2.getX(), e2.getY());
- }
- //todo: Test what happens if you use 2-nearest neighbours instead of all in range.
- }
- }
- float distance(IEntity e1, IEntity e2)
- {
- float x1 = e1.getX() - e2.getX();
- float y1 = e1.getY() - e2.getY();
- return sqrt(x1*x1 + y1*y1);
- }
- void drawBuckets()
- {
- for (int i = 0; i < selectedBuckets.size(); i++){
- Bucket b = selectedBuckets.get(i);
- for (int y = b.y; y < b.y + b.step; y++)
- {
- for (int x = b.x; x < b.x+b.step; x++)
- {
- pixels[x + y *width] = color(255,0,0);
- }
- }
- }
- updatePixels();
- }
- void mergeBuckets(int mergeRadius)
- {
- ArrayList<Bucket> open = new ArrayList<Bucket>();
- open.addAll(selectedBuckets);
- bucketClusters = new ArrayList<BucketCluster>();
- for (int i = 0; i < open.size(); i++){
- Bucket b = open.get(i);
- open.remove(i);
- ArrayList<Bucket> neighbours = getNeighbours(b.x, b.y, open, mergeRadius, b.step);
- open.removeAll(neighbours);
- neighbours.add(b);
- bucketClusters.add(new BucketCluster(neighbours));
- }
- for (int i = 0; i< bucketClusters.size(); i++){
- BucketCluster bc = bucketClusters.get(i);
- rect(bc.centerX, bc.centerY, 15, 15);
- }
- }
- ArrayList<Bucket> getNeighbours(int x, int y, ArrayList<Bucket> buckets, int checkRadius, int stepSize){
- ArrayList<Bucket> output = new ArrayList<Bucket>();
- for (int x_o = -checkRadius*stepSize; x_o <= checkRadius*stepSize; x_o+= stepSize)
- {
- for (int y_o = -checkRadius*stepSize; y_o <= checkRadius*stepSize; y_o+=stepSize)
- {
- for (int i = 0; i < buckets.size(); i++)
- {
- if (buckets.get(i).containsPoint(x_o + x, y_o + y))
- output.add(buckets.get(i));
- }
- }
- }
- return output;
- }
- void selectBuckets(int amount){
- selectedBuckets = new ArrayList<Bucket>();
- int trg = min(buckets.size(), amount);
- for (int i = 0; i < trg; i++)
- {
- Bucket b = buckets.get(i);
- selectedBuckets.add(b);
- }
- }
- void genGrid(int step){
- buckets = new ArrayList<Bucket>();
- loadPixels();
- for (int y = 0; y < height - step; y+=step){
- for (int x = 0; x < width - step; x+=step)
- {
- float sum = 0;
- float count = 0.0;
- for (int y0 = y; y0 < y + step; y0++)
- {
- for (int x0 = x; x0 < x + step; x0++){
- if (!inBounds(x0, y0))
- continue;
- sum += red(pixels[x0 + y0*width]) / 255.0f;
- count++;
- //pixels[x0 + y0*width] = color(y*4,x*4,0);
- }
- }
- sum /= count;
- buckets.add(
- new Bucket(x, y, sum, step));
- }
- }
- updatePixels();
- }
- void filterPaths(int shelfMul){
- if (shelfMul <= 0) return;
- for(int i = 0; i < width; i++){
- for (int j = 0; j < height; j++){
- if (red(get(i,j)) == noiseShelf*shelfMul)
- set(i,j, color(0,0,255/shelfMul));
- else
- set(i,j, color(255,255,255));
- }
- }
- }
- void baseNoise(float noiseScale, int noiseShelf){
- for(int i = 0; i < width; i++){
- for (int j = 0; j < height; j++){
- float nv = noise(xOffsetInteractive *speed + i * noiseScale,yOffsetInteractive * speed + j * noiseScale);
- nv *= 255.0;
- int cv = (int)nv;
- cv /= noiseShelf;
- cv *= noiseShelf;
- color col = color(cv, cv, cv);
- set(i,j, col);
- }
- }
- }
- boolean inBounds(int x, int y){
- return x >= 0 && x < width && y >= 0 && y < height;
- }
- void smoothNoise(int numPixels){
- int off = 2;
- loadPixels();
- int[] newPixels = new int[pixels.length];
- for(int y = 0; y < height; y++){
- for (int x = 0; x < width; x++){
- color col = get(x,y);
- float sum = 0.0;
- float count = 0.0;
- for (int j1 = y - off; j1 <= y + off; j1++){
- for (int i1 = x - off; i1 <= x + off; i1++){
- if (i1 == x && j1 == y || !inBounds(i1,j1))
- {
- continue;
- }
- color neighbourCol = pixels[i1 + width*j1];
- sum += red(neighbourCol);
- count += 1.0;
- }
- }
- sum /= 255.0;
- float fv = sum / count;
- int iv = (int)(fv * 255);
- iv = max(iv, 0);
- iv = min(iv, 255);
- newPixels[x+width*y] = color(iv,iv,iv);
- }
- }
- arrayCopy(newPixels, pixels);
- updatePixels();
- }
Advertisement
Add Comment
Please, Sign In to add comment