Given two coordinates (x1, y1, z1) and (x2, y2, z2) in 3D, and m and n, find the co-ordinates that divides the line joining (x1, y1, Z1) and (x2, y2, Z2) in the ratio m : n.
Examples:
Input : x1 = 2, y1 = -1, Z1 = 4, x2 = 4, y2 = 3, Z2 = 2,
m = 2, n = 3
Output : (2.8, .6, 3.2)Explanation: co-ordinates (2.8, .6, 3.2)
divides the line in ratio 2 : 3

Approach:
Given two coordinates A(x1, y1, Z1) and B(x2, y2, Z2) in 3D, and m and n, we have to find the co-ordinates that divides the line joining (x1, y1, Z1) and (x2, y2, Z2) in the ratio m : n.
Let the co-ordinates will be P(x, y, z)
then according to section fORmula in 3 D
x = (m * x2 + n * x1) / (m + n)
y = (m * y2 + n * y1) / (m + n)
z = (m * z2 + n * z1) / (m + n)
Below is the implementation of above approach:
C++
// CPP program to find point that divides // given line in given ratio in 3D. #include <iostream> using namespace std; // Function to find the section of the line void section(double x1, double x2, double y1, double y2, double z1, double z2, double m, double n) { // Applying section formula double x = ((m * x2) + (n * x1)) / (m + n); double y = ((m * y2) + (n * y1)) / (m + n); double z = ((m * z2) + (n * z1)) / (m + n); // Printing result cout << "(" << x << ", "; cout << y << ", "; cout << z << ")" << endl; } // Driver code int main() { double x1 = 2, x2 = 4, y1 = -1, y2 = 3, z1 = 4, z2 = 2, m = 2, n = 3; section(x1, x2, y1, y2, z1, z2, m, n); return 0; } |
Java
// Java program to find point that divides // given line in given ratio in 3D. import java.util.*; class solution { // Function to find the section of the line static void section(double x1, double x2, double y1, double y2, double z1, double z2, double m, double n) { // Applying section formula double x = ((m * x2) + (n * x1)) / (m + n); double y = ((m * y2) + (n * y1)) / (m + n); double z = ((m * z2) + (n * z1)) / (m + n); System.out.print( "(" +x +", "); System.out.print( y+ ", "); System.out.println(z + ")" ); } // Driver code public static void main(String arr[]) { double x1 = 2, x2 = 4, y1 = -1, y2 = 3, z1 = 4, z2 = 2, m = 2, n = 3; section(x1, x2, y1, y2, z1, z2, m, n); } } //This code is contributed by Surendra_Gangwar |
Python3
# Python 3 program to find point that divides # given line in given ratio in 3D. # Function to find the section of the line def section(x1, x2, y1, y2, z1, z2, m, n): # Applying section formula x = ((m * x2) + (n * x1)) / (m + n) y = ((m * y2) + (n * y1)) / (m + n) z = ((m * z2) + (n * z1)) / (m + n) # Printing result print("(",x,",",y,",",z,")") # Driver code if __name__ == '__main__': x1 = 2 x2 = 4 y1 = -1 y2 = 3 z1 = 4 z2 = 2 m = 2 n = 3 section(x1, x2, y1, y2, z1, z2, m, n) #This code is contributed by # Surendra_Gangwar |
C#
// C# program to find point that divides // given line in given ratio in 3D. using System; class GFG { // Function to find the section // of the line static void section(double x1, double x2, double y1, double y2, double z1, double z2, double m, double n) { // Applying section formula double x = ((m * x2) + (n * x1)) / (m + n); double y = ((m * y2) + (n * y1)) / (m + n); double z = ((m * z2) + (n * z1)) / (m + n); Console.Write("(" + x +", "); Console.Write(y + ", "); Console.WriteLine(z + ")" ); } // Driver code static public void Main () { double x1 = 2, x2 = 4, y1 = -1, y2 = 3, z1 = 4, z2 = 2, m = 2, n = 3; section(x1, x2, y1, y2, z1, z2, m, n); } } // This code is contributed by ajit. |
PHP
<?php // PHP program to find point that divides // given line in given ratio in 3D. // Function to find the section of the line function section($x1, $x2, $y1, $y2, $z1, $z2, $m, $n) { // Applying section formula $x = (($m * $x2) + ($n * $x1)) / ($m + $n); $y = (($m * $y2) + ($n * $y1)) / ($m + $n); $z = (($m * $z2) + ($n * $z1)) / ($m + $n); // Printing result echo "(" . $x . ", "; echo $y . ", "; echo $z . ")" ."\n"; } // Driver code $x1 = 2; $x2 = 4; $y1 = -1; $y2 = 3; $z1 = 4; $z2 = 2; $m = 2; $n = 3; section($x1, $x2, $y1, $y2, $z1, $z2, $m, $n); // This code is contributed // by Akanksha Rai |
Output:
(2.8, 0.6, 3.2)
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:
- Section formula (Point that divides a line in given ratio)
- Recursive program to print formula for GCD of n integers
- Haversine formula to find distance between two points on a sphere
- Find intersection point of lines inside a section
- Shuffle the position of each Array element by swapping adjacent elements
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- C Program to count the number of zeros from 0 to N
- Smallest positive integer X satisfying the given equation
- Count of repeating digits in a given Number
- Coplanarity of Two Lines in 3D Geometry
- 7 different ways to Add Two Numbers in C/C++
- What happens when we call a Function
- Distance between Incenter and Circumcenter of a triangle using Inradius and Circumradius
- Even Perfect Number
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.

