您当前的位置: 首页 >  c++

插件开发

暂无认证

  • 2浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++-几何计算-求垂足-单位向量-点旋转角度

插件开发 发布时间:2022-05-25 08:16:47 ,浏览量:2

文章目录
    • 1.APlaneGeometryCompute.h
    • 2.APlaneGeometryCompute.cpp
    • 3.读者答疑
  在开发平面几何方面的软件时,经常遇到的问题是点,角和线的运算,作者根据自己平时遇到的问题,梳理了几个常用的函数,供需要的读者使用,使用C++开发,分为头文件和实现文件,APlaneGeometryCompute.h和APlaneGeometryCompute.cpp.

1.APlaneGeometryCompute.h
#pragma once
class APlaneGeometryCompute
{
public:
	APlaneGeometryCompute(void);
	~APlaneGeometryCompute(void);
};

class AJZPoint2D{
public:
	AJZPoint2D(double _x,double _y){
		x=_x;
		y=_y;
	}
	~AJZPoint2D(){

	}
	AJZPoint2D(){
		x=0;
		y=0;
	}

	//单位向量
	AJZPoint2D UnitVector();

	//两点相加
	AJZPoint2D operator+(AJZPoint2D pt){
		AJZPoint2D rltPt;
		rltPt.x=this->x+pt.x;
		rltPt.y=this->y+pt.y;
		return rltPt;
	}
	AJZPoint2D operator-(AJZPoint2D pt){
		AJZPoint2D rltPt;
		rltPt.x=this->x-pt.x;
		rltPt.y=this->y-pt.y;
		return rltPt;
	}
	AJZPoint2D operator*(double s){
		AJZPoint2D rltPt;
		rltPt.x=this->x*s;
		rltPt.y=this->y*s;
		return rltPt;
	}
	AJZPoint2D operator/(double s){
		AJZPoint2D rltPt;
		rltPt.x=this->x/s;
		rltPt.y=this->y/s;
		return rltPt;
	}

	//绕原点的逆时针方向角
	double ClockAngleByOrg();
public:
	double x;
	double y;
};

//角度转弧度
double Degree2Radius(double d);
//弧度转角度
double Radius2Degree(double r);

//求点到线段的垂足
AJZPoint2D GetFootPoint(AJZPoint2D point, AJZPoint2D pnt1, AJZPoint2D pnt2);
//求点到线段的距离
double GetDistancePoineToLine_planeCoord(AJZPoint2D point, AJZPoint2D pnt1, AJZPoint2D pnt2);

//求3点确定的前两点表示矩形的另外两个点,并返回前两点朝另外两点的法向量
AJZPoint2D GetRectOtherTwoPoints(
	AJZPoint2D linePt1,AJZPoint2D linePt2,AJZPoint2D directPt3,AJZPoint2D& rectPt4,AJZPoint2D& rectPt5);

//点沿单位向量前进距离dis
AJZPoint2D PointExtendDis(AJZPoint2D pt,AJZPoint2D vec,double dis);

//一点绕另外一点逆时针旋转
AJZPoint2D RotatePoint(AJZPoint2D centerPt,AJZPoint2D srcPt,double angle_degree);

//求线段的垂直向上单位向量
AJZPoint2D LineVectorVecTop(AJZPoint2D linept1,AJZPoint2D linept2);
2.APlaneGeometryCompute.cpp
#include "IllustratorSDK.h"
#include "APlaneGeometryCompute.h"
#include "Math.h"
#define _PI 3.1415926535898

APlaneGeometryCompute::APlaneGeometryCompute(void)
{

}


APlaneGeometryCompute::~APlaneGeometryCompute(void)
{

}

//角度转弧度
double Degree2Radius(double d)
{
	double rlt = d*_PI / 180;
	return rlt;
}

//弧度转角度
double Radius2Degree(double r)
{
	double rlt = r * 180 / _PI;
	return rlt;
}

//单位向量
AJZPoint2D AJZPoint2D::UnitVector(){
	double len=sqrt(x*x+y*y);
	if (len==0)
	{
		AJZPoint2D rlt=*this;
		return rlt;
	}

	double xt=x/len;
	double yt=y/len;
	AJZPoint2D pt(xt,yt);
	return pt;
}

//绕原点的逆时针方向角
double AJZPoint2D::ClockAngleByOrg(){
	double angle=atan2(y,x);
	if (angle            
关注
打赏
1665481431
查看更多评论
0.0780s