{"id":22657,"date":"2021-09-27T08:36:58","date_gmt":"2021-09-27T03:06:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=22657"},"modified":"2021-11-22T18:35:38","modified_gmt":"2021-11-22T13:05:38","slug":"python-program-to-check-if-three-points-are-collinear","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-three-points-are-collinear\/","title":{"rendered":"Python Program to Check if Three Points are Collinear"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of Series 1^1\/1!+2^2\/2!+3^3\/3!…+n^n\/n!<\/a> Collinear Points:<\/strong><\/p>\n Collinear points are those that are located along the same straight line or in a single line. In Euclidean geometry, two or more points on a line that is close to or far from each other are said to be collinear.<\/p>\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 check whether the given three points are collinear or not 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 If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners<\/a> as our expert team has designed them from scratch.<\/p>\n In the previous article, we have discussed Python Program to Find Sum of Series 1^1\/1!+2^2\/2!+3^3\/3!…+n^n\/n! Given three points the task is to check whether the given three points are collinear or not in Python. Collinear Points: Collinear points are those that are located along the same straight line or in a single line. In Euclidean …<\/p>\n
\nGiven three points the task is to check whether the given three points are collinear or not in Python.<\/p>\nGiven First Point = ( 1 , 1 )\r\nGiven Second Point = ( 1 , 4 )\r\nGiven Third Point = ( 1, 5 )<\/pre>\n
The points ( 1 , 1 ) ( 1 , 4 ) ( 1 , 5 ) are collinear and lies on the straight line<\/pre>\n
Given First Point = ( 7 , 0 )\r\nGiven Second Point = ( 8, 9 )\r\nGiven Third Point = ( 1, 2 )<\/pre>\n
The points ( 7 , 0 ) ( 8 , 9 ) ( 1 , 2 ) are not collinear<\/pre>\n
Program to Check if Three Points are Collinear in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Give the first point as static input and store it in two variables.\r\na1 = 1\r\nb1 = 1\r\n# Give the second point as static input and store it in another two variables.\r\na2 = 1\r\nb2 = 4\r\n# Give the third point as static input and store it in another two variables.\r\na3 = 1\r\nb3 = 5\r\n# Calculate the area of these three points(triangle) and store it in a variable.\r\n# To avoid floating point computations, we avoided multiplication with 0.5.\r\nareaofPts = a1 * (b2 - b3) + a2 * (b3 - b1) + a3 * (b1 - b2)\r\n# Check the above-calculated area is equal to 0 or not using the If conditional statement.\r\nif(areaofPts == 0):\r\n # If it is true then the given three points are collinear and lie in the straight line.\r\n print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,\r\n ',', b3, ')', 'are collinear and lies on the straight line ')\r\nelse:\r\n # Else the given three points are not collinear.\r\n print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,\r\n ',', b3, ')', 'are not collinear ')\r\n<\/pre>\n
The points ( 1 , 1 ) ( 1 , 4 ) ( 1 , 5 ) are collinear and lies on the straight line<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\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\n# Give the third point as user input using map(),int(),split() functions\r\n# and store it in two variables.\r\na3, b3 = map(int, input(\r\n 'Enter some random third point values separated by spaces = ').split())\r\n# Calculate the area of these three points(triangle) and store it in a variable.\r\n# To avoid floating point computations, we avoided multiplication with 0.5.\r\nareaofPts = a1 * (b2 - b3) + a2 * (b3 - b1) + a3 * (b1 - b2)\r\n# Check the above-calculated area is equal to 0 or not using the If conditional statement.\r\nif(areaofPts == 0):\r\n # If it is true then the given three points are collinear and lie in the straight line.\r\n print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,\r\n ',', b3, ')', 'are collinear and lies on the straight line ')\r\nelse:\r\n # Else the given three points are not collinear.\r\n print('The points (', a1, ',', b1, ') (', a2, ',', b2, ') (', a3,\r\n ',', b3, ')', 'are not collinear ')\r\n<\/pre>\n
Enter some random first point values separated by spaces = 7 0\r\nEnter some random second point values separated by spaces = 8 9\r\nEnter some random third point values separated by spaces = 1 2\r\nThe points ( 7 , 0 ) ( 8 , 9 ) ( 1 , 2 ) are not collinear<\/pre>\n
\n