3

始点 (x1,y1) と終点 (x2,y2) を持つ線があるとします。

線に矢印キャップを描画するには (objective-c)、矢印の角度 (45 度) と長さを指定して、矢印のポイント (x3、y3、x4、y4) を見つける必要があります。矢印の先端 (h)。

与えられた x1,y1,x2,y2,h,alpha は x3,y3,x4,y4 でしょうか?

質問を説明する画像を追加しました。

答えが目的の c (UIBezierpath と CGPoint を使用) にある場合は、非常に高く評価されます。

ありがとう!矢印の描画

4

1 に答える 1

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

これが役立つことを願っています:)

于 2012-09-09T21:46:12.237 に答える