 # Python Program for Section Formula (Point that Divides a Line in Given Ratio)

In the previous article, we have discussed Python Program to Find the Mid-Point of a Line
Given two points of a line and P, Q, the task is to find the point that divides the line in the given ratio P: Q in Python.

The section formula gives us the coordinates of the point that divides a given line segment into two parts with lengths that are in the ratio m: n.

Section Formula:

(mx2+nx1)/(m+n), (my2+ny1)/(m+n)

Examples:

Example1:

Input:

Given First Point = ( 2, 4 )
Given Second Point = ( 1, 3 )
Given ratio value p= 1
Given ratio value q= 2

Output:

The point that divides the line in the given ratio ( 1 : 2 ) is :
( 1.6666666666666667 , 3.6666666666666665 )

Example2:

Input:

Given First Point = ( 5, 7)
Given Second Point = ( 2, 8 )
Given ratio value p= 2
Given ratio value q= 5

Output:

The point that divides the line in the given ratio ( 2 : 5 ) is :
( 4.142857142857143 , 7.285714285714286 )

## Program for Section Formula (Point that Divides a Line in Given Ratio) in Python:

Below are the ways to find the point that divides the line in the given ratio P: Q in Python:

### Method #1: Using Mathematical Formula (Static Input)

Approach:

• Give the first point as static input and store it in two variables.
• Give the second point as static input and store it in another two variables.
• Give the ratio values p, q as static input and store them in two separate variables.
• Pass the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments to the Find_Section() function.
• Create a function to say Find_Midpoint() which takes the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments and prints the point that divides the line in the given ratio p: q.
• Inside the function calculate the x coordinate of the point using the mathematical formula (q * a1)+(p * a2))/(p + q) and convert it into float using the float() function.
• Store it in a variable.
• Calculate the y coordinate of the point using the mathematical formula (q * b1)+(p * b2))/(p + q) and convert it into float using the float() function.
• Store it in another variable.
• Print the point that divides the line in the given ratio p: q.
• The Exit of the Program.

Below is the implementation:

# Create a function to say Find_Midpoint() which takes the given two points of a line
# and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments and prints the point
# that divides the line in the given ratio p: q.

def Find_Section(a1, a2, b1, b2, p, q):
# Inside the function calculate the x coordinate of the point using the mathematical
# formula (q * a1)+(p * a2))/(p + q) and convert it into float
# using the float() function.
# Store it in a variable.
x_coordinate = (float)((q * a1)+(p * a2))/(p + q)
# Calculate the y coordinate of the point using the mathematical formula
# (q * b1)+(p * b2))/(p + q) and convert it into float using the
# float() function.
# Store it in another variable.
y_coordinate = (float)((q * b1)+(p * b2))/(p + q)

# Print the point that divides the line in the given ratio p: q.
print("(", x_coordinate, ",", y_coordinate, ")")

# Give the first point as static input and store it in two variables.
a1 = 2
b1 = 4
# Give the second point as static input and store it in another two variables.
a2 = 1
b2 = 3
p = 1
q = 2
print("The point that divides the line in the given ratio (", p, ":", q, ") is :")
# Pass the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as
# the arguments to the Find_Section() function.
Find_Section(a1, a2, b1, b2, p, q)


Output:

The point that divides the line in the given ratio ( 1 : 2 ) is :
( 1.6666666666666667 , 3.6666666666666665 )

### Method #2: Using Mathematical Formula (User Input)

Approach:

• Give the first point as user input using map(),int(),split() functions and store it in two variables.
• Give the second point as user input using map(),int(),split() functions and store it in two variables.
• Give the ratio values p, q as static input and store them in two separate variables.
• Pass the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments to the Find_Section() function.
• Create a function to say Find_Midpoint() which takes the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments and prints the point that divides the line in the given ratio p: q.
• Inside the function calculate the x coordinate of the point using the mathematical formula (q * a1)+(p * a2))/(p + q) and convert it into float using the float() function.
• Store it in a variable.
• Calculate the y coordinate of the point using the mathematical formula (q * b1)+(p * b2))/(p + q) and convert it into float using the float() function.
• Store it in another variable.
• Print the point that divides the line in the given ratio p: q.
• The Exit of the Program.

Below is the implementation:

# Create a function to say Find_Midpoint() which takes the given two points of a line
# and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments and prints the point
# that divides the line in the given ratio p: q.

def Find_Section(a1, a2, b1, b2, p, q):
# Inside the function calculate the x coordinate of the point using the mathematical
# formula (q * a1)+(p * a2))/(p + q) and convert it into float
# using the float() function.
# Store it in a variable.
x_coordinate = (float)((q * a1)+(p * a2))/(p + q)
# Calculate the y coordinate of the point using the mathematical formula
# (q * b1)+(p * b2))/(p + q) and convert it into float using the
# float() function.
# Store it in another variable.
y_coordinate = (float)((q * b1)+(p * b2))/(p + q)

# Print the point that divides the line in the given ratio p: q.
print("(", x_coordinate, ",", y_coordinate, ")")

# Give the first point as user input using map(),int(),split() functions
# and store it in two variables.
a1, b1 = map(int, input(
'Enter some random first point values separated by spaces = ').split())
# Give the second point as user input using map(),int(),split() functions
# and store it in two variables.
a2, b2 = map(int, input(
'Enter some random second point values separated by spaces = ').split())
p = 2
q = 5
print("The point that divides the line in the given ratio (", p, ":", q, ") is :")
# Pass the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as
# the arguments to the Find_Section() function.
Find_Section(a1, a2, b1, b2, p, q)


Output:

Enter some random first point values separated by spaces = 5 7
Enter some random second point values separated by spaces = 2 8
The point that divides the line in the given ratio ( 2 : 5 ) is :
( 4.142857142857143 , 7.285714285714286 )

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.