ios 如何找到一个箭头提示点给定的起点和终点的一条线

aiazj4mn  于 2023-05-19  发布在  iOS
关注(0)|答案(2)|浏览(111)

假设你有一条线,起点(x1,y1)和终点(x2,y2)。
为了画一个指向直线的箭头帽(在目标c中),我需要找到箭头的点(x3,y3,x4,y 4),给定箭头的Angular (45度)和箭头的长度(h)。
给定x1,y1,x2,y2,h,alpha,x3,y3,x4,y 4是什么?
添加了解释问题的图片。
如果答案可以在objective-c中(使用UIBezierpath和CGPoint),将非常感谢。
谢谢!x1c 0d1x

7tofc5zh

7tofc5zh1#

#import <math.h>
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>

float phi = atan2(y2 - y1, x2 - x1); // substitute x1, x2, y1, y2 as needed
float tip1angle = phi - M_PI / 4; // -45°
float tip2angle = phi + M_PI / 4; // +45°

float x3 = x2 - h * cos(tip1angle); // substitute h here and for the following 3 places
float x4 = x2 - h * cos(tip2angle);
float y3 = y2 -  h * sin(tip1angle);
float y4 = y2 -  h * sin(tip2angle);

CGPoint arrowStartPoint = CGPointMake(x1, y1);
CGPoint arrowEndPoint = CGPointMake(x2, y2);
CGPoint arrowTip1EndPoint = CGPointMake(x3, y3);
CGPoint arrowTip2EndPoint = CGPointMake(x4, y4);

CGContextRef ctx = UIGraphicsGetCurrentContext(); // assuming an UIView subclass
[[UIColor redColor] set];
CGContextMoveToPoint(ctx, arrowStartPoint.x, arrowStartPoint.y);
CGContextAddLineToPoint(ctx, arrowEndPoint.x, arrowEndPoint.y);
CGContextAddLineToPoint(ctx, arrowTip1EndPoint.x, arrowTip1EndPoint.y);
CGContextMoveToPoint(ctx, arrowEndPoint.x, arrowEndPoint.y);
CGContextAddLineToPoint(ctx, arrowTip2EndPoint.x, arrowTip2EndPoint.y);

我希望这对你有帮助:)

4c8rllxm

4c8rllxm2#

这是我的Java实现我的需要。startend是Point对象。

double lineAngle = Math.atan2(end.y-start.y, start.x-end.x);
    double ang1 = lineAngle-Math.PI/6;
    double ang2 = lineAngle+Math.PI/6;
    
    int tipLen = 30;
    Point tip1 = new Point(end.x+(int)(tipLen*Math.cos(ang1)), end.y-(int)(tipLen*Math.sin(ang1)));
    Point tip2 = new Point(end.x+(int)(tipLen*Math.cos(ang2)), end.y-(int)(tipLen*Math.sin(ang2)));
    
    g.drawLine(end.x,end.y,tip1.x,tip1.y);
    g.drawLine(end.x,end.y,tip2.x,tip2.y);

计算尖端的Angular 并使用Angular 找到它们的位置。

相关问题