Graphics 确定直线是否与CGRect相交
确定直线是否与矩形相交的最有效方法是什么 我正在寻找类似的东西:Graphics 确定直线是否与CGRect相交,graphics,core-graphics,Graphics,Core Graphics,确定直线是否与矩形相交的最有效方法是什么 我正在寻找类似的东西: CGPoint startLine = CGPointMake(5.0f,5.0f); CGPoint endLine = CGPointMake(25.0f,25.0f); CGRect intersectingRect = CGRectMake(10.0f,10.0f,50.0f,50.0f); if (CGRectContainsLine(intersectingRect,startLine,endLine)) { //
CGPoint startLine = CGPointMake(5.0f,5.0f);
CGPoint endLine = CGPointMake(25.0f,25.0f);
CGRect intersectingRect = CGRectMake(10.0f,10.0f,50.0f,50.0f);
if (CGRectContainsLine(intersectingRect,startLine,endLine)) { //true
//line intersects rectangle
}
没有内置的东西,但这应该可以做到:
BOOL RectContainsLine(CGRect r, CGPoint lineStart, CGPoint lineEnd)
{
BOOL (^LineIntersectsLine)(CGPoint, CGPoint, CGPoint, CGPoint) = ^BOOL(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End)
{
CGFloat q =
//Distance between the lines' starting rows times line2's horizontal length
(line1Start.y - line2Start.y) * (line2End.x - line2Start.x)
//Distance between the lines' starting columns times line2's vertical length
- (line1Start.x - line2Start.x) * (line2End.y - line2Start.y);
CGFloat d =
//Line 1's horizontal length times line 2's vertical length
(line1End.x - line1Start.x) * (line2End.y - line2Start.y)
//Line 1's vertical length times line 2's horizontal length
- (line1End.y - line1Start.y) * (line2End.x - line2Start.x);
if( d == 0 )
return NO;
CGFloat r = q / d;
q =
//Distance between the lines' starting rows times line 1's horizontal length
(line1Start.y - line2Start.y) * (line1End.x - line1Start.x)
//Distance between the lines' starting columns times line 1's vertical length
- (line1Start.x - line2Start.x) * (line1End.y - line1Start.y);
CGFloat s = q / d;
if( r < 0 || r > 1 || s < 0 || s > 1 )
return NO;
return YES;
};
/*Test whether the line intersects any of:
*- the bottom edge of the rectangle
*- the right edge of the rectangle
*- the top edge of the rectangle
*- the left edge of the rectangle
*- the interior of the rectangle (both points inside)
*/
return (LineIntersectsLine(lineStart, lineEnd, CGPointMake(r.origin.x, r.origin.y), CGPointMake(r.origin.x + r.size.width, r.origin.y)) ||
LineIntersectsLine(lineStart, lineEnd, CGPointMake(r.origin.x + r.size.width, r.origin.y), CGPointMake(r.origin.x + r.size.width, r.origin.y + r.size.height)) ||
LineIntersectsLine(lineStart, lineEnd, CGPointMake(r.origin.x + r.size.width, r.origin.y + r.size.height), CGPointMake(r.origin.x, r.origin.y + r.size.height)) ||
LineIntersectsLine(lineStart, lineEnd, CGPointMake(r.origin.x, r.origin.y + r.size.height), CGPointMake(r.origin.x, r.origin.y)) ||
(CGRectContainsPoint(r, lineStart) && CGRectContainsPoint(r, lineEnd)));
}
BOOL RectContainsLine(CGRect r、CGPoint lineStart、CGPoint lineEnd)
{
BOOL(^LineIntersectsLine)(CGPoint,CGPoint,CGPoint,CGPoint)=^BOOL(CGPoint line1Start,CGPoint line1End,CGPoint line2Start,CGPoint line2End)
{
CGQ=
//行的起始行之间的距离乘以line2的水平长度
(line1Start.y-line2Start.y)*(line2End.x-line2Start.x)
//行的起始列之间的距离乘以line2的垂直长度
-(line1Start.x-line2Start.x)*(line2End.y-line2Start.y);
CGD=
//第1行的水平长度乘以第2行的垂直长度
(line1End.x-line1Start.x)*(line2End.y-line2Start.y)
//第1行的垂直长度乘以第2行的水平长度
-(line1End.y-line1Start.y)*(line2End.x-line2Start.x);
如果(d==0)
返回否;
cgr=q/d;
q=
//行的起始行之间的距离乘以行1的水平长度
(line1Start.y-line2Start.y)*(line1End.x-line1Start.x)
//行的起始列之间的距离乘以行1的垂直长度
-(line1Start.x-line2Start.x)*(line1End.y-line1Start.y);
cgs=q/d;
如果(r<0 | | r>1 | | s<0 | | s>1)
返回否;
返回YES;
};
/*测试该线是否与以下任一点相交:
*-矩形的底边
*-矩形的右边缘
*-矩形的上边缘
*-矩形的左边缘
*-矩形的内部(两个点都在内部)
*/
返回(LineIntersectsLine(lineStart、lineEnd、CGPointMake(r.origin.x、r.origin.y)、CGPointMake(r.origin.x+r.size.width、r.origin.y))||
LineIntersectsLine(线起点、线终点、CGPointMake(r.origin.x+r.size.width、r.origin.y)、CGPointMake(r.origin.x+r.size.width、r.origin.y+r.size.height))||
LineIntersectsLine(线起点、线终点、CGPointMake(r.origin.x+r.size.width、r.origin.y+r.size.height)、CGPointMake(r.origin.x、r.origin.y+r.size.height))||
直线相交直线(直线起点、直线终点、CGPointMake(r.origin.x、r.origin.y+r.size.height)、CGPointMake(r.origin.x、r.origin.y))||
(CGRectContainsPoint(r,lineStart)和&CGRectContainsPoint(r,lineEnd));
}
从这个问题中简单地引出:对于任何使用objective-c的人:
- (BOOL)rectContainsLine:(CGRect)rect startPoint:(CGPoint)lineStart endPoint:(CGPoint)lineEnd {
BOOL (^LineIntersectsLine)(CGPoint, CGPoint, CGPoint, CGPoint) = ^BOOL(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
CGFloat q =
//Distance between the lines' starting rows times line2's horizontal length
(line1Start.y - line2Start.y) * (line2End.x - line2Start.x)
//Distance between the lines' starting columns times line2's vertical length
- (line1Start.x - line2Start.x) * (line2End.y - line2Start.y);
CGFloat d =
//Line 1's horizontal length times line 2's vertical length
(line1End.x - line1Start.x) * (line2End.y - line2Start.y)
//Line 1's vertical length times line 2's horizontal length
- (line1End.y - line1Start.y) * (line2End.x - line2Start.x);
if( d == 0 )
return NO;
CGFloat r = q / d;
q =
//Distance between the lines' starting rows times line 1's horizontal length
(line1Start.y - line2Start.y) * (line1End.x - line1Start.x)
//Distance between the lines' starting columns times line 1's vertical length
- (line1Start.x - line2Start.x) * (line1End.y - line1Start.y);
CGFloat s = q / d;
if( r < 0 || r > 1 || s < 0 || s > 1 )
return NO;
return YES;
};
/*Test whether the line intersects any of:
*- the bottom edge of the rectangle
*- the right edge of the rectangle
*- the top edge of the rectangle
*- the left edge of the rectangle
*- the interior of the rectangle (both points inside)
*/
return (LineIntersectsLine(lineStart, lineEnd, CGPointMake(rect.origin.x, rect.origin.y), CGPointMake(rect.origin.x + rect.size.width, rect.origin.y)) ||
LineIntersectsLine(lineStart, lineEnd, CGPointMake(rect.origin.x + rect.size.width, rect.origin.y), CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height)) ||
LineIntersectsLine(lineStart, lineEnd, CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height), CGPointMake(rect.origin.x, rect.origin.y + rect.size.height)) ||
LineIntersectsLine(lineStart, lineEnd, CGPointMake(rect.origin.x, rect.origin.y + rect.size.height), CGPointMake(rect.origin.x, rect.origin.y)) ||
(CGRectContainsPoint(rect, lineStart) && CGRectContainsPoint(rect, lineEnd)));
}
-(BOOL)rectContainsLine:(CGRect)rect startPoint:(CGPoint)lineStart endPoint:(CGPoint)lineEnd{
BOOL(^LineIntersectsLine)(CGPoint,CGPoint,CGPoint,CGPoint)=^BOOL(CGPoint line1Start,CGPoint line1End,CGPoint line2Start,CGPoint line2End){
CGQ=
//行的起始行之间的距离乘以line2的水平长度
(line1Start.y-line2Start.y)*(line2End.x-line2Start.x)
//行的起始列之间的距离乘以line2的垂直长度
-(line1Start.x-line2Start.x)*(line2End.y-line2Start.y);
CGD=
//第1行的水平长度乘以第2行的垂直长度
(line1End.x-line1Start.x)*(line2End.y-line2Start.y)
//第1行的垂直长度乘以第2行的水平长度
-(line1End.y-line1Start.y)*(line2End.x-line2Start.x);
如果(d==0)
返回否;
cgr=q/d;
q=
//行的起始行之间的距离乘以行1的水平长度
(line1Start.y-line2Start.y)*(line1End.x-line1Start.x)
//行的起始列之间的距离乘以行1的垂直长度
-(line1Start.x-line2Start.x)*(line1End.y-line1Start.y);
cgs=q/d;
如果(r<0 | | r>1 | | s<0 | | s>1)
返回否;
返回YES;
};
/*测试该线是否与以下任一点相交:
*-矩形的底边
*-矩形的右边缘
*-矩形的上边缘
*-矩形的左边缘
*-矩形的内部(两个点都在内部)
*/
返回(LineIntersectsLine(lineStart、lineEnd、CGPointMake(rect.origin.x、rect.origin.y)、CGPointMake(rect.origin.x+rect.size.width、rect.origin.y))||
LineIntersectsLine(lineStart、lineEnd、CGPointMake(rect.origin.x+rect.size.width、rect.origin.y)、CGPointMake(rect.origin.x+rect.size.width、rect.origin.y+rect.size.height))||
LineIntersectsLine(lineStart、lineEnd、CGPointMake(rect.origin.x+rect.size.width、rect.origin.y+rect.size.height)、CGPointMake(rect.origin.x、rect.origin.y+rect.size.height))||
LineIntersectsLine(lineStart、lineEnd、CGPointMake(rect.origin.x、rect.origin.y+rect.size.height)、CGPointMake(rect.origin.x、rect.origin.y))||
(CGRectContainsPoint(rect,lineStart)和&CGRectContainsPoint(rect,lineEnd));
}
Swift版本:
extension CGRect {
private func lineIntersectsLine(_ line1Start: CGPoint, _ line1End: CGPoint, _ line2Start: CGPoint, _ line2End: CGPoint) -> Bool {
// Distance between the lines' starting rows times line2's horizontal length
var q = (line1Start.y - line2Start.y) * (line2End.x - line2Start.x)
//Distance between the lines' starting columns times line2's vertical length
- (line1Start.x - line2Start.x) * (line2End.y - line2Start.y)
let d =
//Line 1's horizontal length times line 2's vertical length
(line1End.x - line1Start.x) * (line2End.y - line2Start.y)
//Line 1's vertical length times line 2's horizontal length
- (line1End.y - line1Start.y) * (line2End.x - line2Start.x)
if d == 0 {
return false
}
let r = q / d
q =
//Distance between the lines' starting rows times line 1's horizontal length
(line1Start.y - line2Start.y) * (line1End.x - line1Start.x)
//Distance between the lines' starting columns times line 1's vertical length
- (line1Start.x - line2Start.x) * (line1End.y - line1Start.y);
let s = q / d
if r < 0 || r > 1 || s < 0 || s > 1 {
return false
}
return true
}
func instersectsLine(start lineStart: CGPoint, end lineEnd: CGPoint) -> Bool
{
/*Test whether the line intersects any of:
*- the bottom edge of the rectangle
*- the right edge of the rectangle
*- the top edge of the rectangle
*- the left edge of the rectangle
*- the interior of the rectangle (both points inside)
*/
return (lineIntersectsLine(lineStart, lineEnd, CGPoint(x:self.origin.x, y: self.origin.y), CGPoint(x: self.origin.x + self.size.width, y: self.origin.y)) ||
lineIntersectsLine(lineStart, lineEnd, CGPoint(x: self.origin.x + self.size.width, y: self.origin.y), CGPoint(x: self.origin.x + self.size.width, y: self.origin.y + self.size.height)) ||
lineIntersectsLine(lineStart, lineEnd, CGPoint(x: self.origin.x + self.size.width, y: self.origin.y + self.size.height), CGPoint(x: self.origin.x, y: self.origin.y + self.size.height)) ||
lineIntersectsLine(lineStart, lineEnd, CGPoint(x: self.origin.x, y: self.origin.y + self.size.height), CGPoint(x: self.origin.x, y: self.origin.y)) ||
(contains(lineStart) && contains(lineEnd)))
}
}
扩展CGRect{
专用函数直线相交直线(uLine1开始:CGPoint,Line1结束:CGPoint,Line2开始:CGPoint,Line2结束:CGPoint)->Bool{
//行的起始行之间的距离乘以line2的水平长度
变量q=(line1Start.y-line2Start.y)*(line2End.x-line2Start.x)
//行的起始列之间的距离乘以line2的垂直长度
-(line1Start.x-line2Start.x)*(line2End.y-line2Start.y)
让d=
//第1行的水平长度乘以第2行的垂直长度
(line1End.x-line1Start.x)*(line2End.y-line2Start.y)
//第1行的垂直长度乘以第2行的水平长度
-(line1End.y-line1Start.y)*(line2End.x-line2Start.x)
如果d==0{
返回错误
}
设r=q/d
q=
//行的起始行之间的距离乘以l