Given two coordinates of a line starting is (x1,y1) and ending is (x2,y2) find out the mid-point of a line.
Examples :
Input : x1 = –1, y1 = 2,
x2 = 3, y2 = –6
Output : 1,–2
Input : x1 = 6.4, y1 = 3
x2 = –10.7, y2 = 4
Output : –2.15, 3.5

The Midpoint Formula: The midpoint of two points, (x1, y2) and (x2, y2) is the point M found by the following formula: M = ((x1+x2)/2 , (y1+y2)/2)
C++
#include<iostream>
using namespace std;
void midpoint( int x1, int x2,
int y1, int y2)
{
cout << ( float )(x1+x2)/2 <<
" , " << ( float )(y1+y2)/2 ;
}
int main()
{
int x1 =-1, y1 = 2 ;
int x2 = 3, y2 = -6 ;
midpoint(x1, x2, y1, y2);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void midpoint( int x1, int x2,
int y1, int y2)
{
System.out.print((x1 + x2) / 2 +
" , " + (y1 + y2) / 2 ) ;
}
public static void main (String[] args)
{
int x1 =- 1 , y1 = 2 ;
int x2 = 3 , y2 = - 6 ;
midpoint(x1, x2, y1, y2);
}
}
|
Python3
def midpoint(x1, x2, y1, y2):
print ((x1 + x2) / / 2 , " , " ,
(y1 + y2) / / 2 )
x1, y1, x2, y2 = - 1 , 2 , 3 , - 6
midpoint(x1, x2, y1, y2)
|
C#
using System;
class GFG
{
static void midpoint( int x1, int x2,
int y1, int y2)
{
Console.WriteLine((x1 + x2) / 2 +
" , " + (y1 + y2) / 2) ;
}
public static void Main ()
{
int x1 =-1, y1 = 2 ;
int x2 = 3, y2 = -6 ;
midpoint(x1, x2, y1, y2);
}
}
|
PHP
<?php
function midpoint( $x1 , $x2 , $y1 , $y2 )
{
echo ((float)( $x1 + $x2 )/2 . " , " .
(float)( $y1 + $y2 )/2) ;
}
$x1 = -1; $y1 = 2 ;
$x2 = 3; $y2 = -6 ;
midpoint( $x1 , $x2 , $y1 , $y2 );
?>
|
Javascript
<script>
function midpoint(x1, x2,
y1, y2)
{
document.write((x1 + x2) / 2 +
" , " + (y1 + y2) / 2) ;
}
let x1 =-1, y1 = 2 ;
let x2 = 3, y2 = -6 ;
midpoint(x1, x2, y1, y2);
</script>
|
Output :
1 , -2
Time complexity: O(1) since performing only constant operations
Auxiliary Space: O(1)