写在前面
简单实现:
求线段AP在线段AB上的投影长度;
求点P在线段AB上的投影点的坐标;
判断点P的投影点是否在线段AB内;
求∠PAB的角度值;
判断∠PAB是锐角、直角,还是钝角。
求向量AP和AB组成的平行四边形的面积;
若有另外一点Q,判断向量PQ与AB是否平行;
判断点P在向量AB的左侧还是右侧。
方法有三种,向量法可以求的答案多、此外还有面积法和待定系数求解法(设pp的坐标为x,y,直接推导)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <thread>
#include <mutex>
#include <iostream>
#include <glog/logging.h>
using namespace std;
int main (int argc, char* argv[])
{
google::InitGoogleLogging(argv[0]);
google::SetStderrLogging(google::GLOG_ERROR);
FLAGS_colorlogtostderr = true;
FLAGS_logtostderr = true;
FLAGS_minloglevel = 0;
// questions
float distance_PtoAB =0;
pair<int,int> a=make_pair(1,1);
pair<int,int> b=make_pair(2,4);
pair<int,int> p=make_pair(3,3);
pair<int,int> ap=make_pair(2,2);
pair<int,int> ab=make_pair(1,3);
pair<int,int> bp=make_pair(1,-1);
pair<int,int> ba=make_pair(-1,-3);
float AP_l=sqrt(ap.first*ap.first+ap.second*ap.second);
float BP_l=sqrt(bp.first*bp.first+bp.second*bp.second);
float AB_l=sqrt(ab.first*ab.first+ab.second*ab.second);
float thetaPAB_cos= (ap.first*ab.first+ap.second*ab.second)/AP_l/AB_l;
float thetaABP_cos= (ba.first*bp.first+ba.second*bp.second)/BP_l/AB_l;
LOG(INFO) << "thetaPAB_cos: "<< thetaPAB_cos;
LOG(INFO) << "thetaABP_cos: "<< thetaABP_cos;
cout<<"thetaPAB: "<<acos(thetaPAB_cos)<<endl;
if (thetaPAB_cos<=0)
{
distance_PtoAB=sqrt(ap.first*ap.first+ap.second*ap.second);
if (thetaPAB_cos!=0)
{
cout<<"PAB is obtuse angle"<<endl;
cout<<"Project of P is outside AB"<<endl;
}
}
else if (thetaABP_cos<=0)
{
distance_PtoAB=sqrt(bp.first*bp.first+bp.second*bp.second);
if (thetaABP_cos!=0)
{
cout<<"PAB is acute angle"<<endl;
cout<<"Project of P is outside AB"<<endl;
}
}
else
{
distance_PtoAB=(ap.first)*sqrt((1-thetaPAB_cos*thetaPAB_cos));
cout<<"PAB is acute angle"<<endl;
cout<<"Project of P(PP) is inside AB"<<endl;
// cal unit vector of AB
pair <float,float> AB_unitvector (ab.first/AB_l,ab.second/AB_l);
LOG(INFO) << "AB_unitvector: "<< AB_unitvector.first<<","<<AB_unitvector.second;
float APP_l=(ap.first*ab.first+ap.second*ab.second)/AB_l;
pair <float,float> APP (APP_l*AB_unitvector.first,APP_l*AB_unitvector.second);
pair <float,float> PP (APP.first-a.first,APP.second-a.second);
cout<<"and the PP is "<< PP.first<<","<<PP.second <<endl;
}
std::cout<<"distance_PtoAB: "<<distance_PtoAB<<std::endl;
google::ShutdownGoogleLogging();
}
sin相关
求向量AP和AB组成的平行四边形的面积;
若有另外一点Q,判断向量PQ与AB是否平行;
判断点P在向量AB的左侧还是右侧。
问题比较简单 通过观察叉乘的结果就可以判断