【问题标题】:Swift - Remove intersecting linesSwift - 删除相交的线
【发布时间】:2020-02-28 13:52:13
【问题描述】:

帮助!如何删除相交的线,让我只有外部形状?

我需要对此进行哪些更改才能删除相交线?

let width = 300
func circle(withRadius radius: CGFloat) -> UIView {
    let path = UIBezierPath(arcCenter: CGPoint(x: width * (1/2), y: width * (1/3)), radius: 80, startAngle: .zero, endAngle: .pi * 2, clockwise: true)
    path.append(UIBezierPath(arcCenter: CGPoint(x: width * (1/3), y: width * (2/3)), radius: 80, startAngle: .zero, endAngle: .pi * 2, clockwise: true))
    path.append(UIBezierPath(arcCenter: CGPoint(x: width * (2/3), y: width * (2/3)), radius: 80, startAngle: .zero, endAngle: .pi * 2, clockwise: true))

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.black.cgColor
    shapeLayer.lineWidth = 3.0

    backView.layer.addSublayer(shapeLayer)
    return backView
}

【问题讨论】:

    标签: swift uiview draw


    【解决方案1】:

    一般情况下,您需要求解一个二次方程来计算圆相交的点。圆的方程是 (x-x0)^2 + (y-y0)^2 = r^2,其中 (x0,y0) 是圆心。但是我发现了一些类似的点来确定宽度和半径的值。

        let width: CGFloat = 300
        let radius: CGFloat = 80
    
        let point1 = CGPoint(x: width * (1/2), y: width * (1/3))
        let point2 = CGPoint(x: width * (1/3), y: width * (2/3))
        let point3 = CGPoint(x: width * (2/3), y: width * (2/3))
    
        circle(center: point1, withRadius: radius, startAngle: .pi - .pi/10, endAngle: .pi * 2 + .pi/10)
        circle(center: point2, withRadius: radius, startAngle: .pi / 3.5, endAngle: .pi / 3.5 + .pi * 1.11)
        circle(center: point3, withRadius: radius, startAngle: .pi * 1.6, endAngle: .pi * 1.72 + .pi)
    
        func circle(center: CGPoint, withRadius radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat) {
            let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
    
            let shapeLayer = CAShapeLayer()
            shapeLayer.path = path.cgPath
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.strokeColor = UIColor.black.cgColor
            shapeLayer.lineWidth = 3.0
    
            backView.layer.addSublayer(shapeLayer)
        }
    

    【讨论】:

    • 你能解释一下你是如何计算 startAngle 和 endAngle 的吗?我不知道如何使用您发布的方程式。谢谢
    【解决方案2】:

    一种方法是计算圆切割的 3 个外部点的位置。 (图中红色圈出的点)。

    然后你计算这些点的中心(我们称这个点为中心)。之后,您要计算一个外部点与中心之间的距离(我们将此距离称为半径)。 然后,您可以使用我们之前找到的半径在中心创建一个椭圆。这个椭圆必须与背景颜色相同,并且它的 zPosition 大于其他 3 个圆。 完成,相交的线现在消失了:)

    (如果您不知道如何创建椭圆,这是一个可以帮助您的脚本:)

    let circle = SKShapeNode(circleOfRadius: radius)
    circle.position = center
    circle.zPosition = 100 //Need to be greater than the others zPosition
    self.addChild(circle)
    

    【讨论】:

    • 那么你提到的所有计算是如何进行的?没有这些信息,这个答案不是很有帮助
    • 我回答了这个问题,但是我可以添加代码来计算这个。
    猜你喜欢
    • 2015-05-11
    • 2017-02-09
    • 2016-02-22
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多