基本上我需要计算 XY 轴上的点和线(具有两个已知坐标)之间的距离 我得到了算法,但我不知道如何将它实现到 ObjC 中。
Illustration: I have a line that pass through two points A(100,300) and B(200,100)
m = (y-y1)/x(x-x1) = 300-100 / 100 - 200 = -2
so:
-2(x-x1) = (y-y1)
Replace x and y we have:
-2(100-x1) = 200 - y1 <=> -2x + y - 500 = 0 (Line equation that passes through above 2 points)
And the use another formula to calculate the distance:
ax + by + c / sqrt(a^2 + b^2)
Replace the x and y of point I want to calculate the distance to C(10,20)
-2*1000 + 2000 - 500 / sqrt (100 + 400) = distance I wanna find
就是这样,很简单,但是我如何在 ObjC 中执行此操作?
Okey 这基本上就是我所做的,不知道如何继续:
float coordinateXStart;
float coordinateYStart;
float coordinateXEnd;
float coordinateYEnd;
coordinateXStart = [[strokesArray objectAtIndex:strokeNo] startX];
coordinateYStart = [[strokesArray objectAtIndex:strokeNo] startY];
coordinateXEnd = [[strokesArray objectAtIndex:strokeNo] endX];
coordinateYEnd = [[strokesArray objectAtIndex:strokeNo] endY];
//let's rock on distance formula from point to line
float m = (coordinateYEnd-coordinateYStart)/(coordinateXEnd-coordinateXStart);
enter code here
Blockquote Okey so I had m....but how to automate the calculation of a,b and c of ax + by + c of line equation?
最佳答案
你说 -2*1000 + 2000 - 500/sqrt (100 + 400) = 我想找到的距离
,为什么不直接用代码写呢?
声明:
距离 = -2*1000 + 2000 - 500/sqrt (100 + 400)
是完全有效的 C(片段),因此是 Objective-C,只需确保您使用 C Math library第一
关于iphone - Objective C Iphone 开发距离公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4119398/