Pythonアルゴリズム(ここでは、境界ボックスに収まるように線分を拡張する)をiPhoneアプリケーションに変換しようとしていますが、startPoint.x>次にendPointxの場合にのみ機能します。
ここで何を変更しますか?理解できません..
左上から右下に線を引くとうまくいきます!しかし、右上から左下に線を引くと失敗します。ですから、それは一方向にしか機能しません。右から左への場合、いくつかの変数を変更する必要があると思います。
MINは(0,0)で、MAXはデバイスによって異なりますが、iPhone Retinaの場合(300,568)
- (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;
}