

- 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 the other end point of a line with given one end and mid using C++
In this problem, we are given the coordinates of two points of a line starting point A(xA, yA) and midpoint M(xM, yM) .Our task is to find the other end point of a line with given one end and mid.
Let’s take an example to understand the problem,
Input
A = [1, 2], M = [3, 0]
Output
[5, -2]
Explanation
The line is −
Solution Approach
To solve the problem, we will be using the concepts of geometry we have learned in mathematics. If you remember there is a midpoint formula for every line which is,
mid(x) = (x1 + x2) / 2 mid(y) = (y1 + y2) / 2
But we are given the value of the midpoint in the problem and need the value for x2 and y2. So, we will change the formula accordingly.
x2 = 2*mid(x) - x1 y2 = 2*mid(y) - y1
Using the above formula, we can find the value of the other endpoint using the midpoint and one point of the line.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; void findMissingPointLine(float x1, float y1, float xm, float ym){ float x2 = (2 * xm) - x1; float y2 = (2 * ym) - y1; cout<<"B(x, y) = "<<"( "<<x2<<", "<<y2<<" )"; } int main() { float x1 = -4, y1 = -1, xm = 3, ym = 5; cout<<"The other end point of the line is \n"; findMissingPointLine(x1, y1, xm, ym); return 0; }
Output
The other end point of the line is B(x, y) = ( 10, 11 )
- Related Questions & Answers
- Program to find the mid-point of a line in C++
- C++ program to find out number of changes required to get from one end to other end in a grid
- Find the other-end coordinates of diameter in a circler in C++
- Mid-Point Line Generation Algorithm in C++
- Match optional end of line after every record with REGEXP?
- How to match end of a particular string/line using Java RegEx
- Append to StringBuilder in C# with a new line on the end
- Find the Kth Node from the end in a given singly Linked List using C++
- Python Pandas - Find the end time for the given Period object
- Set animation with a slow end using CSS
- Count strings that end with the given pattern in C++
- Set an animation with a slow start and end using CSS
- Python program to find angle between mid-point and base of a right angled triangle
- Explain the END OF FILE (EOF) with a C Program
- How to annotate the end of lines using Python and Matplotlib?
Advertisements