

- 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
Program to find the mid-point of a line in C++
In this problem, we are given two points A and B, starting and ending point of a line. Our task is to create a program to find the mid-point of a line in C++.
Problem Description − Here, we have a line with starting and ending points A(x1, y1) and B(x2, y2). And we need to find the mid-point of the line.
Let’s take an example to understand the problem,
Input
a(x1, y1) = (4, -5) b(x2, y2) = (-2, 6)
Output
(1, 0.5)
Explanation
(x1 + x2)/2 = 4 - 2 / 2 = 1 (y1 + y2)/2 = -5 + 6 / 2 = 0.5
Solution Approach
To solve the problem, a simple method is using the geometrical formula for the mid of a line. The formula is given by,
Mid = ( ((x1 + x2)/2), ((y1 + y2)/2) )
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; int main() { float point[2][2] = {{-4, 5}, {-2, 6}}; float midX = (float)(( point[0][0] + point[1][0])/2); float midY = (float)(( point[0][1] + point[1][1])/2); cout<<"The mid-points are ("<<midX<<" , "<<midY<<")"; return 0; }
Output
The mid-points are (-3 , 5.5)
- Related Questions & Answers
- Mid-Point Line Generation Algorithm in C++
- Find the other end point of a line with given one end and mid using C++
- Python program to find angle between mid-point and base of a right angled triangle
- C++ Program to Show the Duality Transformation of Line and Point
- Program to find the highest altitude of a point in Python
- C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
- Find foot of perpendicular from a point in 2D plane to a Line in C++
- Python program to find the Decreasing point in a List
- Program to find slope of a line in C++
- Program to find the Break Even Point in C++
- C++ Program to find out the moves to read a point from another point in a 2D plane
- Program to find the number of possible position in a line in Python
- Program to find GCD of floating point numbers in C++
- Find Corners of Rectangle using mid points in C++
- C++ program to find first collision point of two series
Advertisements