# Find maximum and minimum distance between magnets

Given coordinates of two pivot points (x0, y0) & (x1, y1) in coordinates plane. Along with each pivot, two different magnets are tied with the help of a string of length r1 and r2 respectively. Find the distance between both magnets when they repelling each other and when they are attracting each other.

**Examples :**

Input : x1=0, y1=0, x2=5, y2=0, r1=2, r2=2

Output : Distance while repulsion = 9, Distance while attraction = 1

Input : x1=0, y1=0, x2=5, y2=0, r1=3, r2=3

Output : Distance while repulsion = 11, Distance while attraction = 0

As we all know about the properties of magnet that they repel each other when they are facing each other with the same pole and attract each other when they are facing each other with opposite pole. Also, the force of attraction, as well as repulsion, always work in a straight line.

We have two pivots points on coordinates, so distance between these points are **D = ((x1-x2) ^{2} +(y1-y2)^{2} )^{1/2}**.

Also, we can conclude that distance between magnet is maximum while repulsion and that too should be the distance between pivots + sum of the length of both strings.

In case of attraction we have two cases to take care of:

Either the minimum distance is the distance between pivots – the sum of the length of both strings

Or minimum distance should be zero in case if the sum of the length of strings is greater than the distance between pivot points.

Illustration with help of diagram:

## C++

`// C++ program for max and min distance ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for finding distance between pivots ` `int` `pivotDis(` `int` `x0, ` `int` `y0, ` `int` `x1, ` `int` `y1) ` `{ ` ` ` `return` `sqrt` `((x1 - x0) * (x1 - x0) + ` ` ` `(y1 - y0) * (y1 - y0)); ` `} ` ` ` `// Function for minimum distance ` `int` `minDis(` `int` `D, ` `int` `r1, ` `int` `r2) ` `{ ` ` ` `return` `max((D - r1 - r2), 0); ` `} ` ` ` `// Function for maximum distance ` `int` `maxDis(` `int` `D, ` `int` `r1, ` `int` `r2) ` `{ ` ` ` `return` `D + r1 + r2; ` `} ` ` ` `// Drivers code ` `int` `main() ` `{ ` ` ` `int` `x0 = 0, y0 = 0, x1 = 8, y1 = 0, r1 = 4, r2 = 5; ` ` ` `int` `D = pivotDis(x0, y0, x1, y1); ` ` ` `cout << ` `"Distance while repulsion = "` `<< maxDis(D, r1, r2); ` ` ` `cout << ` `"\nDistance while attraction = "` `<< minDis(D, r1, r2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for max ` `// and min distance ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function for finding ` `// distance between pivots ` `static` `int` `pivotDis(` `int` `x0, ` `int` `y0, ` ` ` `int` `x1, ` `int` `y1) ` `{ ` ` ` `return` `(` `int` `)Math.sqrt((x1 - x0) * ` ` ` `(x1 - x0) + ` ` ` `(y1 - y0) * ` ` ` `(y1 - y0)); ` `} ` ` ` `// Function for ` `// minimum distance ` `static` `int` `minDis(` `int` `D, ` `int` `r1, ` `int` `r2) ` `{ ` ` ` `return` `Math.max((D - r1 - r2), ` `0` `); ` `} ` ` ` `// Function for ` `// maximum distance ` `static` `int` `maxDis(` `int` `D, ` `int` `r1, ` `int` `r2) ` `{ ` ` ` `return` `D + r1 + r2; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `int` `x0 = ` `0` `, y0 = ` `0` `, x1 = ` `8` `, ` ` ` `y1 = ` `0` `, r1 = ` `4` `, r2 = ` `5` `; ` `int` `D = pivotDis(x0, y0, x1, y1); ` `System.out.print( ` `"Distance while "` `+ ` ` ` `"repulsion = "` `+ ` ` ` `maxDis(D, r1, r2)); ` `System.out.print(` `"\nDistance while "` `+ ` ` ` `"attraction = "` `+ ` ` ` `minDis(D, r1, r2)); ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program for max and min ` `# distance ` `import` `math ` ` ` `# Function for finding distance between ` `# pivots ` `def` `pivotDis(x0, y0, x1, y1): ` ` ` ` ` `return` `math.sqrt((x1 ` `-` `x0) ` `*` `(x1 ` `-` `x0) ` ` ` `+` `(y1 ` `-` `y0) ` `*` `(y1 ` `-` `y0)) ` ` ` `# Function for minimum distance ` `def` `minDis( D, r1, r2): ` ` ` ` ` `return` `max` `((D ` `-` `r1 ` `-` `r2), ` `0` `) ` ` ` `# Function for maximum distance ` `def` `maxDis( D, r1, r2): ` ` ` ` ` `return` `D ` `+` `r1 ` `+` `r2 ` ` ` `# Drivers code ` `x0 ` `=` `0` `y0 ` `=` `0` `x1 ` `=` `8` `y1 ` `=` `0` `r1 ` `=` `4` `r2 ` `=` `5` `D ` `=` `pivotDis(x0, y0, x1, y1) ` `print` `(` `"Distance while repulsion = "` `, ` ` ` `int` `(maxDis(D, r1, r2))) ` ` ` `print` `(` `"Distance while attraction = "` `, ` ` ` `minDis(D, r1, r2)) ` ` ` `# This code is contributed by Smitha ` |

