Encuentre el otro punto final de una línea con un extremo y un medio dados

Dado un punto medio de la línea (m1, m2) y una coordenada de una línea (x1, y1), encuentre el otro punto final (x2, y2) de una línea.

Ejemplos: 

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

La fórmula del punto medio: El punto medio de dos puntos, (x1, y2) y (x2, y2) es el punto M que se encuentra usando: 
M=((x1+x2)/2, (y1+y2)/2), 

Necesitamos un (x2, y2) por lo que modificamos la fórmula 

          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 <iostream>
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 coordinates
    float x2 = (float)(2 * m1 - x1);
 
    // find end point for y coordinates
    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 coordinates
        float x2 = (float)(2 * m1 - x1);
     
        // find end point for y coordinates
        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 coordinates
    x2 = (2 * m1 - x1)
 
    # find end point for y coordinates
    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 coordinates
        float x2 = (float)(2 * m1 - x1);
     
        // find end point for y coordinates
        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

<?php
// php program to find the end point of a line
 
// PHP function to find the end point of a line
function otherEndPoint($x1, $y1, $m1, $m2)
{
     
    // find end point for x coordinates
    $x2 = (2 * $m1 - $x1);
 
    // find end point for y coordinates
    $y2 = (2 * $m2 - $y1);
 
    echo "x2 = " . $x2 . ", y2 = " . $y2 ;
}
 
// Driven Program
    $x1 = -4; $y1 = -1; $m1 = 3; $m2 = 5;
    otherEndPoint($x1, $y1, $m1, $m2);
 
// This code is contributed by nitin mittal.
?>

Javascript

<script>
 
// Javascript program to find the
// end point of a line
 
// Function to find
// the end point of a line
function otherEndPoint(x1, y1, m1, m2)
{
     
    // Find end point for x coordinates
    let x2 = 2 * m1 - x1;
 
    // Find end point for y coordinates
    let y2 = 2 * m2 - y1;
 
    document.write("x2 = " + x2 + ", " +
                   "y2 = " + y2);
}
 
// Driver code
let x1 = -4, y1 = -1, m1 = 3, m2 = 5;
 
otherEndPoint(x1, y1, m1, m2);
 
// This code is contributed by jana_sayantan
     
</script>
Producción: 

x2 = 10, y2 = 11

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por jaingyayak y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *