在线绘制箭头

时间:2010-03-26 09:08:33

标签: iphone algorithm math graphics

我有这段代码:

CGPoint arrowMiddle = CGPointMake((arrowOne.x + arrowTo.x)/2, (arrowOne.y + arrowTo.y)/2);

CGPoint arrowLeft = CGPointMake(arrowMiddle.x-40, arrowMiddle.y);
CGPoint arrowRight = CGPointMake(arrowMiddle.x, arrowMiddle.y + 40);

[arrowPath addLineToScreenPoint:arrowLeft];
[arrowPath addLineToScreenPoint:arrowMiddle];
[arrowPath addLineToScreenPoint:arrowRight];
[[mapContents overlay] addSublayer:arrowPath];
[arrowPath release];

使用此输出:

http://img517.yfrog.com/img517/7690/schermafbeelding2010032.png

我需要添加什么才能使左右两侧的线相同+ 30°。

如果有人在一条线上绘制箭头的算法,请求给它。它是什么编程语言并不重要......

由于

2 个答案:

答案 0 :(得分:3)

这是你做的。首先,取线的矢量并将其除以其长度进行标准化 - 这将为您提供一个长度为1的向量,指向线的方向。接下来,将它乘以您需要的长度。将其旋转120°和-120°以制作箭头。最后,将它偏移到你想要的坐标。以下是代码中的样子:

// calculate the position of the arrow
CGPoint arrowMiddle;
arrowMiddle.x = (arrowOne.x + arrowTo.x) / 2;
arrowMiddle.y = (arrowOne.y + arrowTo.y) / 2;

// create a line vector
CGPoint v;
v.x = arrowTo.x - arrowOne.x;
v.y = arrowTo.y - arrowOne.y;

// normalize it and multiply by needed length
CGFloat length = sqrt(v.x * v.x + v.y * v.y);
v.x = 40 * (v.x / length);
v.y = 40 * (v.y / length);

// turn it by 120° and offset to position
CGPoint arrowLeft = CGPointApplyAffineTransform(v, CGAffineTransformMakeRotation(3.14 * 2 / 3));
arrowLeft.x = arrowLeft.x + arrowMiddle.x;
arrowLeft.y = arrowLeft.y + arrowMiddle.y;

// turn it by -120° and offset to position
CGPoint arrowRight = CGPointApplyAffineTransform(v, CGAffineTransformMakeRotation(-3.14 * 2 / 3));
arrowRight.x = arrowRight.x + arrowMiddle.x;
arrowRight.y = arrowRight.y + arrowMiddle.y;

答案 1 :(得分:1)

感谢您的回复! 与此同时,我发现了一个解决方案。

就像这样:


double slopy , cosy , siny;
                double Par = 10.0;  //length of Arrow (>)
                slopy = atan2( ( arrowOne.y - arrowTo.y ),
                              ( arrowOne.x - arrowTo.x ) );
                cosy = cos( slopy );
                siny = sin( slopy ); //need math.h for these functions

            CGPoint arrowMiddle = CGPointMake((arrowOne.x + arrowTo.x)/2, (arrowOne.y + arrowTo.y)/2);
            [arrowPath addLineToScreenPoint:arrowMiddle];

            CGPoint arrowLeft = CGPointMake( arrowMiddle.x + round( - Par * cosy - ( Par / 2.0 * siny ) ), arrowMiddle.y + round( - Par * siny + ( Par / 2.0 * cosy ) ) );
            [arrowPath addLineToScreenPoint:arrowLeft];

            CGPoint arrowRight = CGPointMake( arrowMiddle.x + round( - Par * cosy + ( Par / 2.0 * siny ) ),arrowMiddle.y - round( Par / 2.0 * cosy + Par * siny ) );
            [arrowPath addLineToScreenPoint:arrowRight];

            [arrowPath addLineToScreenPoint:arrowMiddle];
            [[mapContents overlay] addSublayer:arrowPath];
            [arrowPath release];

这里唯一的问题是我画它就像是一个RMPath(route-me框架),当你放大/缩小时箭头会变得更大/更小。

但是感谢您的回复,我将调查哪个代码的效果最好。