

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a line touches or intersects a circle in C++
Suppose we have a circle and another straight line. Our task is to find if the line touches the circle or intersects it, otherwise, it passes through outside. So there are three different cases like below −
Here we will solve it by following steps. These are like below −
- Find perpendicular P between the center and given a line
- Compare P with radius r −
- if P > r, then outside
- if P = r, then touches
- otherwise inside
To get the perpendicular distance, we have to use this formula (a center point is (h, k))
$$\frac{ah+bk+c}{\sqrt{a^2+b^2}}$$
Example
#include <iostream> #include <cmath> using namespace std; void isTouchOrIntersect(int a, int b, int c, int h, int k, int radius) { int dist = (abs(a * h + b * k + c)) / sqrt(a * a + b * b); if (radius == dist) cout << "Touching the circle" << endl; else if (radius > dist) cout << "Intersecting the circle" << endl; else cout << "Outside the circle" << endl; } int main() { int radius = 5; int h = 0, k = 0; int a = 3, b = 4, c = 25; isTouchOrIntersect(a, b, c, h, k, radius); }
Output
Touching the circle
- Related Questions & Answers
- Check if a circle lies inside another circle or not in C++
- Check If It Is a Straight Line in C++
- Check if a line passes through the origin in C++
- How to create a line chart using ggplot2 that touches the edge in R?
- C++ Program to Check if a Point d lies inside or outside a circle defined by Points a, b, c in a Plane
- C++ Program to Check if a Given Set of Three Points Lie on a Single Line or Not
- Check if two line segments intersect
- Check if a word exists in a grid or not in Python
- Check if a given graph is tree or not
- How to check if a field in MongoDB is [] or {}?
- Check if a number is jumbled or not in C++
- Check if input is a number or letter in JavaScript?
- Check if a Tree is Isomorphic or not in C++
- Check if a string is Isogram or not in Python
- Check if a Java ArrayList contains a given item or not
Advertisements