gpt4 book ai didi

ios - 创建与 CGPathRef 垂直的线

转载 作者:行者123 更新时间:2023-12-01 19:00:34 24 4
gpt4 key购买 nike

我正在使用 SKShapeNodes 通过在用户触摸屏幕时更新路径属性来动态绘制线条。这条线完成后,我想在路径的末端附加一条新的固定长度垂直线。

我已经研究了 CGAffineTransform 以根据路径的终点旋转新线,但到目前为止还没有任何运气。任何提示或见解将不胜感激。

我当前的一些引用代码如下所示:

        - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];

//add a line to the new coordinates the user moved to
CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//create local copy of path
CGPathRef myPath_ = pathToDraw;

CGPoint myPoint = CGPathGetCurrentPoint(pathToDraw);

//create rectangle to append to end of line
CGRect newRect = CGRectMake(myPoint.x, myPoint.y, 25, 3);
CGPathRef newPath = CGPathCreateWithRect(newRect, NULL);

//add new line to the end of the path
CGPathAddPath(newPath, NULL, myPath_);

//set shape node path to drawn path
lineNode.path = myPath_;
}

最佳答案

要获得垂直线,您可以将向量指向最后一条线段的方向,然后交换 x 和 y,并反转其中一个。像这样的东西:

CGPoint v = { currentPoint.x - lastPoint.x, currentPoint.y - lastPoint.y };
CGPoint perp;
if (v.x == 0)
{
perp.x = -v.y;
perp.y = v.x;
}
else
{
perp.x = v.y;
perp.y = -v.x;
}

现在您可以在 perp 的方向上画一条线。 ,从当前点开始,如下所示:
CGPathMoveToPoint (somePath, currentPoint);
CGPathAddLineToPoint (somePath, NULL, currentPoint.x + perp.x * length, currentPoint.y + perp.y * length);

在哪里 length是您要绘制的线段的长度。

并且不要忘记设置 lastPointcurrentPoint所以下次它是正确的:
lastPoint = currentPoint;

关于ios - 创建与 CGPathRef 垂直的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177401/

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