

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#include<iostream> #include<vector> #define N 3 using namespace std; vector<int> getResult(int v[]) { vector<int> res; int sum = v[0] + v[1] + v[2]; res.push_back(sum - v[1]*2); res.push_back(sum - v[2]*2); res.push_back(sum - v[0]*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
- Related Questions & Answers
- Find minimum area of rectangle with given set of coordinates in C++
- Center of each side of a polygon in JavaScript
- Python Pandas - Return the midpoint of each Interval in the IntervalArray as an Index
- Defining the midpoint of a colormap in Matplotlib
- Python Pandas - Return the midpoint of the Interval
- Golang Program to Find the Area of a Triangle Given All Three Sides
- How to plot bar graphs with same X coordinates side by side in Matplotlib?
- Find the hypotenuse of a right angled triangle with given two sides in C++
- How to find the coordinates of the cursor with JavaScript?
- Find all possible coordinates of parallelogram in C++
- Program to find the count of coins of each type from the given ratio in C++
- Find the dimensions of Right angled triangle in C++
- Coordinates of rectangle with given points lie inside in C++
- Program to find the Centroid of the Triangle in C++
- C++ program to find the Radius of the incircle of the triangle
Advertisements