{"id":22963,"date":"2021-09-29T20:13:44","date_gmt":"2021-09-29T14:43:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=22963"},"modified":"2021-11-22T18:35:36","modified_gmt":"2021-11-22T13:05:36","slug":"python-program-to-check-if-a-line-touches-or-intersects-a-circle","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-line-touches-or-intersects-a-circle\/","title":{"rendered":"Python Program to Check If a Line Touches or Intersects a Circle"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Pizza Cut Problem (Or Circle Division by Lines)<\/a> The line equation is in the form ax+by+c.<\/p>\n Hence a, b, c values are given for a line equation.<\/p>\n The three possibilities :<\/p>\n The following formula<\/strong> can be used to calculate the distance of a line from a point:<\/p>\n (ax+by+c)\/sqrt(a*a+b*b)<\/strong><\/p>\n If d > r,\u00a0 the line lies outside the circle. where d = the distance of a line from a center.<\/p>\n r is the radius of the circle.<\/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 line touches or intersects the circle 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 Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n In the previous article, we have discussed Python Program for Pizza Cut Problem (Or Circle Division by Lines) Given the coordinates of the center point as (0,0), the radius of the circle, and the equation of a line and the task is to check whether the given line touches or intersects the circle. The line …<\/p>\n
\nGiven the coordinates of the center point as (0,0), the radius of the circle, and the equation of a line and the task is to check whether the given line touches or intersects the circle.<\/p>\n\n
\nIf d = r,\u00a0 the line touches the circle.
\nIf d < r,\u00a0 the line intersects the circle.<\/p>\nGiven radius = 6\r\nGiven a = 2\r\nGiven b = 1\r\nGiven c = 1<\/pre>\n
The Given line intersects the circle<\/pre>\n
Given radius = 5\r\nGiven a = 1\r\nGiven b = 1\r\nGiven c = -15<\/pre>\n
The Given line is outside the circle<\/pre>\n
Program to Check If a Line Touches or Intersects a Circle in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the radius as static input and store it in a variable.\r\ngvn_radiuss = 6\r\n# Take the x-coordinate of center and initialize its value to 0.\r\nm = 0\r\n# Take the y-coordinate of center and initialize its value to 0.\r\nn = 0\r\n# Give the number as static input and store it in a variable.\r\np = 2\r\n# Give the other number as static input and store it in another variable.\r\nq = 1\r\n# Give the third number as static input and store it in another variable.\r\nr = 1\r\n# Calculate the distance of the given line from the given center point using the\r\n# above given mathematical formula, abs(), math.sqrt() functions\r\n# store it in another variable.\r\ndistancee_val = ((abs(p * m + q * n + r)) \/\r\n math.sqrt(p * p + q * q))\r\n# Check if the given radius value is equal to the above obtained distance using the\r\n# if conditional statement.\r\nif (gvn_radiuss == distancee_val):\r\n # If it is true, then print \"The Given line touches the circle\".\r\n print(\"The Given line touches the circle\")\r\n# Check if the given radius value is greater than the above obtained distance using\r\n# the elif conditional statement.\r\nelif (gvn_radiuss > distancee_val):\r\n # If it is true, then print \"The Given line intersects the circle\".\r\n print(\"The Given line intersects the circle\")\r\nelse:\r\n # Else, print \"The Given line is outside the circle\".\r\n print(\"The Given line is outside the circle\")\r\n<\/pre>\n
The Given line intersects the circle<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the radius as user input using the int(input()) function and \r\n# store it in a variable.\r\ngvn_radiuss = int(input(\"Enter some random number = \"))\r\n# Take the x-coordinate of center and initialize its value to 0.\r\nm = 0\r\n# Take the y-coordinate of center and initialize its value to 0.\r\nn = 0\r\n# Give the number as user input using the int(input()) function and store it in a variable.\r\np = int(input(\"Enter some random number = \"))\r\n# Give the other number as user input using the int(input()) function and store it in another variable.\r\nq = int(input(\"Enter some random number = \"))\r\n# Give the third number as user input using the int(input()) function and store it in another variable.\r\nr = int(input(\"Enter some random number = \"))\r\n# Calculate the distance of the given line from the given center point using the\r\n# above given mathematical formula, abs(), math.sqrt() functions\r\n# store it in another variable.\r\ndistancee_val = ((abs(p * m + q * n + r)) \/\r\n math.sqrt(p * p + q * q))\r\n# Check if the given radius value is equal to the above obtained distance using the\r\n# if conditional statement.\r\nif (gvn_radiuss == distancee_val):\r\n # If it is true, then print \"The Given line touches the circle\".\r\n print(\"The Given line touches the circle\")\r\n# Check if the given radius value is greater than the above obtained distance using\r\n# the elif conditional statement.\r\nelif (gvn_radiuss > distancee_val):\r\n # If it is true, then print \"The Given line intersects the circle\".\r\n print(\"The Given line intersects the circle\")\r\nelse:\r\n # Else, print \"The Given line is outside the circle\".\r\n print(\"The Given line is outside the circle\")\r\n<\/pre>\n
Enter some random number = 5\r\nEnter some random number = 1\r\nEnter some random number = 1\r\nEnter some random number = -15\r\nThe Given line is outside the circle<\/pre>\n
\n