iphone - Objective C Iphone 开发距离公式

标签 iphone objective-c algorithm ios4

基本上我需要计算 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/

相关文章:

iphone - 我应该注意什么来调试这个 Core-Data/NSFetchedResultsController 错误?

objective-c - 使用 PDFDocument initWithData 获取 'unrecognized selector sent to instance'?

ios - 无法使用并找到 setTexture :resize SpriteKit

algorithm - 如何在从左到右和从上到下排序的二维数组中搜索数字?

algorithm - 给定 N 个坐标为整数的点,找出平行线的数量

iphone - 对象界面布局?

iphone - 带阴影的分组 uitableview

iphone - UIView 中只有白色填充颜色是透明的

ios - 嵌入式 ViewContainer 中的手势识别器不会被击中

c# - 为什么在 MergeSort 中使用 InsertionSort 而不是 Merge 平均速度更快?