问题描述
- 运行不成功,这是求点到直线的 距离
-
#include"iostream.h"
#include"math.h"class line;
class point()
{
private:
double x,y;
public:
point (double x1,double y1)
{
x=x1;
y=y1;} friend double dist(point,line);
};
class line()
{
private:
double a,b,c;
public:
line(double a1,double b1,double c1)
{
a=a1;
b=b1;
c=c1;
}
friend double dist(point,line);
};double dist(point p1,point p2)
{
double d,d1,d2;
d1=p2.a*p1.x+p2.b*p1.y+p2.c;
d2=sqrt(p2.a*p2.a+p2.b*p2.b);
d=fabs(d1/d2);
return d;}
void main()
{
point p1(4,5);
line p2(1,2,4);
cout<<dist(p1,p2)<<endl;}
解决方案
1:类point,line声明时后面有括号吗?
2:函数dist实现时第2个参数类型是line,不是point
3:建议传递类,结构等类型的参数时使用引用(&)。
愚见,你试试。
解决方案二:
我就改通了lz的程序,至于逻辑对不对我没有仔细看
#include"iostream.h"
#include"math.h"
class line;
class point
{
private:
double x,y;
public:
point (double x1,double y1)
{
x=x1;
y=y1;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
friend double dist(point,line);
};
class line
{
private:
double a,b,c;
public:
line(double a1,double b1,double c1)
{
a=a1;
b=b1;
c=c1;
}
double getA()
{
return a;
}
double getB()
{
return b;
}
double getC()
{
return c;
}
friend double dist(point,line);
};
double dist(point *p1,line *p2)
{
double d,d1,d2;
d1=p2->getA()*p1->getX()+p2->getB()*p1->getY()+p2->getC();
d2=sqrt(p2->getA()*p2->getA()+p2->getB()*p2->getB());
d=fabs(d1/d2);
return d;
}
int main()
{
point *p1 = new point(4,5);
line *p2 = new line(1,2,4);
cout<<dist(p1,p2)<<endl;
return 0;
}
解决方案三:
求点到直线的距离
点到直线的距离
arengine 点到直线的距离
解决方案四:
没有这么复杂吧楼上。。。
解决方案五:
#include"iostream.h"
#include"math.h"
class line;
class point
{
private:
double x,y;
public:
point (double x1,double y1)
{
x=x1;
y=y1;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
friend double dist(point,line);
};
class line
{
private:
double a,b,c;
public:
line(double a1,double b1,double c1)
{
a=a1;
b=b1;
c=c1;
}
double getA()
{
return a;
}
double getB()
{
return b;
}
double getC()
{
return c;
}
friend double dist(point,line);
};
double dist(point *p1,line *p2)
{
double d,d1,d2;
d1=p2->getA()*p1->getX()+p2->getB()*p1->getY()+p2->getC();
d2=sqrt(p2->getA()*p2->getA()+p2->getB()*p2->getB());
d=fabs(d1/d2);
return d;
}
int main()
{
point *p1 = new point(4,5);
line *p2 = new line(1,2,4);
cout<<dist(p1,p2)<<endl;
return 0;
}
解决方案六:
#include"iostream.h"
#include"math.h"
class line;
class point
{
private:
double x,y;
public:
point (double x1,double y1)
{
x=x1;
y=y1;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
friend double dist(point,line);
};
class line
{
private:
double a,b,c;
public:
line(double a1,double b1,double c1)
{
a=a1;
b=b1;
c=c1;
}
double getA()
{
return a;
}
double getB()
{
return b;
}
double getC()
{
return c;
}
friend double dist(point,line);
};
double dist(point *p1,line *p2)
{
double d,d1,d2;
d1=p2->getA()*p1->getX()+p2->getB()*p1->getY()+p2->getC();
d2=sqrt(p2->getA()*p2->getA()+p2->getB()*p2->getB());
d=fabs(d1/d2);
return d;
}
int main()
{
point *p1 = new point(4,5);
line *p2 = new line(1,2,4);
cout<<dist(p1,p2)<<endl;
return 0;
}