{"id":22684,"date":"2021-09-27T15:43:07","date_gmt":"2021-09-27T10:13:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=22684"},"modified":"2021-11-22T18:35:38","modified_gmt":"2021-11-22T13:05:38","slug":"python-program-to-find-the-mid-point-of-a-line","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-mid-point-of-a-line\/","title":{"rendered":"Python Program to Find the Mid-Point of a Line"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Line Passing Through 2 Points<\/a> The formula for the midpoint of a line:<\/strong><\/p>\n Let the two points of a line are (x1, y2) and (x2, y2).<\/p>\n The formula for midpoint is:<\/p>\n Midpoint = ((x1<\/sub>+x2<\/sub>)\/2 , (y1<\/sub>+y2<\/sub>)\/2)<\/strong><\/p><\/blockquote>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the midpoint of the given line in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners<\/a>. Using them you can develop code on your own and master coding skills.<\/p>\n In the previous article, we have discussed Python Program to Find Line Passing Through 2 Points Given two points of a line (which are the start and endpoints) and the task is to find the midpoint of the given line in python. The formula for the midpoint of a line: Let the two points of …<\/p>\n
\nGiven two points of a line (which are the start and endpoints) and the task is to find the midpoint of the given line in python.<\/p>\nGiven First Point = ( 3 , 1 )\r\nGiven Second Point = ( 4 , 5 )<\/pre>\n
The Midpoint of the given line is:\r\n( 3 , 3 )<\/pre>\n
Given First Point = ( 6 , -2 )\r\nGiven Second Point = ( 7, 4 )<\/pre>\n
The Midpoint of the given line is:\r\n( 6 , 1 )<\/pre>\n
Program to Find the Mid-Point of a Line in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Create a function to say Find_Midpoint() which takes the given two points of a line i.e,\r\n# a1, a2, b1, b2 as the arguments, and prints the midpoint of the given line.\r\n\r\n\r\ndef Find_Midpoint(a1, a2, b1, b2):\r\n # Calculate the x coordinate of the midpoint of the given line using the above\r\n # given mathematical formula and store it in a variable.\r\n mid_x_Coordinate = (a1 + a2) \/\/ 2\r\n # Calculate the y coordinate of the midpoint of the given line using the above\r\n # given mathematical formula and store it in another variable.\r\n mid_y_Coordinate = (b1 + b2) \/\/ 2\r\n # Print the midpoint of the given line.\r\n print(\"(\", mid_x_Coordinate, \",\", mid_y_Coordinate, \")\")\r\n\r\n\r\n# Give the first point as static input and store it in two variables.\r\na1 = 3\r\nb1 = 1\r\n# Give the second point as static input and store it in another two variables.\r\na2 = 4\r\nb2 = 5\r\nprint(\"The Midpoint of the given line is:\")\r\n# Pass the given two points of a line i.e, a1, a2, b1, b2 as the arguments to the\r\n# Find_Midpoint() function.\r\nFind_Midpoint(a1, a2, b1, b2)\r\n<\/pre>\n
The Midpoint of the given line is:\r\n( 3 , 3 )<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Create a function to say Find_Midpoint() which takes the given two points of a line i.e,\r\n# a1, a2, b1, b2 as the arguments, and prints the midpoint of the given line.\r\n\r\n\r\ndef Find_Midpoint(a1, a2, b1, b2):\r\n # Calculate the x coordinate of the midpoint of the given line using the above\r\n # given mathematical formula and store it in a variable.\r\n mid_x_Coordinate = (a1 + a2) \/\/ 2\r\n # Calculate the y coordinate of the midpoint of the given line using the above\r\n # given mathematical formula and store it in another variable.\r\n mid_y_Coordinate = (b1 + b2) \/\/ 2\r\n # Print the midpoint of the given line.\r\n print(\"(\", mid_x_Coordinate, \",\", mid_y_Coordinate, \")\")\r\n\r\n\r\n# Give the first point as user input using map(),int(),split() functions\r\n# and store it in two variables.\r\na1, b1 = map(int, input(\r\n 'Enter some random first point values separated by spaces = ').split())\r\n# Give the second point as user input using map(),int(),split() functions\r\n# and store it in two variables.\r\na2, b2 = map(int, input(\r\n 'Enter some random second point values separated by spaces = ').split())\r\nprint(\"The Midpoint of the given line is:\")\r\n# Pass the given two points of a line i.e, a1, a2, b1, b2 as the arguments to the\r\n# Find_Midpoint() function.\r\nFind_Midpoint(a1, a2, b1, b2)\r\n<\/pre>\n
Enter some random first point values separated by spaces = 6 -2\r\nEnter some random second point values separated by spaces = 7 4\r\nThe Midpoint of the given line is:\r\n( 6 , 1 )<\/pre>\n
\n