# Thread: Stumped by a problem that would have been easy for me in high school.

1. ## Stumped by a problem that would have been easy for me in high school.

Yep. My skills are far deteriorated, so much so that the problem I'm about to describe I'd have cringed at needed help with way back when. Anyways.....

Suppose I have a rectangle, $w wide and$h tall. Consider the diagonals going from top-left to bottom-right and from bottom-left to top-right. I will be cycling through each pixel, and I want to figure out for each one what is the distance from that pixel to the nearer diagonal.

I've tried to work it out on paper but ended up getting frustrated twice.

Can anyone help?

THX
KeeganT

2. ## Re: Stumped by a problem that would have been easy for me in high school.

I've noticed you have marked this as solved. Would you like to post your solution? Someone who has a similar problem might find it useful...

3. ## Oops, didn't mean to mark it Solved.

Yeah, I'm still looking for a solution.

KeeganT

4. ## Re: Stumped by a problem that would have been easy for me in high school.

FYI: I have removed the "Solved" tag.

-Dan

5. ## Re: Stumped by a problem that would have been easy for me in high school.

Hello, KeeganT!

Suppose I have a rectangle, $w$ wide and $h$ tall.
Consider the diagonals going from top-left to bottom-right and from bottom-left to top-right.
I will be cycling through each pixel, and I want to figure out for each one
what is the distance from that pixel to the nearer diagonal.

I placed the rectangle on a coordinate system.
Code:
   (-w/2,h/2)       |        (w/2,h/2)
B * - - - - - + - - - - - * A
|   .       |       .   |
|       .   |   .       |
- - + - - - - - + - - - - - + - -
|       .   |   .       |
|   .       |       .   |
C * - - - - - + - - - - - * D
(-w/2,-h/2)      |       (w/2,-h/2)
|
Diagonal $AC$ has the equation: $\; y = \tfrac{h}{w}x \quad\Rightarrow\quad hx - wy \:=\:0$

Diagonal $BD$ has the equation: $\;y = \text{-}\tfrac{h}{w}x \quad\Rightarrow\quad hx + wy \:=\:0$

If the point $P(x_1,y_1)$ is in Quadrant 1 or 3, it is closer to $AC.$
The distance is: $\;d \:=\:\dfrac{|hx_1 - wy_1|}{\sqrt{h^2+w^2}}$

If the point $P(x_1,y_1)$ is in Quadrant 2 or 4, it is closer to $BD.$
The distance is: $\:d \:=\:\dfrac{|hx_1+wy_1|}{\sqrt{h^2+w^2}}$

I hope this helps.