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

**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

## C++

`// CPP program to find point that divides ` `// given line in given ratio. ` `#include <iostream> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find point that ` `// divides given line in given ratio. ` ` ` `// Function to find the ` `// section of the line ` `function` `section(` `$x1` `, ` `$x2` `, ` `$y1` `, ` ` ` `$y2` `, ` `$m` `, ` `$n` `) ` `{ ` ` ` ` ` `// Applying section formula ` ` ` `$x` `= ((` `$n` `* ` `$x1` `) + (` `$m` `* ` `$x2` `)) ` ` ` `/ (` `$m` `+ ` `$n` `); ` ` ` ` ` `$y` `= ((` `$n` `* ` `$y1` `) + (` `$m` `* ` `$y2` `)) ` ` ` `/ (` `$m` `+ ` `$n` `); ` ` ` ` ` `// Printing result ` ` ` `echo` `(` `"("` `. ` `$x` `. ` `", "` `); ` ` ` `echo` `(` `$y` `. ` `")"` `); ` `} ` ` ` `// Driver code ` `$x1` `= 2; ` `$x2` `= 4; ` `$y1` `= 4; ` `$y2` `= 6; ` `$m` `= 2; ` `$n` `= 3; ` `section(` `$x1` `, ` `$x2` `, ` `$y1` `, ` `$y2` `, ` `$m` `, ` `$n` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

(2.8, 4.8)

**How does this work?**

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Section formula for 3 D
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Find intersection point of lines inside a section
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Find an Integer point on a line segment with given two ends
- Find the other end point of a line with given one end and mid
- Check whether the point (x, y) lies on a given line
- Slope of the line parallel to the line with the given slope
- Program for Stirling Interpolation Formula
- Print first n Fibonacci Numbers using direct formula
- Newton's Divided Difference Interpolation Formula
- Haversine formula to find distance between two points on a sphere
- Program to implement Inverse Interpolation using Lagrange Formula

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.