gpt4 book ai didi

iphone - 在直线上画箭头

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:17 26 4
gpt4 key购买 nike

我有这个代码:

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°。

如果有人有在线上画箭头的算法,请提供。它是什么编程语言并不重要......

谢谢

最佳答案

这就是你要做的。首先,获取线的向量并通过将其除以它的长度来对其进行归一化——这将为您提供一个指向线方向的长度为 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;

关于iphone - 在直线上画箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2522064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com