首页 > 解决方案 > Swift - 删除相交的线

问题描述

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

在此处输入图像描述

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

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
}

标签: swiftuiviewdraw

解决方案


在一般情况下,您需要求解一个二次方程来计算圆相交的点。圆的方程是 (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)
    }

推荐阅读