当前位置:   article > 正文

1.C++实现:计算点 C 到一条线 AB 的距离_c++垂足计算

c++垂足计算
#include <iostream>
#include <algorithm>

using namespace std;
double calDistance(double x1, double x2, double y1, double y2){
    double s1 = pow((x1 - x2),2) + pow((y1 - y2), 2);
    return sqrt(s1);
}
void test(){
    double C_x(3), C_y(0), A_x(0), A_y(0), B_x(2), B_y(2);
    double distance_a = calDistance(C_x, A_x, C_y, A_y);
    double distance_b = calDistance(A_x, B_x, A_y, B_y);
    double distance_c = calDistance(C_x, B_x, C_y, B_y);
    double s = (distance_a + distance_b + distance_c)/2;

    double area_tmp = s * (s - distance_a) * (s - distance_b) * (s - distance_c);
    area_tmp = area_tmp < 0 ? -area_tmp : area_tmp;

    double triangle_area = sqrt(area_tmp);

    double x_error = 2 * triangle_area / distance_b;
    cout << x_error << endl;
}
int main(){
	test();
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/562793
推荐阅读
相关标签
  

闽ICP备14008679号