【发布时间】:2019-11-20 00:58:18
【问题描述】:
完整的错误信息:
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 = segment(v1x: 1, v1y: 3, v2x: 3, v2y: 1)
这些值应该是可以互换的,有人知道这里发生了什么吗?
我在网上 swift 操场上尝试过同样的事情,但同样的问题仍然存在,同时交换 seg1 的值有效。我要做的就是将一个段数组传递给我的辅助函数。
【问题讨论】:
-
刚刚发布,
-
是不是因为第一次比较中seg1和seg2是一样的?
标签: ios swift xcode swift-playground