我想蟒蛇算法(在这里:Extending a line segment to fit into a bounding box)转换到我的iPhone应用程序..但它只能当我startPoint.x>然后endPointx如何在一个矩形拟合线
是什么在这里改变? 我不明白它..
如果我画一条从左上角到右下角的线,它的工作原理!但是,如果我从右上角画一条线,则会失败。因此,它只能在一个方向。我想我必须要改变一些变量如果从右到左
MIN为(0,0)和MAX取决于设备,但对于iPhone的Retina(300568)
- (NSMutableArray *) extendAlgorithm:(CGPoint)start withEnd:(CGPoint)end withBorderMin:(CGPoint)min andBorderMax:(CGPoint)max {
int x1 = (int) start.x; int y1 = (int) start.y;
int x2 = (int) end.x; int y2 = (int) end.y;
int xmin = (int) min.x; int ymin = (int) min.y;
int xmax = (int) max.x; int ymax = (int) max.y;
if(y1 == y2) {
return [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithDouble:xmin],
[NSNumber numberWithDouble:y1],
[NSNumber numberWithDouble:xmax],
[NSNumber numberWithDouble:y1],
nil];
}
if(x1 == x2) {
return [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithDouble:x1],
[NSNumber numberWithDouble:ymin],
[NSNumber numberWithDouble:x1],
[NSNumber numberWithDouble:ymax],
nil];
}
double y_for_xmin = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
double y_for_xmax = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
double x_for_ymin = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
double x_for_ymax = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
if(ymin <= y_for_xmin <= ymax) {
if(xmin <= x_for_ymax <= xmax) {
return [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithDouble:xmin],
[NSNumber numberWithDouble:y_for_xmin],
[NSNumber numberWithDouble:x_for_ymax],
[NSNumber numberWithDouble:ymax],
nil];
}
if(xmin <= x_for_ymin <= xmax) {
return [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithDouble:xmin],
[NSNumber numberWithDouble:y_for_xmin],
[NSNumber numberWithDouble:x_for_ymin],
[NSNumber numberWithDouble:ymin],
nil];
}
}
if(ymin <= y_for_xmax <= ymax) {
if(xmin <= x_for_ymin <= xmax) {
return [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithDouble:x_for_ymin],
[NSNumber numberWithDouble:ymin],
[NSNumber numberWithDouble:xmax],
[NSNumber numberWithDouble:y_for_xmax],
nil];
}
if(xmin <= x_for_ymax <= xmax) {
return [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithDouble:x_for_ymax],
[NSNumber numberWithDouble:ymax],
[NSNumber numberWithDouble:xmax],
[NSNumber numberWithDouble:y_for_xmax],
nil];
}
}
return nil;
}
会不会是你很难用调试器到达那里,或者在检查这些值时是否有任何问题? – 2013-03-26 17:38:23
那么让我给你一个提示,你没有足够的条件来涵盖所有的情况。 – 2013-03-26 18:05:56