Calculate pixels between two points on a image

独自空忆成欢 提交于 2019-12-10 10:23:17

问题


Turns out I am not explaining what I want to do very well so I am going to re-write the whole question again and add graphics to assist in the explanation.

I am designing an app for android/iPhone. I have designed one algorithm for it already, but am stuck on the next one. What I am aiming to do is place two horizontal lines on an image (any image, just a picture taken by the iPhone/android) and then calculate what pixel the lines lye on, to then calculate the number of pixels between them. I.e. Take this image:

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

Then place two horizontal lines anywhere on the image, like so:

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

What I want to calculate is the value of y, or how many vertical pixels are between the two lines. To do this I must know what pixel the two lines lye on. Assuming that the horizontal lines are both only 1 pixel in height what would I use to work out what pixel in the image the line lies on. I.e. what is the value of the y-intercept(y=mx+c), or c, on each of the horizontal lines. To explain what I mean further, lets assume that an image is a graph. Each pixel equals a value of 1, so for an image with a resolution of 1920x2560 the maximum value of the y-axis would be 1920 and the maximum of the x-axis would be 2560. How would I design an algorithm to calculate what the y-intercept of both lines are?


回答1:


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 as y1 = k1x + n1 and y2 = k2x + n2, then (they're horizontal, k1 and k2 are 0) the distance between them is n2 - n1.

EDIT: ok, after you edited your question it makes a bit more sense now. But still: since you (or user) is adding the lines, your code always knows where they lie. Their end coordinates would be:

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

Since they're both horizontal they ofcourse never cross.

You should just keep a reference to y1 and y2 (from the line-placing code) in an appropriate space. Since your question is for Android and iOS the answer is: in that part of code that would correspond to model in MVC.



来源:https://stackoverflow.com/questions/9598867/calculate-pixels-between-two-points-on-a-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!