📜  用给定的一端和中间找到线的另一端点

📅  最后修改于: 2021-04-23 20:49:12             🧑  作者: Mango

给定直线(m1,m2)的中点和直线的一个坐标(x1,y1),找到直线的另一个端点(x2,y2)
例子:

Input  : x1 = –1, y1 = 2, and 
         m1 = 3, m2 = –6
Output : x2 = 7, y2 = 10

Input  : x1 = 6.4, y1 = 3 and 
         m1 = –10.7, m2 = 4
Output : x2 = 3, y2 = 4

中点公式:(x1,y2)和(x2,y2)这两个点的中点是使用以下公式找到的点M:
M =((x1 + x2)/ 2,(y1 + y2)/ 2),
我们需要一个(x2,y2),所以我们修改公式

m1 = ((x1+x2)/2),  m2 = ((y1+y2)/2)
          2*m1 = (x1+x2),  2*m2 = (y1+y2)
          x2 = (2*m1 - x1),  y2 = (2*m2 - y1)
C++
// CPP program to find the end point of a line
#include 
using namespace std;
 
// CPP function to find the end point of a line
void otherEndPoint(int x1, int y1, int m1, int m2)
{
    // find end point for x cordinates
    float x2 = (float)(2 * m1 - x1);
 
    // find end point for y cordinates
    float y2 = (float)(2 * m2 - y1);
 
    cout << "x2 = " << x2 << ", "
         << "y2 = " << y2;
}
 
// Driven Program
int main()
{
    int x1 = -4, y1 = -1, m1 = 3, m2 = 5;
    otherEndPoint(x1, y1, m1, m2);
    return 0;
}


Java
// Java program to find the end point of a line
class GFG {
     
    // CPP function to find the end point
    // of a line
    static void otherEndPoint(int x1, int y1,
                               int m1, int m2)
    {
        // find end point for x cordinates
        float x2 = (float)(2 * m1 - x1);
     
        // find end point for y cordinates
        float y2 = (float)(2 * m2 - y1);
     
        System.out.println("x2 = " + x2 + ", "
                               + "y2 = " + y2);
    }
     
    // Driven Program
    public static void main(String args[])
    {
        int x1 = -4, y1 = -1, m1 = 3, m2 = 5;
        otherEndPoint(x1, y1, m1, m2);
    }
}
 
// This code is contributed by JaideepPyne.


Python3
# Python3 program to find the end
# point of a line
 
# function to find the end point
# of a line
def otherEndPoint(x1, y1, m1, m2):
     
    # find end point for x cordinates
    x2 = (2 * m1 - x1)
 
    # find end point for y cordinates
    y2 = (2 * m2 - y1)
 
    print ("x2 = {}, y2 = {}"
               . format(x2, y2))
 
# Driven Program
x1 = -4
y1 = -1
m1 = 3
m2 = 5
otherEndPoint(x1, y1, m1, m2)
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#
// C# program to find the
// end point of a line
using System;
 
class GFG {
     
    // function to find the
    // end pointof a line
    static void otherEndPoint(int x1, int y1,
                              int m1, int m2)
    {
         
        // find end point for x cordinates
        float x2 = (float)(2 * m1 - x1);
     
        // find end point for y cordinates
        float y2 = (float)(2 * m2 - y1);
     
        Console.WriteLine("x2 = " + x2 + ", "
                          + "y2 = " + y2);
    }
     
    // Driver Program
    public static void Main(String []args)
    {
        int x1 = -4, y1 = -1, m1 = 3, m2 = 5;
        otherEndPoint(x1, y1, m1, m2);
    }
}
 
// This code is contributed by nitin mittal.


PHP


输出:
x2 = 10, y2 = 11

时间复杂度: O(1)

辅助空间: O(1)