本文目錄一覽:
c語言編程
第一個直線方程y = a*x + b
第二個直線方程y = c*x + d
點的坐標(x,y)
#includestdio.h
#includemath.h
void xiangJiao(int a, int b, int c, int d);
void pingXing(int a, int b, int c, int d);
void dianDaoXian(int x, int y, int a, int b);
int main()
{
// 下班了。。。調用你自己寫把,有不明白的uewing@126.com找我
// 調用要判斷直線是否相交,是否平行等等
getch();
return 0;
}
void xiangJiao(int a, int b, int c, int d)
{
printf(“相交點x坐標:%d\n”, (d – b) / (a – c) );
printf(“相交點y坐標:%d\n”, ((d – b) / (a – c))*a + b );
}
void pingXing(int a, int b, int c, int d)
{
printf(“兩直線距離:%d”, abs(d – b) );
}
void dianDaoXian(int x, int y, int a, int b)
{
int newA, newB, jiaodianX, jiaodianY;
// 求出垂直直線方程
newA = -(1/a);
newB = y + (1/a)*x;
// 求出新直線與原直線焦點
jiaodianX = (newB – b) / (a – newA);
jiaodianY = ((newB – b) / (a – newA))*a + b;
// 求出距離
int distance, tempX, tempY;
tempY = jiaodianY – y;
tempX = jiaodianX – x;
distance = sqrt(tempY*tempY + tempX*tempX);
printf(“點到直線距離為:%d\n”, distance);
}
誰能提供,點到直線的距離公式 C語言程序
設點(x0,y0) ,直線為ax+by+c=0
則距離為fabs(ax0+by0+c)/sqrt(a*a+b*b)
前提是在文件前面加上#include math.h
求一C++程序:計算點到直線的距離?
在Windows XP+VC++6.0下編譯通過並正常運行
我的測試程序是用默認參數值初始化的
當然,你可以改為:
Point P(2,3);
Line L(1,2,3);
其它的都不變
懂了么?
#includeiostream
#includecmath
using namespace std;
class Line;//聲明類Line,因為Point類中聲明友元函數friend dist(Point P,Line L)用到該類
class Point
{
private:
double x;
double y;
public:
Point(double xx=0,double yy=0)
{
x=xx;
y=xx;
}
friend double dist(Point P,Line L);
};
class Line
{
private:
double a;
double b;
double c;
public:
Line(double aa=1,double bb=1,double cc=1)
{
a=aa;
b=bb;
c=cc;
}
friend double dist(Point P,Line L);
};
double dist(Point P,Line L)
{
double s;
s=(L.a*P.x+L.b*P.y+L.c)/sqrt(L.a*L.a+L.b*L.b);
if(s0)
return s;
else
return -s;
}
int main()
{
Point P;//這相當於 Point P(0,0);
Line L;//相當於 Line L(1,1,1);
coutdist(P,L)endl;
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/184003.html