"Lock Pixels" function

edited July 2016 in Questions about Code
int size;
color [] ccc;
float rotAngle = 0;
color colour;
color rand;



void setup()   
{
  size(1200, 900);
  background(0);
  frameRate (50);
  smooth();
  noStroke();

  size = 20;
  ccc = new color[size*size];
  colour = color(random(255), random(255), random(255));
  rand = color(random(-50, 50), random(-50, 50), random(-50, 50));

  for (int i = 0; i < size*size; i++)
  {
    rand = rand+int(random(-10,10));
    ccc[i] = colour+rand;
  }
} 



void draw()
{
  if (mousePressed)
  {
    brush01();
  }
}



void brush01()
{

  int i;
  float brush_X = 0;
  float brush_Y = 0;

  for (i =1; i<size*size; i++)
  {

    if (mouseX != pmouseX || mouseY != pmouseY)
    {
      rotAngle = atan2(mouseY - pmouseY, mouseX - pmouseX);
    }

    pushMatrix();
    rectMode(CENTER);
    translate(mouseX, mouseY);
    rotate(rotAngle);
    translate(0-size/2, 0-size/2);
    fill(ccc[i]);
    println("Brush X = " + brush_X);
    println("Brush Y = " + brush_Y);
    rect(brush_X, brush_Y, 1, 1);
    popMatrix();

    brush_X ++;
    if (brush_X > size)
    {
      brush_X = 0;
      brush_Y ++;
    }
  }
}



void mouseReleased()
{
  colour = color(random(255), random(255), random(255));
  rand = color(random(-50, 50), random(-50, 50), random(-50, 50));

  for (int i = 0; i < size*size; i++)
  {
    rand = rand+int(random(-10,10));
    ccc[i] = colour+rand;
  }
}

Here is my little drawing sketch. Please, run and draw something on a canvas. I want to add something like "lock pixels" function, so if this function is on, the brush have to draw strokes only on colorful pixels of a canvas, and do not draw on black pixels. I tried to compare the pixels using "get" function, but the problem is that processing thinks my brush pixels coordinates (brush_X and brush_Y) is in the top left corner, even after translation an rotation. So, I can't compare brush pixels with canvas pixels. Any suggestions of how to make "lock pixels" function?

Tagged:

Answers

  • Answer ✓

    I am taking a wild guess....

    screenX() and screenY()?

    After googling I found this comment;

    screenX and screenY themselves do take into account the matrix (translate / rotate)....

    I hope this helps,

    Kf

  • I tried screenX and screenY, but this makes the same effect, processing still thinks my pixel coordinates in the top left corner. Maybe I use screenX and screenY in the wrong way, but I don't know how to do it right.

  • There are some sample using this in the forum. Try:

    https://forum.processing.org/two/search?Search=screenX

    I can't help any further atm...

    Kf

  • Answer ✓

    After the println statements in lines 62/62 add the following lines

    println("Screen X = " + screenX(brush_X, brush_Y));
    println("Screen Y = " + screenY(brush_X, brush_Y));
    

    This will print the screen pixel position for the brush position taking into account the translations and the rotation.

  • okay, now I see how screenX and screenY works, but there is another problem - somehow I have to hold a state of a canvas "before the stroke starts" to compare the pixels with my brush strokes, even when the stroke is in action. Hope that makes sense.

Sign In or Register to comment.