As it is, the println in the second loop just starts back from 0 over and over and I cannot figure out why. If I remove everything but the println in the second loop it does move onto the draw function. Any possible reasons?
void setup() {
size(400, 500, P2D);
background(255,0,0);
frameRate(25);
noStroke();
frameNumber = 0;
robDrawing = loadImage("drawing.png");
robDrawing.loadPixels();
// Scan loaded image first and grab all pixels that aren't black.
// Grab their coords and their colours.
for (int x = 0; x < robDrawing.width; x++) {
for (int y = 0; y < robDrawing.height; y++) {
if (robDrawing.pixels[(y * robDrawing.width) + x] != color(0)) {
PVector newEndPoint = new PVector(x+ 50,y,0); // + 50 centres it
color 0 (black) represents an empty space, 255 (white) represents a starting point for a trail and 149 (grey) represents a trail.
I scan the image at the start to find any white pixels and make an array of this amount called crayonPoints. This is a PVector that holds the position of 'crayons' and they begin on these starting points. White pixels are isolated one offs.
The image is drawn in such a way that there is always a grey pixel line leading off from these initial white pixels that makes a path. It can change direction in any of the 8 directions around the current position but never junctions. My code searches the 8 pixels around the current location (crayonPoints) and finds the next touching grey pixel. Ruling out that it is one we were already came from, or the current location, it changes the coordinates of crayonPoints to this new location. Repeating this should follow a trail.
However, what I'm experiencing is the pixels moving diagonally around the black space and then bouncing off any grey pixels they encounter:
void searchPixelNeighbours(int i) {
// The trail in the image is pixels of grey of value 149.
// Search in a circle (neighbourhood) around the currentLocation
PVector nextPoint = new PVector(0,0);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
int xCoord = int(crayonPoints[i].x - 1 + x);
int yCoord = int(crayonPoints[i].y - 1 + y);
if (previousPoints[i].x != xCoord && previousPoints[i].y != yCoord) {
// Confirms this was not our last point. We don't want to backtrack
if (crayonPoints[i].x != xCoord && crayonPoints[i].y != yCoord) {
// Confirms this is not our current point.
if (red(lightMap.pixels[(yCoord * lightMapWidth) + xCoord]) == red(149)) {
// Next trail pixel found!
nextPoint = new PVector(xCoord, yCoord);
}
}
}
}
}
previousPoints[i] = crayonPoints[i].get();
crayonPoints[i].set(nextPoint);
}
I'm completely stumped. I've rearranged this several times with different results but nothing close to success.
Excuse the incorrect use of terms. I have a vague sense that these are related to my question so thought they would be an apt title.
Basically, I am unsure how to say:
a=x;
b=a;
Then change x without b changing. I want b to what a was when I said b=a and stay that way until I manually tell b what it is later.
If anyone's interested in the reason: I want to follow a trail of pixels on an image I've loaded. Given a certain coordinate on the image, I want to search all 8 pixels surrounding it and see if any match a labled colour, indicating it is the next step on the path.
I don't want the path to be confused by the pixel it last came from or itself, so I rule these out with if statements. To store the pixel we last travelled from, I have a PVector array of 'previousPoints'. I consistently update this by: