ios – Tips on how to constrain a shifting circle in a bigger circle


This can be a lot simpler should you change the place the calculation is. As a substitute of attempting to calculate whether or not the circumference of the interior circle goes outdoors the outer circle you possibly can cut back this to take a look at the centres.

When you’ve got an outer circle of radius R and an interior circle of radius R' then, as a result of they’re circles we are able to say that if the interior circle centre is larger than (R - R') away from the centre will probably be outdoors the outer circle.

So, if the centre of the outer circle is at (0,0) then we simply must calculate the Pythagorean distance of the centre of the interior circle.

If the coordinates of the centre of the interior circle is (x,y) then we are able to do the calculation…

if x*x + y*y > (R - R') * (R - R') {
  // interior circle is outdoors outer circle
}

If the outer circle centre isn’t at (0,0) you then simply have to make use of dx and dy within the calculation the place dx = outer circle centre x - interior circle centre x and identical for dy.

Leave a Reply