gpt4 book ai didi

ios - Xcode Playground : "error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

转载 作者:行者123 更新时间:2023-11-29 05:18:21 51 4
gpt4 key购买 nike

完整错误消息:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

在这行代码上var x = isThereInterection(segments:lines)

这里有更多背景信息:

class segment {

var v1x:Double
var v1y:Double
var v2x:Double
var v2y:Double
var slope: Double
var b: Double

init(v1x: Double, v1y: Double, v2x: Double, v2y: Double) {
self.v1x = v1x
self.v1y = v1y
self.v2x = v2x
self.v2y = v2y
self.slope = (v2y - v1y) / (v2x - v1x) //finding the slope for the line segment
self.b = v1y - self.slope * v1x //finding the b value for the line function in y = mx + b
}

}

func isThereInterection(segments: [segment]) -> Bool{
var isThere:Bool?

for seg1 in segments {
for seg2 in segments {
let leftSide = seg1.slope - seg2.slope
let rightSide = seg2.b - seg1.b
let xValue = rightSide/leftSide //this is the x value where the lines would intersect
if((seg1.v1y...seg1.v2x).contains(xValue) && (seg2.v1x...seg2.v2x).contains(xValue)){ //here we check if the x value of intersection is in range of both line segments
return true //if it is in range of both line segments, we have an intersection so return true
}

}

}
return false
}

var seg1 = segment(v1x: 3, v1y: 1, v2x: 1, v2y: 3)
var seg2 = segment(v1x: 1, v1y: 1, v2x: 3, v2y: 3)
var lines = [seg1, seg2]
var x = isThereInterection(segments: lines)
print(x)

但这很奇怪,因为当我将 seg1 更改为var seg1 = 段(v1x: 1, v1y: 3, v2x: 3, v2y: 1)这些值应该可以互换,有人知道这里发生了什么吗?

我在在线 Swift Playground 上尝试过同样的操作,在交换 seg1 的值时仍然存在同样的问题。我想做的就是将一组段传递到我的辅助函数中。

最佳答案

这是你的问题

for seg1 in segments {
for seg2 in segments {
// At the very first iteration of the loops, seg1 and seg2 are the same
let leftSide = seg1.slope - seg2.slope
let rightSide = seg2.b - seg1.b
let xValue = rightSide/leftSide //this is the x value where the lines would intersect
if((seg1.v1y...seg1.v2x).contains(xValue) && (seg2.v1x...seg2.v2x).contains(xValue)){ //here we check if the x value of intersection is in range of both line segments
return true //if it is in range of both line segments, we have an intersection so return true
}

}

}

如果您的输入段数组始终有两个元素,则不需要循环。如果不是这种情况,您必须进行计算,其中 seg1 != seg2

关于ios - Xcode Playground : "error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58945094/

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