# Find coordinates of the triangle given midpoint of each side in C++

Suppose we have three coordinates which are midpoint of sides of the triangle. We have to find the coordinates of the triangle. So if the inputs are like (5, 3), (4, 4), (5, 5), then output will be (4, 2), (4, 6), (6, 4).

To solve this, we have to solve for X-coordinates and Y-coordinates separately. 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. If we observe the sum of these three expressions is equal to sum of X-coordinates. Now, we have sum of three variables and three expressions for sum of every pair of them. We have to find out the values of coordinates by solving equations. Similarly, we solve for Y-coordinates.

## Example

Live Demo

#include<iostream>
#include<vector>
#define N 3
using namespace std;
vector<int> getResult(int v[]) {
vector<int> res;
int sum = v + v + v;
res.push_back(sum - v*2);
res.push_back(sum - v*2);
res.push_back(sum - v*2);
return res;
}
void searchPoints(int mid_x_coord[], int mid_y_coord[]) {
vector<int> x_vals = getResult(mid_x_coord);
vector<int> y_vals = getResult(mid_y_coord);
for (int i = 0; i < 3; i++)
cout << x_vals[i] << " " << y_vals[i] <<endl;
}
int main() {
int mid_x_coord[N] = { 5, 4, 5 };
int mid_y_coord[N] = { 3, 4, 5 };
searchPoints(mid_x_coord, mid_y_coord);
}

## Output

6 4
4 2
4 6