QT计算圆与直线关系
circle.h:
#ifndef CIRCLE_H
#define CIRCLE_H
#include "line.h"
#include "circle.h"
#include<math.h>
class line;
class circle
{
public:
circle(double xx,double yy,double rr);
double getX();
double getY();
double getR();
void judge(line &l,circle &c);
private:
double x,y,r;
};
#endif // CIRCLE_H
line.h:
#ifndef LINE_H
#define LINE_H
#include "line.h"
#include "circle.h"
#include<math.h>
class line
{
public:
line(double kk,double bb);
double distant(circle &c);
private:
double k;
double b;
};
#endif // LINE_H
circle.cpp:
#include "line.h"
#include "circle.h"
#include<math.h>
#include<QDebug>
circle::circle(double xx,double yy,double rr)
:x(xx),
y(yy),
r(rr)
{
}
double circle::getX()
{
return x;
}
double circle:: getY()
{
return y;
}
double circle::getR()
{
return r;
}
void circle:: judge(line &l,circle &c)
{
double d=l.distant(c);
if(d>r)
qDebug()<<"圆与直线不相交!"<<endl;
else if(d==r)
qDebug()<<"圆与直线相切!"<<endl;
else
qDebug()<<"圆与直线相交!"<<endl;
}
line .cpp:
#include "line.h"
#include "circle.h"
#include<math.h>
line::line(double kk,double bb)
{
k=kk;
b=bb;
}
double line::distant(circle &c)
{
double x=c.getX();
double y=c.getY();
double a,f,d;
a=k*x+b+y;
f=k*k+1;
d=sqrt(a/f);
return d;
}
main.cpp:
#include <QCoreApplication>
#include "line.h"
#include "circle.h"
#include<math.h>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
circle c(0,0,1);
line l(1,3);
c.judge(l,c);
cout<<"r="<<c.getR()<<endl;
cout<<"d="<<l.distant(c)<<endl;
return 0;
return a.exec();
}