Find the Maximum and Minimum Distance Between Magnets


In this problem, we need to calculate the separation between two magnets that are attached to distinct pivots. We need to calculate the maximum and minimum distance between magnets i.e when the magnets attract and when they repel.

The string's length between each magnet and the pivot is specified. Depending on their polarity, the magnets will either repel or attract one another. Calculating the distance between the two magnets when they are attracted and repelling one another is the task. Using the distance formula and taking the polarity of the magnets into account, the issue can be resolved.

To calculate the maximum distance between the two magnets when they are repelling each other, we calculate the distance between the pivot points plus the sum of the lengths of the strings.

To calculate the minimum distance between the two magnets when they are attracting each other, consider the following.

If the length of the strings is greater than the distance between the pivot points, the magnets will touch each other and the minimum distance will be zero. Otherwise, the minimum distance is the distance between the pivot points minus the sum of the lengths of the strings.

Approach

Let's discuss step by step approach to solve this:

  • The two coordinate points (x0, y0), (x1, y1) are taken as user input.

  • The length of the strings attached to the two magnets (here r1,r2) should also be taken as user input.

  • To calculate the distance between the two pivot points we use the formula d = sqrt((x1 − x0)2 + (y1 − y0)2). This is a general formula for determining the separation between two coordinate points.

  • Use the equation min_dist = max(0, d − (r1 + r2)) to determine the smallest distance. The max() function makes sure that the distance is zero when the two magnets touch. Otherwise, if they don’t touch then min_distance = distance between the two pivots − length of the two strings.

  • The greatest separation between the magnets when they are repelling one another can be calculated using the formula max dist = d + r1 + r2 by adding the lengths of the two strings to the distance between the two pivot points.

  • Finally, print the minimum and maximum distance between the magnets

Code Implementation

Here is the code implementation in c++ to find the minimum and maximum distance between magnets

Example

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x0= 0;
 double y0= 0;
 double x1= 8;
 double y1= 0;
 double r1= 4;
 double r2=5;
 double  d;
    

    d = sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2));
    double min_dist = max(0.0, d - (r1 + r2));
    double max_dist = d + r1 + r2;

    cout << "Minimum distance between magnets: " << min_dist << endl;
    cout << "Maximum distance between magnets: " << max_dist << endl;

    return 0;
}

Output

Minimum distance between magnets: 0
Maximum distance between magnets: 17

Time Complexity: O(1)

Space Complexity: O(1)

Conclusion

In this article, we have tried to explain the approach to find out the minimum and maximum distance between two magnets that are attached to strings, with the coordinates of the pivot along with the string length provided as input. I hope this article helps you to learn the concept in a better way

Updated on: 23-Aug-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements