Section formula (Point that divides a line in given ratio) (original) (raw)

Last Updated : 28 Jan, 2024

Given two coordinates (x1, y1) and (x2, y2), and m and n, find the co-ordinates that divides the line joining (x1, y1) and (x2, y2) in the ratio m : n

Section formula problems

**Examples:

Input : x1 = 1, y1 = 0, x2 = 2 y2 = 5,
m = 1, n = 1
Output : (1.5, 2.5)
Explanation: co-ordinates (1.5, 2.5)
divides the line in ratio 1 : 1

Input : x1 = 2, y1 = 4, x2 = 4, y2 = 6,
m = 2, n = 3
Output : (2.8, 4.8)
Explanation: (2.8, 4.8) divides the line
in the ratio 2:3

The section formula tells us the coordinates of the point which divides a given line segment into two parts such that their lengths are in the ratio m : n

Section formula

C++ `

// CPP program to find point that divides // given line in given ratio. #include using namespace std;

// Function to find the section of the line void section(double x1, double x2, double y1, double y2, double m, double n) { // Applying section formula double x = ((n * x1) + (m * x2)) / (m + n); double y = ((n * y1) + (m * y2)) / (m + n);

// Printing result
cout << "(" << x << ", ";
cout << y << ")" << endl;

}

// Driver code int main() { double x1 = 2, x2 = 4, y1 = 4, y2 = 6, m = 2, n = 3; section(x1, x2, y1, y2, m, n); return 0; }

Java

// Java program to find point that divides // given line in given ratio. import java.io.*;

class sections { static void section(double x1, double x2, double y1, double y2, double m, double n) { // Applying section formula double x = ((n * x1) + (m * x2)) / (m + n); double y = ((n * y1) + (m * y2)) / (m + n);

    // Printing result
    System.out.println("(" + x + ", " + y + ")");
}

public static void main(String[] args)
{
    double x1 = 2, x2 = 4, y1 = 4,
           y2 = 6, m = 2, n = 3;
    section(x1, x2, y1, y2, m, n);
}

}

Python

Python program to find point that divides

given line in given ratio.

def section(x1, x2, y1, y2, m, n):

# Applying section formula
x = (float)((n * x1)+(m * x2))/(m + n)
y = (float)((n * y1)+(m * y2))/(m + n)

# Printing result
print (x, y)

x1 = 2 x2 = 4 y1 = 4 y2 = 6 m = 2 n = 3 section(x1, x2, y1, y2, m, n)

C#

// C# program to find point that divides // given line in given ratio. using System;

class GFG {

static void section(double x1, double x2,
                    double y1, double y2,
                      double m, double n)
{
    
    // Applying section formula
    double x = ((n * x1) + (m * x2)) /
                                (m + n);
                                
    double y = ((n * y1) + (m * y2)) /
                               (m + n);

    // Printing result
    Console.WriteLine("(" + x + ", " + y + ")");
}

// Driver code
public static void Main()
{
    
    double x1 = 2, x2 = 4, y1 = 4,
            y2 = 6, m = 2, n = 3;
            
    section(x1, x2, y1, y2, m, n);
}

}

// This code is contributed by vt_m.

JavaScript

PHP

x2,x2, x2,y1, y2,y2, y2,m, $n) { // Applying section formula x=((x = ((x=((n * x1)+(x1) + (x1)+(m * $x2)) / ($m + $n); y=((y = ((y=((n * y1)+(y1) + (y1)+(m * $y2)) / ($m + $n); // Printing result echo("(" . $x . ", "); echo($y . ")"); } // Driver code x1=2;x1 = 2; x1=2;x2 = 4; y1=4;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mn>2</mn><mo>=</mo><mn>6</mn><moseparator="true">;</mo></mrow><annotationencoding="application/x−tex">y2=6;</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8389em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal"style="margin−right:0.03588em;">y</span><spanclass="mord">2</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:0.8389em;vertical−align:−0.1944em;"></span><spanclass="mord">6</span><spanclass="mpunct">;</span></span></span></span>m=2;y1 = 4; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mn>2</mn><mo>=</mo><mn>6</mn><mo separator="true">;</mo></mrow><annotation encoding="application/x-tex">y2 = 6; </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord">2</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord">6</span><span class="mpunct">;</span></span></span></span>m = 2; y1=4;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mn>2</mn><mo>=</mo><mn>6</mn><moseparator="true">;</mo></mrow><annotationencoding="application/xtex">y2=6;</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8389em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal"style="marginright:0.03588em;">y</span><spanclass="mord">2</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:0.8389em;verticalalign:0.1944em;"></span><spanclass="mord">6</span><spanclass="mpunct">;</span></span></span></span>m=2;n = 3; section($x1, x2,x2, x2,y1, y2,y2, y2,m, $n); // This code is contributed by Ajit. ?>

`

Output:

(2.8, 4.8)

**Time Complexity: O(1)
**Auxiliary Space: O(1)

**How does this work?

Section formula working

From our diagram, we can see,
PS = x – x1 and RT = x2 – x

We are given,

PR/QR = m/n

Using similarity, we can write
RS/QT = PS/RT = PR/QR

Therefore, we can write
PS/RR = m/n
(x - x1) / (x2 - x) = m/n

From above, we get
x = (mx2 + nx1) / (m + n)

Similarly, we can solve for y.

**References:
http://doubleroot.in/lessons/coordinate-geometry-basics/section-formula/#.WjYXQvbhU8o