我有2類:Objective-C中一條直線和一個點之間的距離?
// point : (x, y)
@interface ICPoint : NSObject {
NSInteger x;
NSInteger y;
}
// line : y= ax + b
@interface ICLine : NSObject {
float a;
float b;
}
而且這種方法:
// return the distance between a line and a point
-(NSInteger) distance:(ICPoint *)point {
return fabs(-a*point.x +point.y - b)/sqrt(a*a + 1);
}
的公式似乎是正確的(根據維基百科),但結果是錯誤的,爲什麼?
謝謝!
編輯:
ICLine *line1 = [[ICLine alloc] initWithA:0.60 andB:11.25];
ICLine *line2 = [[ICLine alloc] initWithA:0.61 andB:-2.85];
ICPoint *point = [[ICPoint alloc] initWithX:41 andY:22];
[line1 distance:point]; // give 11, it's ok.
[line2 distance:point]; // give 24, it should be ~0...
他們怎麼錯了? – Mark 2010-06-01 10:36:00
我剛剛添加了一個例子。 – pimpampoum 2010-06-01 10:42:09
你能否提供公式的來源? – swegi 2010-06-01 10:45:35