1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <cmath>
using namespace std;
bool isChord( double a, double b, double c, double x0, double y0, double R, double &length )
{
double temp = a * x0 + b * y0 + c;
double dsq = temp * temp / ( a * a + b * b ); // (square of) distance from centre to chord
if ( dsq > R * R ) return false; // not a chord
length = 2.0 * sqrt( R * R - dsq ); // by Pythagoras
return true;
}
int main()
{
double x0, y0, R, a, b, c, length;
cout << "Circle, centre (x0,y0), radius R. Enter x0 y0 R: "; cin >> x0 >> y0 >> R;
cout << "Line a x + b y + c = 0. Enter a b c: "; cin >> a >> b >> c;
if ( isChord( a, b, c, x0, y0, R, length ) ) cout << "Line segment is a chord, with length " << length << '\n';
else cout << "Line misses the circle\n";
}
| |