查看: 993|回复: 2
收起左侧

[AVEVA MARINE] 自己编的C++几何库,持续更新

[复制链接]
发表于 2022-9-17 23:31 | 显示全部楼层 |阅读模式 来自: 中国上海
  1. class XhmTransformation3D
  2. {
  3. public:
  4.     double matrix11, matrix12, matrix13, matrix14;
  5.     double matrix21, matrix22, matrix23, matrix24;
  6.     double matrix31, matrix32, matrix33, matrix34;
  7.     double matrix41, matrix42, matrix43, matrix44;

  8.     XhmTransformation3D() {
  9.         matrix11 = 1.0, matrix12 = 0.0, matrix13 = 0.0, matrix14 = 0.0;
  10.         matrix21 = 0.0, matrix22 = 1.0, matrix23 = 0.0, matrix24 = 0.0;
  11.         matrix31 = 0.0, matrix32 = 0.0, matrix33 = 1.0, matrix34 = 0.0;
  12.         matrix41 = 0.0, matrix42 = 0.0, matrix43 = 0.0, matrix44 = 1.0;
  13.     }
  14. };

  15. class XhmPoint2D
  16. {
  17. public:
  18.     double X, Y;  // XY坐标

  19.     //默认初始化
  20.     XhmPoint2D() {
  21.         X = 0.0, Y = 0.0;
  22.     }

  23.     //输入初始化
  24.     XhmPoint2D(double x, double y) {
  25.         X = x, Y = y;
  26.     }

  27.     //计算点到点的距离
  28.     double DistanceToPoint(XhmPoint2D p){
  29.         return (sqrt(pow(X - p.X,2)  + pow(Y - p.Y,2) ));
  30.     }

  31.     //移动点
  32.     double Move(double xmove, double ymove){
  33.         X += xmove, Y += ymove;
  34.     }

  35.     //通过两个点的中点定义点
  36.     void SetFromMidpoint(XhmPoint2D p1, XhmPoint2D p2) {
  37.         X = p1.X + (p2.X - p1.X) * 0.5;
  38.         Y = p1.Y + (p2.Y - p1.Y) * 0.5;
  39.     }
  40. };


  41. class XhmPoint3D
  42. {
  43. public:
  44.     double X, Y, Z;  // XYZ坐标

  45.     //默认初始化
  46.     XhmPoint3D() {
  47.         X = 0.0, Y = 0.0, Z = 0.0;
  48.     }

  49.     //输入初始化
  50.     XhmPoint3D(double x, double y, double z) {
  51.         X = x, Y = y, Z = z;
  52.     }

  53.     //计算点到点的距离
  54.     double DistanceToPoint(XhmPoint3D p) {
  55.         return (sqrt(pow(X - p.X, 2) + pow(Y - p.Y, 2) + pow(Z - p.Z, 2)));
  56.     }

  57.     //计算点到点的距离
  58.     double Move(double xmove, double ymove, double zmove) {
  59.         X += xmove, Y += ymove, Z += zmove;
  60.     }

  61.     //通过两个点的中点定义点
  62.     void SetFromMidpoint(XhmPoint3D p1, XhmPoint3D p2) {
  63.         X = p1.X + (p2.X - p1.X) * 0.5;
  64.         Y = p1.Y + (p2.Y - p1.Y) * 0.5;
  65.         Z = p1.Z + (p2.Z - p1.Z) * 0.5;
  66.     }

  67.     //点矩阵变换
  68.     void SetFromMidpoint(XhmTransformation3D tra) {
  69.        double proj = X * tra.matrix14 + Y * tra.matrix24 + Z * tra.matrix34 + tra.matrix44;
  70.        if (proj <= 1.0E-15 and proj >= -1.0E-15) {
  71.            proj = 1.0;
  72.        }        
  73.        X = (X * tra.matrix11 + Y * tra.matrix21 + Z * tra.matrix31 + tra.matrix41) / proj;
  74.        Y = (X * tra.matrix12 + Y * tra.matrix22 + Z * tra.matrix32 + tra.matrix42) / proj;
  75.        Z = (X * tra.matrix13 + Y * tra.matrix23 + Z * tra.matrix33 + tra.matrix43) / proj;
  76.     }
  77. };


  78. class XhmVector2D
  79. {
  80. public:
  81.     double X, Y;  // XY坐标

  82.     //默认初始化
  83.     XhmVector2D() {
  84.         X = 0.0, Y = 0.0;
  85.     }

  86.     //输入初始化
  87.     XhmVector2D(double x, double y) {
  88.         X = x, Y = y;
  89.     }

  90.     //向量相加
  91.     void Add(XhmVector2D vec) {
  92.         X += vec.X, Y += vec.Y;
  93.     }

  94.     //向量相减
  95.     void Subtract(XhmVector2D vec) {
  96.         X -= vec.X, Y -= vec.Y;
  97.     }

  98.     //向量积
  99.     double DotProduct(XhmVector2D vec) {
  100.         return (X * vec.X + Y * vec.Y);
  101.     }

  102.     //缩放矢量
  103.     void BlankProduct(double sc) {
  104.         X *= sc, Y *= sc;
  105.     }

  106.     //向量比较
  107.     bool CompareVector(XhmVector2D vec, double tolerance) {
  108.         double xx = sqrt((X - vec.X) * (X - vec.X) + (Y - vec.Y) * (Y - vec.Y));
  109.         return (xx < tolerance) ? true : false;
  110.     }

  111.     //获取向量偏X还是偏Y
  112.     string LargestComponentAxis(XhmVector2D vec, double tolerance) {
  113.         return (X < Y) ? "Y" : "X";
  114.     }

  115.     //获取向量长度
  116.     double Length() {
  117.         return (sqrt(X * X + Y * Y));
  118.     }

  119.     //旋转矢量,angle为弧度
  120.     void Rotate(double angle) {
  121.         X = cos(angle) * X - sin(angle) * Y;
  122.         Y = sin(angle) * X + cos(angle) * Y;
  123.     }

  124.     //向量上的标量分量
  125.     double ScalarComponentOnVector(XhmVector2D vec) {
  126.         double L1 = vec.Length();
  127.         return (L1 < 1.0E-15) ? 0 : (X * vec.X + Y * vec.Y) / L1;
  128.     }

  129.     //通过两点定义向量
  130.     void SetFromPoints(XhmPoint2D p1, XhmPoint2D p2) {
  131.         X = p2.X - p1.X;
  132.         Y = p2.Y - p1.Y;
  133.     }

  134.     //通过VEC向量定义向量
  135.     void SetFromVector(XhmVector2D vec) {
  136.         X = vec.X;
  137.         Y = vec.Y;
  138.     }

  139.     //通过VEC向量相减定义向量
  140.     void SetFromVectorDifference(XhmVector2D vec1, XhmVector2D vec2) {
  141.         X = vec1.X - vec2.X;
  142.         Y = vec1.Y - vec2.Y;
  143.     }

  144.     //通过VEC向量相加定义向量
  145.     void SetFromVectorSum(XhmVector2D vec1, XhmVector2D vec2) {
  146.         X = vec1.X + vec2.X;
  147.         Y = vec1.Y + vec2.Y;
  148.     }

  149.     //设置为单位向量,长度为1
  150.     void SetToUnitVector() {
  151.         double l = Length();
  152.         if (l >= 1.0E-15)
  153.         {
  154.             X = X / l;
  155.             Y = Y / l;
  156.         }
  157.     }

  158.     //设置向量长度
  159.     void SetLength(double l) {
  160.         SetToUnitVector();
  161.         BlankProduct(l);
  162.     }
  163. };
复制代码

回复

使用道具 举报

龙船学院
发表于 2022-9-19 00:12 | 显示全部楼层 来自: 中国
编程每每都是晚上最辛苦,因为夜阑人静的晚上是头脑风暴的最佳迸发时间。支持一下~~~~

缺月挂疏桐,漏断人初静。谁见幽人独往来,缥缈孤鸿影。
回复 支持 反对

使用道具 举报

发表于 2022-9-19 11:08 | 显示全部楼层 来自: 中国浙江舟山
C++,牛了兄弟
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|标签|免责声明|龙船社区

GMT+8, 2024-6-1 09:58

Powered by Imarine

Copyright © 2006, 龙船社区

快速回复 返回顶部 返回列表