考慮您的起點(x1,y1)和終點(x2,y2)有一條線。爲了繪製一個箭頭帽(在objective-c中),我需要找到箭頭(x3,y3,x4,y4)給定箭頭角度(45度)的點,和箭頭的長度(h)。如何查找給定線的起點和終點的箭頭提示點
因此給定x1,y1,x2,y2,h,alpha什麼是x3,y3,x4,y4?
添加了解釋問題的圖像。
如果答案可以在Objective-C中(使用UIBezierpath和CGPoint),將會非常感激。
謝謝!
考慮您的起點(x1,y1)和終點(x2,y2)有一條線。爲了繪製一個箭頭帽(在objective-c中),我需要找到箭頭(x3,y3,x4,y4)給定箭頭角度(45度)的點,和箭頭的長度(h)。如何查找給定線的起點和終點的箭頭提示點
因此給定x1,y1,x2,y2,h,alpha什麼是x3,y3,x4,y4?
添加了解釋問題的圖像。
如果答案可以在Objective-C中(使用UIBezierpath和CGPoint),將會非常感激。
謝謝!
#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);
我希望這有助於:)
這是功課什麼的?爲什麼不直接使用上下文旋轉來旋轉它? – Metabble
@Metabble我認爲這是從代碼做這樣一個簡單的繪圖是非常合理的,而不是打擾加載圖像... – 2012-09-09 21:47:00
是的,這取決於你想要做什麼,但我只是好奇,爲什麼他們會煩。也許是因爲我很懶。 xD – Metabble