Circle Geometry Fundamentals
A circle is the set of points at a fixed distance r from a center C; its disk also contains the interior. Circle geometry combines distance predicates with constructions that usually need floating point. This program classifies a point, calculates disk area and circumference, and finds the intersections of a circle boundary with an infinite line. It treats tangency as one point and avoids a quadratic formula with large canceling coefficients.
Prerequisites: Vectors; Dot Product; point-to-line projection; Pythagoras.
Build the idea
Represent and classify
Store the center as a Point and radius as a nonnegative long double. A point is inside, on or outside the circle according to whether its distance to C is below, equal to or above r. The implementation uses an absolute 10^−10 distance tolerance for moderate coordinates; values within that band are reported as boundary rather than claiming exact floating equality.
Recall measurements
Circumference is 2πr and disk area is πr². Obtain π as acos(−1) rather than relying on a nonstandard M_PI macro. A zero-radius circle is a single point with zero area and circumference; it can still meet a valid line in one point. Negative radii are rejected as invalid.
Project the center onto the line
Normalize B−A to a unit direction v and compute H=A+v·dot(C−A,v), the perpendicular foot from C. Let d=|H−C|. If d>r there is no intersection; d=r is tangency at H. Distinct endpoints are needed to define the line. The projection uses no slope and works for any line direction.
Construct the two points
When d<r, a right triangle gives the offset h=√(r²−d²). The intersections are H−h·v and H+h·v. Tiny negative radicands caused by rounding are clamped to zero only after geometric feasibility has been tested. Sorting the two constructed points lexicographically gives deterministic output independent of the line endpoint order.
Work the sample and limits
For center (0,0),radius 5 and line y=3, H=(0,3), d=3 and h=4, giving (−4,3),(4,3). The point (0,0) is inside. Coordinates and radius are bounded by 10^4; six decimals are approximate. Very small separations or near-tangency within the tolerance cannot be classified with exact certainty by this floating model.
C++17 implementation
This is a complete program. Compile with C++17 and enter the input described below.
#include<iostream>
#include<cmath>
#include<iomanip>
#include<algorithm>
struct Point{
long double x,y;
Point operator+(Point p)const{
return {x+p.x,y+p.y};
}
Point operator-(Point p)const{
return {x-p.x,y-p.y};
}
Point operator*(long double k)const{
return {x*k,y*k};
}
};
long double dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
int main(){
Point c,p,a,b;
long double r;
std::cin>>c.x>>c.y>>r>>p.x>>p.y>>a.x>>a.y>>b.x>>b.y;
if(r<0){
std::cout<<"INVALID RADIUS\n";
return 0;
}
const long double eps=1e-10L;
long double dist=std::hypotl(p.x-c.x,p.y-c.y);
std::cout<<(dist<r-eps?"INSIDE":dist>r+eps?"OUTSIDE":"BOUNDARY")<<'\n';
std::cout<<std::fixed<<std::setprecision(6)<<std::acos(-1.0L)*r*r<<' '
<<2*std::acos(-1.0L)*r<<'\n';
Point v=b-a;
long double norm=std::hypotl(v.x,v.y);
if(norm==0){
std::cout<<"INVALID LINE\n";
return 0;
}
// Project the center onto the infinite line before constructing chord offsets.
v=v*(1/norm);
Point foot=a+v*dot(c-a,v);
long double d=std::hypotl(foot.x-c.x,foot.y-c.y);
if(d>r+eps){
std::cout<<0<<'\n';
return 0;
}
long double h=std::sqrt(std::max(0.0L,r*r-d*d));
if(std::fabs(d-r)<=eps){
std::cout<<1<<'\n'<<foot.x<<' '<<foot.y<<'\n';
return 0;
}
Point x=foot-v*h,y=foot+v*h;
if(x.x>y.x||(x.x==y.x&&x.y>y.y)){
std::swap(x,y);
}
std::cout<<2<<'\n'<<x.x<<' '<<x.y<<'\n'<<y.x<<' '<<y.y<<'\n';
}
Complexity analysis
O(1) time and auxiliary space. A fixed number of projections, distance calculations and square roots constructs at most two points.
Example input and output
Read cx cy r px py ax ay bx by as finite numbers, |coordinate|≤10^4 and 0≤r≤10^4. Print point classification, disk area/circumference, then intersection count and coordinates. Identical line endpoints produce INVALID LINE.
Input
0 0 5 0 0 -10 3 10 3
Output
INSIDE 78.539816 31.415927 2 -4.000000 3.000000 4.000000 3.000000
The 3–4–5 triangle gives horizontal offsets ±4 from the perpendicular foot (0,3).
Practical applications
Use for line-circle crossings, visibility constraints, chords and geometric construction. Circle-circle intersection reduces to a similar base-plus-perpendicular-offset calculation. Segment-circle intersection would additionally constrain the line parameter.
Check your understanding
Where is the tangent point when line distance equals radius?
Put it into practice
Test y=5, y=6 and radius zero with lines through and away from the center.
Show a hint
Expect one, zero, and a point-dependent intersection count.