title: Determining if UIView in array of UIView's overlap with each other at all, and then adjust position
body: <p>I have a loop below that is supposed to detect if a view in an array of <code>UIView</code>'s overlap with each other at all. If they do, then adjust their <code>center</code> value.</p>
  <p>Each view is 25x25.</p>
  <pre><code>func drawCircles() {
          for i in 0..&lt;circles.count{
              circles[i].center = getRandomPoint()
              for j in 0..&lt;circles.count{
                  if(i != j) {
                      let comparingCentre = circles[j].center
                      let dist = distance(comparingCentre, circles[i].center)
                      if dist &lt;= 25 {
                          
                          var newCenter = circles[i].center
                          var centersVector = CGVector(dx: newCenter.x - comparingCentre.x, dy: newCenter.y - comparingCentre.y)
                       
                          //Circle width is 25
                          centersVector.dx *= 26 / dist
                          centersVector.dy *= 26 / dist
                          newCenter.x = comparingCentre.x + centersVector.dx
                          newCenter.y = comparingCentre.y + centersVector.dy
                          circles[i].center = newCenter
                      }
                  }
              }
          }
  ...
  }
  </code></pre>
  <p>Below is the method for generating a random <code>CGPoint</code> that gets set to the view's center:</p>
  <pre><code>func getRandomPoint() -&gt; CGPoint {
      let viewMidX = self.circlesView.bounds.midX
      let viewMidY = self.circlesView.bounds.midY
      
      let xPosition = self.circlesView.frame.midX - viewMidX + CGFloat(arc4random_uniform(UInt32(viewMidX*2)))
      let yPosition = self.circlesView.frame.midY - viewMidY + CGFloat(arc4random_uniform(UInt32(viewMidY*2)))
      let point = CGPoint(x: xPosition, y: yPosition)
      return point
  }
  </code></pre>
  <p>And below is the method for determining the distance between two <code>UIView</code>'s.</p>
  <pre><code>func distance(_ a: CGPoint, _ b: CGPoint) -&gt; CGFloat {
      let xDist = a.x - b.x
      let yDist = a.y - b.y
      return CGFloat(hypot(xDist, yDist))
  }
  </code></pre>
  <p>However, I'm still experiencing, occasionally, when two view's will overlap each other (see the red-circled section below):</p>
  <p><a href="https://i.stack.imgur.com/Jz4Al.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jz4Al.png" alt="enter image description here" /></a></p>
  <p><strong>EDIT</strong> This is code that adds the circles to the view:</p>
  <pre><code>func generateCircles() {
      numberOfCircles = Int.random(in: 1..&lt;50)
      let circleWidth = CGFloat(25)
      let circleHeight = circleWidth
      
      var i = 0
      while i &lt; numberOfCircles {
          let circleView = CircleView(frame: CGRect(x: 0.0, y: 0.0, width: circleWidth, height: circleHeight))
          let number = Int.random(in: 0..&lt;2)
          if number == 1 {
              circleView.mainColor = .yellow
          } else {
              circleView.mainColor = .blue
          }
        
          circles.append(circleView)
          i += 1
      }
      drawCircles()
  }
  </code></pre>