Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Cannot figure out why alpha is acting strange here!

$
0
0
Implemented first solution, after all.
  1. int numHeatPoints = 5;
  2. HeatPoint[] heatPoints = new HeatPoint[numHeatPoints];
  3.  
  4. void setup(){
  5.   size(1280,800);
  6.   smooth();
  7.   for (int i=0;i<numHeatPoints;i++){
  8.     heatPoints[i] = new HeatPoint(random(0,width),random(0,height)); 
  9.   }
  10.   noStroke();
  11.   rectMode(CENTER);
  12.   imageMode(CENTER);
  13.   noLoop();
  14. }
  15.  
  16. void draw(){
  17.   background(0);
  18.   for (int i=0;i<numHeatPoints;i++){
  19.     heatPoints[i].drawHeatPoint();
  20.   }
  21. }
  22.  
  23.  
  24. class HeatPoint{
  25.   final int RADIUS = 200;
  26.   float x,y;
  27.   float val;
  28.   float[] rgba = new float[4];
  29.   PGraphics img = createGraphics(RADIUS * 2, RADIUS * 2, JAVA2D);
  30.   
  31.   HeatPoint(float myX, float myY){
  32.     x = myX;
  33.     y = myY;
  34.     updateHeatPointVal(random(1));
  35.     draw();
  36.   }
  37.  
  38.   void updateHeatPointVal(float v){
  39.     val = v;
  40.     rgba[0] = map(val,0,1,0,255);
  41.     rgba[1] = map(val,0,1,255,0);
  42.     rgba[2] = 0;
  43.     rgba[3] = 255;
  44.     println(val);
  45.   }
  46.  
  47.   float getAlpha(int px, int py)
  48.   {
  49.     float d = dist(px, py, RADIUS, RADIUS);
  50.     if (d < RADIUS)
  51.     {
  52.       return map(d, 0, RADIUS, 175, 0);
  53.     }
  54.     return -1;
  55.   }
  56.  
  57.   void draw()
  58.   {
  59.     img.beginDraw();
  60.     img.noStroke();
  61.     img.rectMode(CENTER);
  62.     for (int px = 0; px < img.width; px += 4)
  63.     {
  64.       for (int py = 0; py < img.width; py += 4)
  65.       {
  66.         float alpha = getAlpha(px, py);
  67.         if (alpha >= 0)
  68.         {
  69.           img.fill(rgba[0], rgba[1], rgba[2], alpha);
  70.           img.rect(px, py, 8, 8);
  71.         }
  72.       }
  73.     }
  74.     img.fill(rgba[0], rgba[1], rgba[2]);
  75.     img.ellipse(RADIUS, RADIUS, 255, 255);
  76.     img.endDraw();
  77.   }
  78.  
  79.   void drawHeatPoint(){
  80.     image(img, x, y);
  81.   }
  82. }
Quick code...

Viewing all articles
Browse latest Browse all 1768

Trending Articles