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

Cannot figure out why alpha is acting strange here!

$
0
0
Hi all.  I am having what appears to be a strange bug, but I'm sure I'm doing something stupid.

I have a "HeatPoint" class which is basically a point that is assigned a color value.  From that color value I am creating a fading circle around that point if a pixel is within a certain distances from the point.  Essentially I want to draw a heatmap, which I'm still wrapping my head around.  To fade, I am just mapping my min/max distance onto an alpha value and drawing a square 8 pixel width/height) as my 'pixels' for a fading circle.  The problem is that when there are multiple HeatPoint objects, the alpha channels for the fading circle are rendering as black.
Attached is a screenshot.  The HeatPoint is being drawn as a 200x200 ellipse and you can see the fading circle behind it.  There are 2 in the screenshot.


I would actually love to do this by coloring the pixel[] array, but alpha seems to get completely ignored.  I have searched the forums with nothing that solves my issue.

I appreciate any help you have to offer.  Code below:

  1. int numHeatPoints = 2;
  2. HeatPoint[] heatPoints = new HeatPoint[numHeatPoints];

  3. void setup(){
  4.   size(1280,800);
  5.   smooth();
  6.   for(int i=0;i<numHeatPoints;i++){
  7.     heatPoints[i] = new HeatPoint(random(0,width),random(0,height));  
  8.   }
  9.   noStroke();
  10.   rectMode(CENTER);
  11. }

  12. float[] colorPixel(float x,float y){
  13.   float[] c = new float[4];
  14.   for(int i=0;i<numHeatPoints;i++){
  15.     if(distanceBetween(x,y,heatPoints[i].x,heatPoints[i].y) < 200){
  16.       float alph = map(distanceBetween(x,y,heatPoints[i].x,heatPoints[i].y),0,200,175,0);
  17.       c[0] = heatPoints[i].rgba[0];
  18.       c[1] = heatPoints[i].rgba[1];
  19.       c[2] = heatPoints[i].rgba[2];
  20.       c[3] = alph;
  21.     }
  22.   }
  23.   return c;
  24. }
  25. float distanceBetween(float x1, float y1, float x2, float y2){
  26.   float a = abs(x1-x2);
  27.   float b = abs(y1-y2);
  28.   return  sqrt((a*a)+(b*b));
  29. }

  30. void draw(){
  31.   background(0);
  32.   //loadPixels();
  33.   for(int i=0;i<numHeatPoints;i++){
  34.     heatPoints[i].drawHeatPoint();
  35.   }
  36.   for(int x=0;x<width;x++){
  37.     if(x%4==0){
  38.       for(int y=0;y<height;y++){
  39.         if(y%4==0){
  40.           //pixels[y*width+x] = colorPixel(x,y);
  41.           float[] c = colorPixel(x,y);
  42.           fill(c[0],c[1],c[2],c[3]);
  43.           rect(x,y,8,8);
  44.         }
  45.       }
  46.     }
  47.   }
  48.   //updatePixels();
  49. }



  50. class HeatPoint{
  51.   float x,y;
  52.   float val;
  53.   float[] rgba = new float[4];
  54.   
  55.   HeatPoint(float myX, float myY){
  56.     x = myX;
  57.     y = myY;
  58.     updateHeatPointVal(random(0.0,1.0));
  59.     println(val);
  60.   }
  61.   
  62.   void updateHeatPointVal(float v){
  63.     val = v;
  64.     rgba[0] = map(val,0,1,0,255);
  65.     rgba[1] = map(val,0,1,255,0);
  66.     rgba[2] = 0;
  67.     rgba[3] = 255;
  68.   }
  69.   
  70.   void drawHeatPoint(){
  71.     fill(rgba[0],rgba[1],rgba[2],rgba[3]);
  72.     //fill(red(c),green(c),0,alpha(c));
  73.     ellipse(x,y,255,255);
  74.   }
  75. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles