Calculate pixels between two image points

It turns out I'm not explaining what I want to do very well, so I'm going to rewrite the whole question and add graphics to help explain it.

I am developing an Android and iPhone app. I've already developed one algorithm for it, but I'm stuck on the next one. What I intend to do is place two horizontal lines in the image (any image, just an image taken by the iPhone / android) and then calculate which pixel the lines are located on, and then calculate the number of pixels between them. That is, Take this image:

http://i.stack.imgur.com/41vS1.png

Then place two horizontal lines anywhere in the image, for example:

http://i.stack.imgur.com/ne4tV.png

What I want to calculate is the y value or how many vertical pixels are between the two lines. To do this, I need to know which pixel the two lines are on. Assuming the horizontal lines are only 1 pixel high, what would I use to determine which pixel in the image is on the line. That is, what is the value of the y-intercept (y = mx + c) or c, on each horizontal line. To explain what I mean next, let's assume the image is a graph. Each pixel equals a value of 1, so for an image with a resolution of 1920x2560, the maximum y-axis will be 1920 and the maximum x-axis will be 2560. How would I design an algorithm to calculate that the y-intercept of both lines?

+3


source to share


1 answer


Distance between two points (Pythagora):

dx = x1 - x2;
dy = y1 - y2;

dist = sqrt (dx*dx + dy*dy);

      

Distance between two horizontal lines:

d = y1 - y2;

      

If your lines are defined like y1 = k1x + n1

and y2 = k2x + n2

, then (they are horizontal k1

and k2

equal to 0) the distance between them n2 - n1

.



EDIT: OK, after you've edited your question, it's a little better now. But still: since you (or the user) add lines, your code always knows where they are. Their final coordinates:

line1: {(0, y1) :( picture.width, y1)} line2: {(0, y2) :( picture.width, y2)} distance: | y2-y1 |

Since they are both horizontal, they certainly never intersect.

You should just store the reference to y1 and y2 (from the line placement code) in the appropriate space. Since your question is about Android and iOS, the answer is: in the part of the code that matches model

in MVC.

+3


source







All Articles