# Minimum revolutions to move center of a circle to a target

Given a circle of radius r and center in point(x1, y1) and given a point(x2, y2). The task is move center of circle from given center (x1, y1) to target (x2, y2) using minimum number of steps. In one step, we can put a pin on the border of the circle at any point, then rotate the circle around that pin by any angle and finally remove the pin.

**Examples :**

Input : r = 2 x1 = 0, y1 = 0 x2 = 0, y2 = 4 Output :1 Input : r = 1 x1 = 1, y1 = 1, x2 = 4, y2 = 4 Output : 3

Let’s consider a straight line between the two centers. Clearly to move the center with maximum distance we need to rotate it around the the line and with 180 degrees. So the maximum distance we can move the center each time is 2 * r. Let’s continue moving the center with 2 * r distance each time until the two circles intersects. Now obviously we can make the center moves into its final position by rotating it around one of the intersection points of the two circles until it reaches the final destination.

Every time we make the circle moves 2 * r times except the last move it’ll be < 2 * r. Let the initial distance between the two points be d. Solution to the problem will be ceil(d/2*r).

## C++

`// C++ program to find minimum number of ` `// revolutions to reach a target center ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Minimum revolutions to move center from ` `// (x1, y1) to (x2, y2) ` `int` `minRevolutions(` `double` `r, ` `int` `x1, ` `int` `y1, ` ` ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `double` `d = ` `sqrt` `((x1 - x2)*(x1 - x2) + ` ` ` `(y1 - y2)*(y1 - y2)); ` ` ` `return` `ceil` `(d/(2*r)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `r = 2, x1 = 0, y1 = 0, x2 = 0, y2 = 4; ` ` ` `cout << minRevolutions(r, x1, y1, x2, y2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum number of ` `// revolutions to reach a target center ` `class` `GFG { ` ` ` ` ` `// Minimum revolutions to move center ` ` ` `// from (x1, y1) to (x2, y2) ` ` ` `static` `double` `minRevolutions(` `double` `r, ` ` ` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` ` ` `double` `d = Math.sqrt((x1 - x2) ` ` ` `* (x1 - x2) + (y1 - y2) ` ` ` `* (y1 - y2)); ` ` ` ` ` `return` `Math.ceil(d / (` `2` `* r)); ` ` ` `} ` ` ` ` ` `// Driver Program to test above function ` ` ` `public` `static` `void` `main(String arg[]) { ` ` ` ` ` `int` `r = ` `2` `, x1 = ` `0` `, y1 = ` `0` `; ` ` ` `int` `x2 = ` `0` `, y2 = ` `4` `; ` ` ` ` ` `System.out.print((` `int` `)minRevolutions(r, ` ` ` `x1, y1, x2, y2)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# minimum number of ` `# revolutions to reach ` `# a target center ` `import` `math ` ` ` `# Minimum revolutions to move center from ` `# (x1, y1) to (x2, y2) ` `def` `minRevolutions(r,x1,y1,x2,y2): ` ` ` ` ` `d ` `=` `math.sqrt((x1 ` `-` `x2)` `*` `(x1 ` `-` `x2) ` `+` ` ` `(y1 ` `-` `y2)` `*` `(y1 ` `-` `y2)) ` ` ` `return` `math.ceil(d` `/` `/` `(` `2` `*` `r)) ` ` ` `# Driver code ` ` ` `r ` `=` `2` `x1 ` `=` `0` `y1 ` `=` `0` `x2 ` `=` `0` `y2 ` `=` `4` ` ` `print` `(minRevolutions(r, x1, y1, x2, y2)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum number of ` `// revolutions to reach a target center ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Minimum revolutions to move center ` ` ` `// from (x1, y1) to (x2, y2) ` ` ` `static` `double` `minRevolutions(` `double` `r, ` ` ` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` ` ` `double` `d = Math.Sqrt((x1 - x2) ` ` ` `* (x1 - x2) + (y1 - y2) ` ` ` `* (y1 - y2)); ` ` ` ` ` `return` `Math.Ceiling(d / (2 * r)); ` ` ` `} ` ` ` ` ` `// Driver Program to test above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `r = 2, x1 = 0, y1 = 0; ` ` ` `int` `x2 = 0, y2 = 4; ` ` ` ` ` `Console.Write((` `int` `)minRevolutions(r, ` ` ` `x1, y1, x2, y2)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// number of revolutions to reach ` `// a target center ` ` ` ` ` `// Minimum revolutions to move ` `// center from (x1, y1) to (x2, y2) ` `function` `minRevolutions(` `$r` `, ` `$x1` `, ` `$y1` `, ` ` ` `$x2` `, ` `$y2` `) ` `{ ` ` ` `$d` `= sqrt((` `$x1` `- ` `$x2` `) * (` `$x1` `- ` `$x2` `) + ` ` ` `(` `$y1` `- ` `$y2` `) * (` `$y1` `- ` `$y2` `)); ` ` ` `return` `ceil` `(` `$d` `/ (2 * ` `$r` `)); ` `} ` ` ` `// Driver code ` `$r` `= 2; ` `$x1` `= 0; ` `$y1` `= 0; ` `$x2` `= 0; ` `$y2` `= 4; ` `echo` `minRevolutions(` `$r` `, ` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `); ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1

**Time Complexity :** O(1)

This article is contributed by **Rakesh Kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Find the center of the circle using endpoints of diameter
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Length of the chord of the circle whose radius and the angle subtended at the center by the chord is given
- Minimum cuts required to divide the Circle into equal parts
- Find minimum radius such that atleast k point lie inside the circle
- Distance of chord from center when distance between center and another equal length chord is given
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Circle and Lattice Points
- Number of rectangles in a circle of radius R
- Area of decagon inscribed within the circle