*chevron_right*

*filter_none*

## C#

`// C# program for max and min distance ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function for finding ` ` ` `// distance between pivots ` ` ` `static` `int` `pivotDis(` `int` `x0, ` `int` `y0, ` ` ` `int` `x1, ` `int` `y1) ` ` ` `{ ` ` ` `return` `(` `int` `)Math.Sqrt((x1 - x0) * ` ` ` `(x1 - x0) + ` ` ` `(y1 - y0) * ` ` ` `(y1 - y0)); ` ` ` `} ` ` ` ` ` `// Function for ` ` ` `// minimum distance ` ` ` `static` `int` `minDis(` `int` `D, ` `int` `r1, ` `int` `r2) ` ` ` `{ ` ` ` `return` `Math.Max((D - r1 - r2), 0); ` ` ` `} ` ` ` ` ` `// Function for ` ` ` `// maximum distance ` ` ` `static` `int` `maxDis(` `int` `D, ` `int` `r1, ` `int` `r2) ` ` ` `{ ` ` ` `return` `D + r1 + r2; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `x0 = 0, y0 = 0, x1 = 8, ` ` ` `y1 = 0, r1 = 4, r2 = 5; ` ` ` `int` `D = pivotDis(x0, y0, x1, y1); ` ` ` ` ` `Console.WriteLine( ` `"Distance while "` `+ ` ` ` `"repulsion = "` `+ ` ` ` `maxDis(D, r1, r2)); ` ` ` `Console.WriteLine(` `"Distance while "` `+ ` ` ` `"attraction = "` `+ ` ` ` `minDis(D, r1, r2)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for max and ` `// min distance ` ` ` `// Function for finding ` `// distance between pivots ` `function` `pivotDis(` `$x0` `, ` `$y0` `, ` ` ` `$x1` `, ` `$y1` `) ` `{ ` ` ` `return` `sqrt((` `$x1` `- ` `$x0` `) * ` ` ` `(` `$x1` `- ` `$x0` `) + ` ` ` `(` `$y1` `- ` `$y0` `) * ` ` ` `(` `$y1` `- ` `$y0` `)); ` `} ` ` ` `// Function for minimum distance ` `function` `minDis( ` `$D` `, ` `$r1` `, ` `$r2` `) ` `{ ` ` ` `return` `max((` `$D` `- ` `$r1` `- ` `$r2` `), 0); ` `} ` ` ` `// Function for maximum distance ` `function` `maxDis( ` `$D` `, ` `$r1` `, ` `$r2` `) ` `{ ` ` ` `return` `$D` `+ ` `$r1` `+ ` `$r2` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$x0` `= 0; ` `$y0` `= 0; ` ` ` `$x1` `= 8; ` `$y1` `= 0; ` ` ` `$r1` `= 4; ` `$r2` `= 5; ` ` ` `$D` `= pivotDis(` `$x0` `, ` `$y0` `, ` ` ` `$x1` `, ` `$y1` `); ` ` ` `echo` `"Distance while repulsion = "` ` ` `, maxDis(` `$D` `, ` `$r1` `, ` `$r2` `); ` ` ` `echo` `"\nDistance while attraction = "` ` ` `, minDis(` `$D` `, ` `$r1` `, ` `$r2` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Distance while repulsion = 17 Distance while attraction = 0

## Recommended Posts:

- Find the maximum possible distance from origin using given points
- Program to find the minimum (or maximum) element of an array
- Puzzle | Minimum distance for Lizard
- Minimum distance to the corner of a grid from source
- Minimum distance to travel to cover all intervals
- Find points at a given distance on a line of given slope
- Haversine formula to find distance between two points on a sphere
- Count paths with distance equal to Manhattan distance
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Find the distance covered to collect items at equal distances
- Maximum and Minimum value of a quadratic function
- Find the first, second and third minimum elements in an array
- Distance of chord from center when distance between center and another equal length chord is given
- Program to find the maximum element in a Matrix

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.