Search code examples
swiftuiviewdraw

Swift - Remove intersecting lines


Help! How do I remove the intersecting lines so I will have only the outside shape?

enter image description here

What do I need to change in this to remove the intersecting lines?

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
}

Solution

  • In general case you need to solve a quadratic equation to calculate point where circles are intersected. The equation of circle is (x-x0)^2 + (y-y0)^2 = r^2, where (x0,y0) center of a circle. But I found some similar points for determine values of width and radius.

        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)
        }