Advertisement
Talar97

[GK] Processing - prosta grafika

May 19th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. int Y_AXIS = 1;
  2. int X_AXIS = 2;
  3. ArrayList particles;
  4.  
  5. void setup(){
  6.   size(1000, 750, P2D);
  7.   particles = new ArrayList();
  8.   color skyTop = color(165, 182, 209);
  9.   color skyBottom = color(178, 132, 71);
  10.   color sun = color(255, 238, 0);
  11.   color sea = color(1, 34, 86);
  12.   color sand = color(25, 24, 0);
  13.  
  14.   setGradient(0, 0, 1000, 360, skyTop, skyBottom, 1);
  15.  
  16.   noStroke();
  17.   fill(sun);
  18.  
  19.   ellipse(width/2, 360, 100, 100);
  20.   filter(BLUR, 4);
  21.  
  22.   fill(sea);
  23.   rect(0, 360, 1000, 500);
  24.  
  25.   fill(sand);
  26.   noStroke();
  27.   quad(0, 620, 0, 750, 1000, 750, 1000, 720);
  28.  
  29.   particles.add(new Particle(100, 100, 50, color(245), 12));
  30.   particles.add(new Particle(916, 64, 50, color(245), 12));
  31.   particles.add(new Particle(920, 288, 50, color(245), 12));
  32.   particles.add(new Particle(640, 155, 50, color(245), 12));
  33.   particles.add(new Particle(618, 84, 50, color(245), 12));
  34.   particles.add(new Particle(482, 64, 50, color(245), 12));
  35.   particles.add(new Particle(681, 324, 50, color(245), 12));
  36.   particles.add(new Particle(530, 218, 50, color(245), 12));
  37.   particles.add(new Particle(397, 157, 50, color(245), 12));
  38.   particles.add(new Particle(191, 231, 50, color(245), 12));
  39.   particles.add(new Particle(81, 318, 50, color(245), 12));
  40.   particles.add(new Particle(65, 155, 50, color(245), 12));
  41.   particles.add(new Particle(235, 81, 50, color(245), 12));
  42.   particles.add(new Particle(382, 245, 50, color(245), 12));
  43.   particles.add(new Particle(869, 159, 50, color(245), 12));
  44.  
  45.   for(int i = 0; i < particles.size(); i++)
  46.   {
  47.     Particle p = (Particle) particles.get(i);
  48.     if(p.alive)
  49.     {
  50.       p.drawParticle();
  51.       p.reproduce();
  52.     }
  53.     else
  54.       particles.remove(i);
  55.   }
  56. }
  57.  
  58. void draw(){}
  59.  
  60. void mousePressed(){
  61.   particles.add(new Particle(mouseX, mouseY, 50, color(245), 12));
  62.   println(mouseX + "x" + mouseY);
  63. }
  64.  
  65. void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {
  66.   noFill();
  67.  
  68.   if (axis == Y_AXIS) {  // Top to bottom gradient
  69.     for (int i = y; i <= y+h; i++) {
  70.       float inter = map(i, y, y+h, 0, 1);
  71.       color c = lerpColor(c1, c2, inter);
  72.       stroke(c);
  73.       line(x, i, x+w, i);
  74.     }
  75.   }  
  76.   else if (axis == X_AXIS) {  // Left to right gradient
  77.     for (int i = x; i <= x+w; i++) {
  78.       float inter = map(i, x, x+w, 0, 1);
  79.       color c = lerpColor(c1, c2, inter);
  80.       stroke(c);
  81.       line(i, y, i, y+h);
  82.     }
  83.   }
  84. }
  85.  
  86.  
  87.  
  88. class Particle
  89. {
  90.   PVector position;
  91.   float oppacity;
  92.   float w;
  93.   color clr;
  94.   boolean alive;
  95.  
  96.   protected Particle(float x, float y, float wth, color c, float o)
  97.   {
  98.     position = new PVector(x, y);
  99.     w = wth;
  100.     clr = c;
  101.     oppacity = o;
  102.     alive = true;
  103.   }
  104.  
  105.   public void reproduce()
  106.   {
  107.     if(w > 1)
  108.     {
  109.       for(int i = 0; i < 2; i++)
  110.       {
  111.         float newX = position.x + random(-w, w);
  112.         float newY = position.y + random(-w/2, w/4);
  113.         float r = random(10);
  114.         float newW = w - r;
  115.         if(newW < 1)
  116.           newW = 1;
  117.         particles.add(new Particle(newX, newY, newW, clr, oppacity));
  118.         alive = false;
  119.       }
  120.     }
  121.   }
  122.  
  123.   public void drawParticle()
  124.   {
  125.     fill(clr, oppacity);
  126.     ellipse(position.x, position.y, w, w);
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement