根据两点计算延伸点
2019-01-01 本文已影响0人
Leon1024
// 从两个点计算出指定延伸长度外的射线点,isNextPoint为YES则为向前点,否则为当前点的向后点
- (CGPoint)pointWithExtendLength:(CGFloat)length lastPoint:(CGPoint)pointA currentPoint:(CGPoint)pointB isNextPoint:(BOOL)isNext {
CGFloat relativeX = pointB.x - pointA.x;
CGFloat relativeY = pointB.y - pointA.y;
CGFloat relativeZ = hypot(relativeX, relativeY); // 直角三角形求斜边
CGFloat x = relativeX / relativeZ * length;
CGFloat y = relativeY / relativeZ * length;
if (isNext) {
return CGPointMake(pointB.x + x, pointB.y + y);
}
return CGPointMake(pointB.x - x, pointB.y - y);
}