2012-09-09 3 views
3

시작점 (x1, y1)과 종점 (x2, y2)이있는 행이 있다고 가정하십시오.주어진 원점과 끝점을 찾는 방법

화살표 (목표 -c에서)에 화살표 뚜껑을 그리려면 화살의 각 (45도)이 주어진 경우 화살표의 점 (x3, y3, x4, y4)을 찾아야합니다. 및 화살촉 (h)의 길이.

그래서 주어진 x1, y1, x2, y2, h, 알파는 무엇입니까? x3, y3, x4, y4?

질문을 설명하는 이미지가 추가되었습니다.

대답이 객관적인 -c (UIBezierpath 및 CGPoint 사용) 일 수 있으면 크게 감사하겠습니다.

감사합니다.

+0

이 숙제 또는 뭔가입니까? 컨텍스트 로테이션을 사용하여 화살표를 똑바로 위로 돌리지 않는 이유는 무엇입니까? – Metabble

+0

@ Metabble 나는 코드에서 그런 단순한 그림을 그리는 것과 이미지를 로딩하는 것을 귀찮게하지 않는 것이 합리적이라고 생각한다 ... –

+0

네가하고 싶은 일에 따라 다르지만, 왜 그런지 궁금했다. 귀찮음. 어쩌면 게으름이기 때문일 수도 있습니다. xD – Metabble

답변

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

arrow drawing 난이 도움이 되었으면 좋겠 :)

+0

+1. 도움이 될 것입니다. : p – Metabble

+0

@ Metabble 감사합니다 : D –

+0

당신을 진심으로 환영합니다. 너무 많은 시간을 소비 한 것을 얻을 수 있다는 것은 놀랍습니다. xD – Metabble

관련 문제