📌  相关文章
📜  找出磁铁之间的最大和最小距离

📅  最后修改于: 2021-10-23 09:05:18             🧑  作者: Mango

给定坐标平面中两个轴心点 (x0, y0) & (x1, y1) 的坐标。随着每个枢轴,两个不同的磁铁分别借助长度为 r1 和 r2 的字符串连接在一起。求两个磁铁相互排斥和相互吸引时的距离。

例子 :

众所周知,磁铁的特性是,它们在同极面对面时相互排斥,在相反极点面对面时相互吸引。此外,吸引力和排斥力总是在一条直线上起作用。

我们在坐标上有两个枢轴点,所以这些点之间的距离是D = ((x1-x2) 2 +(y1-y2) 2 ) 1/2
此外,我们可以得出结论,磁铁之间的距离在排斥时最大,并且也应该是枢轴之间的距离+两条字符串的长度之和。

在吸引力的情况下,我们有两种情况需要处理:
最小距离是支点之间的距离——两个字符串的长度之和
或者,如果字符串长度的总和大于枢轴点之间的距离,则最小距离应为零。
借助图表说明:

C++
// C++ program for max and min distance
#include 
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;
}


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.


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


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.


PHP


Javascript


输出:
Distance while repulsion = 17
Distance while attraction = 0