
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Swift Program to Find the Mid-point of a Line
- Mid-Point Line Generation Algorithm in C++
- Prove that the line joining the mid-point of a chord to the centre of the circle passes through the mid-point of the corresponding minor arc.
- Find the other end point of a line with given one end and mid using C++
- In Question 4, point \( \mathrm{C} \) is called a mid-point of line segment \( \mathrm{AB} \). Prove that every line segment has one and only one mid-point.
- Find the mid-point of the line segment joining the points $A ( -2,\ 8)$ and $B ( -6,\ -4)$.
- Find the mid point of the line segment joining the points $( -5,\ 7)$ and $( -1,\ 3)$.
- Find the mid point of the line segment joining the points $( 0,\ 0)$ and $( -2,\ -4)$.
- Find the mid point of the line segment joining the points $( 0,\ 0)$ and $( 2,\ 2)$.
- Python program to find angle between mid-point and base of a right angled triangle
- Find the distance of the point $(1, 2)$ from the mid-point of the line segment joining the points $(6, 8)$ and $(2, 4)$.
- Show that the mid-point of the line segment joining the points $(5, 7)$ and $(3, 9)$ is also the mid-point of the line segment joining the points $(8, 6)$ and $(0, 10)$.
- C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
- If $P( 2,p)$ is the mid-point of the line segment joining the points $A( 6,-5)$ and $B( -2,11)$. Find the value of $p$.
- C++ Program to Show the Duality Transformation of Line and Point

Advertisements