
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Check if a circle lies inside another circle or not in C++
- FabricJS – How to check if a Polygon Object Intersects with Another Object?
- Fill in the blanks :A tangent to a circle intersects it in ……….. point(s).
- Check If It Is a Straight Line in C++
- Fill in the blanks:(i) A tangent to a circle intersects it in ………… point(s).(ii) A line intersecting a circle in two points is called a ………….(iii) A circle can have parallel tangents at the most ………….(iv) The common point of a tangent to a circle and the circle is called ………...
- JAVA Program to Check if a Point is On the Left or Right Side of a Line
- Check if a line passes through the origin in C++
- 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
- How to check if Polyline object intersects with another object using FabricJS?
- How to create a line chart using ggplot2 that touches the edge in R?
- Check if a word exists in a grid or not in Python
- FabricJS – How to check if a specified control is visible in Line?
- How to check if an Image object intersects with another object using FabricJS?
- Check if a String is empty ("") or null in Java

Advertisements