gpt4 book ai didi

ios - Swift:从 sprite 工具包 SKShapeNode 图纸中检测交点

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:51 24 4
gpt4 key购买 nike

我正在使用 Sprite Kit 绘图。我想检测用户的绘图何时相交。
enter image description here

我尝试遵循代码,但它不起作用。看起来 sprite kit 并没有保存所有的点:

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
touch = touches.anyObject() as UITouch!
for drawingPoint in drawingPoints{
if(touch.locationInNode(self) == drawingPoint){println(true)}
}
drawingPoints.append(touch.locationInNode(self))
}

最佳答案

这是一个在 swift 中用于分段交集的函数。

func linesIntersect(line1 : CGPointInterval, line2 : CGPointInterval) -> (intersects: Bool, point : CGPoint?)
{
//The algorithm is taken from http://www.amazon.com/dp/0672323699/?tag=stackoverfl08-20
// http://portal.aauj.edu/portal_resources/downloads/programming/windows_game_programming_guru.pdf

let p0_x = line1.start.x
let p1_x = line1.end.x
let p2_x = line2.start.x
let p3_x = line2.end.x

let p0_y = line1.start.y
let p1_y = line1.end.y
let p2_y = line2.start.y
let p3_y = line2.end.y

let s1_x = p1_x - p0_x
let s1_y = p1_y - p0_y
let s2_x = p3_x - p2_x
let s2_y = p3_y - p2_y

let s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y)
let t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y)

if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
// Collision detected
let finalX = p0_x + (t * s1_x)
let finalY = p0_y + (t * s1_y)
return (true, CGPointMake(finalX, finalY))
}

return (false, nil) // No collision
}

关于ios - Swift:从 sprite 工具包 SKShapeNode 图纸中检测交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894859/

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