Accessing pixel values just above a line

I’m trying to see if the pixel is black or white, and then move on to the next pixel down the line.
I’ve gotten flat lines down, I just keep track of the endpoints of the line, and add 2 to not count the line’s color.
The problem is now I want diagonal lines. I think 45 degree lines shouldn’t be too hard, but how would I go about different angles? I’m thinking there’s an easier way to go about this, but my coding experience goes back about 2 weeks ago…
Any input is much appreciated, thanks.

1 Like

you could try the get method, if you know what color the line is, get() returns the pixel color of a pixel and you could then just take the yth -1 value. Alternatively a line intersection check would provide the same result however you would be constrained to resolution of your line intersections. Another solution might be to rotate the canvas matrix before sampling your pixel, apply the straight line method to identify the pixel you want and then rotate the matrix back. You could then use trig to calculate the position of the rotated point,

x = p1 + dx * cos(theta), y = p1 + dy * sin(theta);

just a few possible ways you could try.


? not sure it helps, but play with


Thank you, yeah I’ve been using get() and got it working, but it’s very messy. I’ll likely try rotating the matrix to clean it up. Any places to start to learn about that matrix stuff?

Also, I’m looking to rotate the photo, but not rotate the matrix (I think) because otherwise my lines that I use to count get rotated and messed up. Is there any way to simply rotate the image?

Wow, weird word for that. I might experiment with it, it could possibly clean up my current code as well.

Khan academy is a great place to start, also check out Coding train on youtube.

1 Like

That should have all you need to get started with transformations.

I think you need to establish whether or not you know the points that construct the lines. If you do, it would be as simple as calling y-1 on each point. Then if you wanted to find each point on that line you would create a for loop which took either the x/y distance traveled, and use trig to find the corresponding point.

Line intersection might work as well. and point line intersection would also work

line intersections:

coded example processing:

point intersection:

coded example p5:


Just a guess, but maybe you are trying to do this because it is a way to do something else? Maybe if you shared your larger goal, people could describe different ways of trying to solve it.

For example:

  • tracing
  • edge detection
  • pathfinding
  • selecting a closed shape
  • etc.

…all might be implemented by trying to step across pixels, but there are usually other ways of solving the problem.