Find the other end point of a line with given one end and mid
Last Updated :
20 Feb, 2023
Given a midpoint of line(m1, m2) and one coordinate of a line (x1, y1), find the other end point(x2, y2) of a line.
Examples:
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
The Midpoint Formula: The midpoint of two points, (x1, y2) and (x2, y2) is the point M found by using:
M=((x1+x2)/2, (y1+y2)/2),
We have need of a (x2, y2) so we modifies the formula
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++
#include <iostream>
using namespace std;
void otherEndPoint( int x1, int y1, int m1, int m2)
{
float x2 = ( float )(2 * m1 - x1);
float y2 = ( float )(2 * m2 - y1);
cout << "x2 = " << x2 << ", "
<< "y2 = " << y2;
}
int main()
{
int x1 = -4, y1 = -1, m1 = 3, m2 = 5;
otherEndPoint(x1, y1, m1, m2);
return 0;
}
|
Java
class GFG {
static void otherEndPoint( int x1, int y1,
int m1, int m2)
{
float x2 = ( float )( 2 * m1 - x1);
float y2 = ( float )( 2 * m2 - y1);
System.out.println( "x2 = " + x2 + ", "
+ "y2 = " + y2);
}
public static void main(String args[])
{
int x1 = - 4 , y1 = - 1 , m1 = 3 , m2 = 5 ;
otherEndPoint(x1, y1, m1, m2);
}
}
|
Python3
def otherEndPoint(x1, y1, m1, m2):
x2 = ( 2 * m1 - x1)
y2 = ( 2 * m2 - y1)
print ( "x2 = {}, y2 = {}"
. format (x2, y2))
x1 = - 4
y1 = - 1
m1 = 3
m2 = 5
otherEndPoint(x1, y1, m1, m2)
|
C#
using System;
class GFG {
static void otherEndPoint( int x1, int y1,
int m1, int m2)
{
float x2 = ( float )(2 * m1 - x1);
float y2 = ( float )(2 * m2 - y1);
Console.WriteLine( "x2 = " + x2 + ", "
+ "y2 = " + y2);
}
public static void Main(String []args)
{
int x1 = -4, y1 = -1, m1 = 3, m2 = 5;
otherEndPoint(x1, y1, m1, m2);
}
}
|
PHP
<?php
function otherEndPoint( $x1 , $y1 , $m1 , $m2 )
{
$x2 = (2 * $m1 - $x1 );
$y2 = (2 * $m2 - $y1 );
echo "x2 = " . $x2 . ", y2 = " . $y2 ;
}
$x1 = -4; $y1 = -1; $m1 = 3; $m2 = 5;
otherEndPoint( $x1 , $y1 , $m1 , $m2 );
?>
|
Javascript
<script>
function otherEndPoint(x1, y1, m1, m2)
{
let x2 = 2 * m1 - x1;
let y2 = 2 * m2 - y1;
document.write( "x2 = " + x2 + ", " +
"y2 = " + y2);
}
let x1 = -4, y1 = -1, m1 = 3, m2 = 5;
otherEndPoint(x1, y1, m1, m2);
</script>
|
Time Complexity:O(1)
Auxiliary Space:O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...