# Accessing pixel values just above a line

**URL:** https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145
**Category:** Coding Questions
**Created:** [August 2, 2019, 7:14pm UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145 "2019-08-02T19:14:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![eric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eric/32/5988_2.png) [@eric](https://discourse.processing.org/u/eric)
#### Post date: [August 2, 2019, 7:14pm UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/1 "2019-08-02T19:14:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [August 2, 2019, 9:28pm UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/2 "2019-08-02T21:28:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [August 3, 2019, 12:47am UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/3 "2019-08-03T00:47:22Z")

</div>

? not sure it helps, but play with  
[https://processing.org/reference/lerp\_.html](https://processing.org/reference/lerp_.html)

---

<div class="post-metadata">

### Author: ![eric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eric/32/5988_2.png) [@eric](https://discourse.processing.org/u/eric)
#### Post date: [August 3, 2019, 3:09am UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/4 "2019-08-03T03:09:39Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![eric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eric/32/5988_2.png) [@eric](https://discourse.processing.org/u/eric)
#### Post date: [August 3, 2019, 3:10am UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/5 "2019-08-03T03:10:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [August 3, 2019, 10:14am UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/6 "2019-08-03T10:14:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [August 4, 2019, 8:53am UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/7 "2019-08-04T08:53:54Z")

</div>

[https://www.khanacademy.org/computing/computer-programming/programming-games-visualizations#programming-transformations](https://www.khanacademy.org/computing/computer-programming/programming-games-visualizations#programming-transformations)

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:

[![](https://img.youtube.com/vi/4bIsntTiKfM/maxresdefault.jpg "Coding Math: Episode 32 - Line Intersections Part I") ](https://www.youtube.com/watch?v=4bIsntTiKfM)

coded example processing:

[https://www.openprocessing.org/sketch/741525](https://www.openprocessing.org/sketch/741525)

point intersection:

> **[Collision Detection](http://www.jeffreythompson.org/collision-detection/line-circle.php)**
>
> An online book about collision detection using Processing.

coded example p5:

> **[Khan Academy](https://www.khanacademy.org/computer-programming/spin-off-of-random-bezier/6287670616948736)**
>
> Learn for free about math, art, computer programming, economics, physics, chemistry, biology, medicine, finance, history, and more. Khan Academy is a nonprofit with the mission of providing a free, world-class education for anyone, anywhere.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [August 8, 2019, 9:25pm UTC](https://discourse.processing.org/t/accessing-pixel-values-just-above-a-line/13145/8 "2019-08-08T21:25:12Z")

</div>

> [@eric](#):
>
> I’m trying to see if the pixel is black or white, and then move on to the next pixel down the line.

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.
