用面向对象的思维设计相关类,从而实现直线与直线、直线与圆、直线与矩形的交点。
基本思想:
直线与直线,直线与圆:利用公式直接求解
直线与矩形:分解成直线与直线的交点:
#include <iostream>
#include <math.h>
class VecPosition//坐标
{
public:
double mx;
double my;
public:
void setpoint(double x,double y)
{
mx = x;
my = y;
}
};
class Line直线
{
public:
double a;
double b;//ax+by+c=0
double c;
public:
void setLine(double ma,double mb,double mc)
{
a=ma;
b=mb;
c=mc;
}
};
class Circle圆
{
public:
VecPosition
center;
double R;
public :
void setcircle(VecPosition my_center,
double my_R)
{
center = my_center;
R = my_R;
}
};
class Rectangel矩形
{
public:
VecPosition top;
VecPosition bottem;
public:
void setRectangel(VecPosition
mytop,VecPosition mybottom)
{
top=mytop;
bottem=mybottom;
}
};
using namespace std;
直线与直线的交点//
VecPosition
LinecrossLine(Line l1,Line l2)
{
VecPosition cross;
double k1=(-1)*l1.a/l1.b;
double k2=(-1)*l2.a/l2.b;
double d;
if(k1==k2)
{
cout<<"没有交点"<<endl;
return cross;
}
else
{
d=l1.a*l2.b - l2.a*l1.b;
cross.mx = (l1.b*l2.c-l2.b*l1.c)/d;
cross.my = (l1.c*l2.a-l2.c*l1.a)/d;
return cross;
}
}
直线与圆的交点//
void LinecrossCircle(Line
l,Circle c)
{
double k;
double b;
double dc;//直线与与圆心的距离
double x1,y1,x2,y2;