Python Program to Find the Center of the Circle using Endpoints of Diameter

In the previous article, we have discussed Python Program for Area of Square Circumscribed by Circle
Given two endpoints (x1, y1) and (x2, y2) of circles diameter, the task is to find the center of the circle for the given diameter endpoints in python.

Formula:

The center of the circle can be calculated by using the midpoint formula.

Midpoint(M) = ((x 1Â + xÂ 2) / 2, (yÂ 1Â + yÂ 2) / 2)

Examples:

Example1:

Input:

Given First Point = ( 2, 4 )
Given Second Point = ( 6, -1)

Output:

The center of the circle for the given diameter endpoints is:
( 4 , 1 )

Example2:

Input:

Given First Point = ( 5, 2 )
Given Second Point = ( 1, 4 )

Output:

The center of the circle for the given diameter endpoints is:
( 3 , 3 )

Program to Find the Center of the Circle using Endpoints of Diameter in Python

Below are the ways to find the center of the circle for the given diameter endpoints 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.
• Pass the given two endpoints of a diameter i.e, a1, a2, b1, b2 as the arguments to the FindCircle_Center() function.
• Create a function to say FindCircle_Center() which takes the given two endpoints of a diameter of circle i.e, a1, a2, b1, b2 as the arguments, and prints the center of the given circle.
• Calculate the x coordinate of the center of the given circle using the above given mathematical formula and store it in a variable.
• Calculate the y coordinate of the center of the given circle using the above given mathematical formula and store it in another variable.
• Print the center of the circle for the given diameter endpoints.
• The Exit of the Program.

Below is the implementation:

# Create a function to say FindCircle_Center() which takes the given two endpoints of
# a diameter of circle i.e, a1, a2, b1, b2 as the arguments, and prints the center
# of the given circle.

def FindCircle_Center(a1, a2, b1, b2):
# Calculate the x coordinate of the center of the given circle using the above
# given mathematical formula and store it in a variable.

mid_x_Coordinate = int((a1 + a2) / 2)
# Calculate the y coordinate of the center of the given circle using the above given
# mathematical formula and store it in another variable.

mid_y_Coordinate = int((b1 + b2) / 2)
# Print the center of the circle for the given diameter endpoints.
print("(", mid_x_Coordinate, ",", mid_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 = 6
b2 = -1
print("The center of the circle for the given diameter endpoints is:")
# Pass the given two endpoints of a diameter i.e, a1, a2, b1, b2 as the arguments to the
# FindCircle_Center() function.
FindCircle_Center(a1, a2, b1, b2)


Output:

The center of the circle for the given diameter endpoints is:
( 4 , 1 )

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.
• Pass the given two endpoints of a diameter i.e, a1, a2, b1, b2 as the arguments to the FindCircle_Center() function.
• Create a function to say FindCircle_Center() which takes the given two endpoints of a diameter of circle i.e, a1, a2, b1, b2 as the arguments, and prints the center of the given circle.
• Calculate the x coordinate of the center of the given circle using the above given mathematical formula and store it in a variable.
• Calculate the y coordinate of the center of the given circle using the above given mathematical formula and store it in another variable.
• Print the center of the circle for the given diameter endpoints.
• The Exit of the Program.

Below is the implementation:

# Create a function to say FindCircle_Center() which takes the given two endpoints of
# a diameter of circle i.e, a1, a2, b1, b2 as the arguments, and prints the center
# of the given circle.

def FindCircle_Center(a1, a2, b1, b2):
# Calculate the x coordinate of the center of the given circle using the above
# given mathematical formula and store it in a variable.

mid_x_Coordinate = int((a1 + a2) / 2)
# Calculate the y coordinate of the center of the given circle using the above given
# mathematical formula and store it in another variable.

mid_y_Coordinate = int((b1 + b2) / 2)
# Print the center of the circle for the given diameter endpoints.
print("(", mid_x_Coordinate, ",", mid_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())
print("The center of the circle for the given diameter endpoints is:")
# Pass the given two endpoints of a diameter i.e, a1, a2, b1, b2 as the arguments to the
# FindCircle_Center() function.
FindCircle_Center(a1, a2, b1, b2)


Output:

Enter some random first point values separated by spaces = 5 2
Enter some random second point values separated by spaces = 1 4
The center of the circle for the given diameter endpoints is:
( 3 , 3 )

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.