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

标签 ios swift xcode swift-playground

完整错误消息:

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/

相关文章:

ios - 已收到 KVO 消息但未处理问题 - SWIFT

iphone - 如何计算触摸点和 UIImageView 中心之间的斜率

ios - 使用 UIViewController 的 View 作为 UITextField 的 inputView 时抛出“UIViewControllerHierarchyInconsistency”

上传到 App Store 时 Xcode 出错 : "No suitable application records were found"

ios - 自引用快速关闭

iphone - kCFCalendarUnitQuarter 没有为每季度触发通知的通知设置下一个触发日期

ios - 如何将 map 缩放到我的注释地标 - Swift

UIPageViewController 的 Ios 下拉刷新策略

ios - 如何在 Firebase 中使用嵌套数据 queryOrderedbyChild 和 queryEqualtoValue?

xcode - 为什么内联汇编不能与 Xcode 中的 ARC 一起使用?