如何找到一个箭头提示点给定的起点和终点的线 [英] How to find an arrow tip points given origin and end point of a line

查看:364
本文介绍了如何找到一个箭头提示点给定的起点和终点的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑你有一条有起点(x1,y1)和终点(x2,y2)的线。

Consider you have a line with start point (x1,y1) and end point (x2,y2).

为了画一个箭头帽(在目标c中),我需要找到给定箭头​​的角度(45度)的箭头(x3,y3,x4,y4)的点和箭头尖端的长度(h)。

In order to draw an arrow cap to the line (in objective-c), I need to find the points of the arrow (x3,y3,x4,y4) given the angle of the arrow (45 degrees), and the length of the arrow tips (h).

添加了x1,y1,x2,y2,h,alpha whats x3,y3,x4,y4?

So given x1,y1,x2,y2,h,alpha whats x3,y3,x4,y4?

如果答案可以在objective-c中(使用UIBezierpath和CGPoint),这将是非常赞赏。

If the answer can be in objective-c (using UIBezierpath and CGPoint) it will be much appreciated.

谢谢!

推荐答案

#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);

我希望这有助于:)

这篇关于如何找到一个箭头提示点给定的起点和终点的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