在三角形cMN中,根據面積相等有:
vim calPoint2LineDistance.cpp#include<iostream>#include<stdlib.h> //abs()#include<math.h> //sqrt()
using namespace std;
int main(int argc, char* argv[]){ cout<<"argc is:"<<argc<<endl; if(argc!=7){ cout<<"need at least three points"<<endl; return 0; } float x1,y1,x2,y2,x3,y3; x1 = *argv[1], y1 = *argv[2], x2 = *argv[3], y2 = *argv[4], x3 = *argv[5], y3 = *argv[6]; if(x1 == x2){ cout<<"vertical,point2line distance is:"<<abs(x3-x1)<<endl; return abs(x3-x1); } else{ float d = abs((y1-y2)*x3+(x2-x1)*y3+(x1*y2-y1*x2))/sqrt(pow(y1-y2,2)+pow(x2-x1,2)); cout<<"point2line distance is:"<<d<<endl; return d; }}g++ calPoint2LineDistance.cpp -o calPoint2LineDistance.out -o 編譯選項為將產生的可執行文件用指定的文件名,此處指定的是calPoint2LineDistance.out,不指定的話,默認是a.out。./calPoint2LineDistance.out 2 0 0 2 2 2argc is:7point2line distance is:1.41421連結: https://pan.baidu.com/s/1LOoCcAwwXfF9MVFtEoQIaA 密碼: 24a9