Find coordinates of the triangle given midpoint of each side
Given three coordinates (x, y), which are the midpoint of the sides of the triangle. The task is to find the coordinates of the triangle.
Examples:
Input : midx1 = 5, midy1 = 3
midx2 = 4, midy2 = 4
midx3 = 5, midy3 = 5
Output : x1 = 4 y1 = 2
x2 = 4 y2 = 6
x3 = 6 y3 = 4
Solution can be verified by the figure.
Let’s separately solve for X-coordinates and Y-coordinates. For X coordinate of vertices, let them be x1, x2, x3. Then, X-coordinate of middle points will be (x1 + x2)/2, (x2 + x3)/2, (x3 + x1)/2. Observe, sum of these 3 expressions is equal to sum of X-coordinates. Now, we have sum of 3 variables and 3 expressions for sum of every pair of them, find out the values of coordinates by solving equations.
Similarly, we solve for Y-coordinates.
Below is the implementation of this approach:
C++
#include<bits/stdc++.h>
#define N 3
using namespace std;
vector< int > solve( int v[])
{
vector< int > res;
int all3 = v[0] + v[1] + v[2];
res.push_back(all3 - v[1]*2);
res.push_back(all3 - v[2]*2);
res.push_back(all3 - v[0]*2);
return res;
}
void findVertex( int xmid[], int ymid[])
{
vector< int > V1 = solve(xmid);
vector< int > V2 = solve(ymid);
for ( int i = 0; i < 3; i++)
cout << V1[i] << " "
<< V2[i] <<endl;
}
int main()
{
int xmid[N] = { 5, 4, 5 };
int ymid[N] = { 3, 4, 5 };
findVertex(xmid, ymid);
return 0;
}
|
Java
import java.util.Vector;
class GFG {
static Vector<Integer> solve( int v[]) {
Vector<Integer> res = new Vector<Integer>();
int all3 = v[ 0 ] + v[ 1 ] + v[ 2 ];
res.add(all3 - v[ 1 ] * 2 );
res.add(all3 - v[ 2 ] * 2 );
res.add(all3 - v[ 0 ] * 2 );
return res;
}
static void findVertex( int xmid[], int ymid[]) {
Vector<Integer> V1 = solve(xmid);
Vector<Integer> V2 = solve(ymid);
for ( int i = 0 ; i < 3 ; i++) {
System.out.println(V1.get(i) + " " + V2.get(i));
}
}
public static void main(String[] args) {
int xmid[] = { 5 , 4 , 5 };
int ymid[] = { 3 , 4 , 5 };
findVertex(xmid, ymid);
}
}
|
Python3
N = 3
def solve(v):
res = []
all3 = v[ 0 ] + v[ 1 ] + v[ 2 ]
res.append(all3 - v[ 1 ] * 2 )
res.append(all3 - v[ 2 ] * 2 )
res.append(all3 - v[ 0 ] * 2 )
return res
def findVertex(xmid, ymid):
V1 = solve(xmid)
V2 = solve(ymid)
for i in range ( 0 , 3 ):
print (V1[i], end = " " )
print (V2[i])
if __name__ = = '__main__' :
xmid = [ 5 , 4 , 5 ]
ymid = [ 3 , 4 , 5 ]
findVertex(xmid, ymid)
|
C#
using System;
using System.Collections;
class GFG
{
static ArrayList solve( int []v)
{
ArrayList res = new ArrayList();
int all3 = v[0] + v[1] + v[2];
res.Add(all3 - v[1] * 2);
res.Add(all3 - v[2] * 2);
res.Add(all3 - v[0] * 2);
return res;
}
static void findVertex( int []xmid, int []ymid)
{
ArrayList V1 = solve(xmid);
ArrayList V2 = solve(ymid);
for ( int i = 0; i < 3; i++)
{
Console.WriteLine(V1[i] + " " + V2[i]);
}
}
public static void Main()
{
int []xmid = {5, 4, 5};
int []ymid = {3, 4, 5};
findVertex(xmid, ymid);
}
}
|
PHP
<?php
$N = 3;
function solve( $v )
{
$res = array ();
$all3 = $v [0] + $v [1] + $v [2];
array_push ( $res , $all3 - $v [1] * 2);
array_push ( $res , $all3 - $v [2] * 2);
array_push ( $res , $all3 - $v [0] * 2);
return $res ;
}
function findVertex( $xmid , $ymid )
{
$V1 = solve( $xmid );
$V2 = solve( $ymid );
for ( $i = 0; $i < 3; $i ++)
print ( $V1 [ $i ] . " " .
$V2 [ $i ] . "\n" );
}
$xmid = array (5, 4, 5);
$ymid = array (3, 4, 5);
findVertex( $xmid , $ymid )
?>
|
Javascript
<script>
function solve(v) {
var res = [];
var all3 = v[0] + v[1] + v[2];
res.push(all3 - v[1] * 2);
res.push(all3 - v[2] * 2);
res.push(all3 - v[0] * 2);
return res;
}
function findVertex(xmid, ymid) {
var V1 = solve(xmid);
var V2 = solve(ymid);
for ( var i = 0; i < 3; i++) {
document.write(V1[i] + " " + V2[i] + "<br>" );
}
}
var xmid = [5, 4, 5];
var ymid = [3, 4, 5];
findVertex(xmid, ymid);
</script>
|
Output:
6 4
4 2
4 6
Time Complexity: O(1)
Auxiliary Space: O(1) because it is using constant space
Last Updated :
27 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...