题干解读:给出了一个点和一条直线的位置信息,要求输出他们的举例
思路:先由直线中两点确定这个直线的一般式,再由一般式来写两点间的距离公式,最后return即可.
#include <bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(double A,double B){
x=A,y=B;
}
point() = default;
};
struct line{
point point_A,point_B;
line(point A,point B){
point_A = A,point_B = B;
}
line() = default;
};
double getDistance(point P, line L){
double C1,C2,C3;//分别对应直线x前,y前系数和常数.
double dis;
C1 = (double)(L.point_A.y -L.point_B.y);
C2 = (double)(-(L.point_A.x - L.point_B.x));
C3 = -C2*L.point_A.y-C1*L.point_A.x;
dis = fabs(C1*P.x +C2*P.y+C3)/sqrt(C1*C1 + C2*C2);
return dis;
}
int main(){
int a, b, sx, sy, tx, ty;
cin >> a >> b >> sx >> sy >> tx >> ty;
point A(sx, sy), B(tx, ty), C(a, b);
line L(A, B);
printf("%.2lf", getDistance(C, L));
return 0;
}

京公网安备 11010502036488号