{"id":22725,"date":"2021-09-27T15:43:09","date_gmt":"2021-09-27T10:13:09","guid":{"rendered":"https:\/\/python-programs.com\/?p=22725"},"modified":"2021-11-22T18:35:38","modified_gmt":"2021-11-22T13:05:38","slug":"python-program-for-section-formula-point-that-divides-a-line-in-given-ratio","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-section-formula-point-that-divides-a-line-in-given-ratio\/","title":{"rendered":"Python Program for Section Formula (Point that Divides a Line in Given Ratio)"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Mid-Point of a Line<\/a> 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.<\/p>\n Section Formula:<\/strong><\/p>\n (mx2+nx1)\/(m+n), (my2+ny1)\/(m+n)<\/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 point that divides the line in the given ratio P: Q 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 Access the big list of Python Programming Code Examples<\/a> with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.<\/p>\n 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 …<\/p>\n
\nGiven 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.<\/p>\nGiven First Point = ( 2, 4 )\r\nGiven Second Point = ( 1, 3 )\r\nGiven ratio value p= 1\r\nGiven ratio value q= 2<\/pre>\n
The point that divides the line in the given ratio ( 1 : 2 ) is :\r\n( 1.6666666666666667 , 3.6666666666666665 )<\/pre>\n
Given First Point = ( 5, 7)\r\nGiven Second Point = ( 2, 8 )\r\nGiven ratio value p= 2\r\nGiven ratio value q= 5<\/pre>\n
The point that divides the line in the given ratio ( 2 : 5 ) is :\r\n( 4.142857142857143 , 7.285714285714286 )<\/pre>\n
Program for Section Formula (Point that Divides a Line in Given Ratio) 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\r\n# and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments and prints the point\r\n# that divides the line in the given ratio p: q.\r\n\r\n\r\ndef Find_Section(a1, a2, b1, b2, p, q):\r\n # Inside the function calculate the x coordinate of the point using the mathematical\r\n # formula (q * a1)+(p * a2))\/(p + q) and convert it into float\r\n # using the float() function.\r\n # Store it in a variable.\r\n x_coordinate = (float)((q * a1)+(p * a2))\/(p + q)\r\n # Calculate the y coordinate of the point using the mathematical formula\r\n # (q * b1)+(p * b2))\/(p + q) and convert it into float using the\r\n # float() function.\r\n # Store it in another variable.\r\n y_coordinate = (float)((q * b1)+(p * b2))\/(p + q)\r\n\r\n # Print the point that\u00a0divides the line in the given ratio p: q.\r\n print(\"(\", x_coordinate, \",\", y_coordinate, \")\")\r\n\r\n\r\n# Give the first point as static input and store it in two variables.\r\na1 = 2\r\nb1 = 4\r\n# Give the second point as static input and store it in another two variables.\r\na2 = 1\r\nb2 = 3\r\np = 1\r\nq = 2\r\nprint(\"The point that divides the line in the given ratio (\", p, \":\", q, \") is :\")\r\n# Pass the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as\r\n# the arguments to the Find_Section() function.\r\nFind_Section(a1, a2, b1, b2, p, q)\r\n<\/pre>\n
The point that divides the line in the given ratio ( 1 : 2 ) is :\r\n( 1.6666666666666667 , 3.6666666666666665 )<\/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\r\n# and the ratio values i.e, a1, a2, b1, b2, p, q as the arguments and prints the point\r\n# that divides the line in the given ratio p: q.\r\n\r\n\r\ndef Find_Section(a1, a2, b1, b2, p, q):\r\n # Inside the function calculate the x coordinate of the point using the mathematical\r\n # formula (q * a1)+(p * a2))\/(p + q) and convert it into float\r\n # using the float() function.\r\n # Store it in a variable.\r\n x_coordinate = (float)((q * a1)+(p * a2))\/(p + q)\r\n # Calculate the y coordinate of the point using the mathematical formula\r\n # (q * b1)+(p * b2))\/(p + q) and convert it into float using the\r\n # float() function.\r\n # Store it in another variable.\r\n y_coordinate = (float)((q * b1)+(p * b2))\/(p + q)\r\n\r\n # Print the point that\u00a0divides the line in the given ratio p: q.\r\n print(\"(\", x_coordinate, \",\", 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\np = 2\r\nq = 5\r\nprint(\"The point that divides the line in the given ratio (\", p, \":\", q, \") is :\")\r\n# Pass the given two points of a line and the ratio values i.e, a1, a2, b1, b2, p, q as\r\n# the arguments to the Find_Section() function.\r\nFind_Section(a1, a2, b1, b2, p, q)\r\n<\/pre>\n
Enter some random first point values separated by spaces = 5 7\r\nEnter some random second point values separated by spaces = 2 8\r\nThe point that divides the line in the given ratio ( 2 : 5 ) is :\r\n( 4.142857142857143 , 7.285714285714286 )<\/pre>\n
\n