BIMHome v1.0.0
BIMHome接口文档说明
Vector2D.h
浏览该文件的文档.
1/************************************************************************
2* @file Vector2D.h
3*
4* @brief 二维坐标/向量类
5*
6* @details 二维坐标/向量类
7*
8* @author lirf
9*
10* @version 1.0
11*
12* @date 2025-6-12
13*
14* @license 北京华科软科技有限公司
15*
16*************************************************************************/
17
18#ifndef BIMHOMEBASE_VECTOR2D_H
19#define BIMHOMEBASE_VECTOR2D_H
20#ifndef BH_GLOBAL_H
21#include <BHGlobal.h>
22#endif
23#include <cmath>
24#include <cfloat>
25#include <stdexcept>
26#ifndef F_PI
27# define F_PI 3.1415926f
28#endif
29
30#ifndef D_PI
31# define D_PI 3.141592653589793
32#endif
33
34#ifndef FLOAT_MAX
35# define FLOAT_MAX 3.402823466E+38F
36#endif
37
38#ifndef FLOAT_MIN
39# define FLOAT_MIN 1.175494351E-38F
40#endif
41
42#ifndef DOUBLE_MAX
43# define DOUBLE_MAX 1.7976931348623157E+308 /* max decimal value of a "double"*/
44#endif
45
46#ifndef DOUBLE_MIN
47# define DOUBLE_MIN 2.2250738585072014E-308 /* min decimal value of a "double"*/
48#endif
49
50
51namespace Base
52{
57 class BaseExport Vector2d
58 {
59 public:
60
65 inline Vector2d();
66
73 inline Vector2d(float x, float y);
74
81 inline Vector2d(double x, double y);
82
88 inline Vector2d(const Vector2d& v);
89
90 // operators
97 inline Vector2d& operator= (const Vector2d& v);
98
105 inline bool operator== (const Vector2d& v) const;
106
112 inline Vector2d operator+ (void) const;
113
120 inline Vector2d operator+ (const Vector2d& v) const;
121
128 inline Vector2d& operator+= (const Vector2d& v);
129
135 inline Vector2d operator- (void) const;
136
143 inline Vector2d operator- (const Vector2d& v) const;
144
151 inline Vector2d& operator-= (const Vector2d& v);
152
159 inline Vector2d operator* (double c) const;
160
167 inline Vector2d& operator*= (double c);
168
175 inline double operator* (const Vector2d& v) const;
176
183 inline Vector2d operator/ (double c) const;
184
191 inline Vector2d& operator/= (double c);
192
193 // methods
200 inline bool IsNull(double tolerance = 0.0) const;
201
207 inline double Length() const;
208
214 inline double Angle() const;
215
221 inline double Sqr() const;
222
230 inline Vector2d& Set(double x, double y);
231
237 inline Vector2d& Negate();
238
245 inline Vector2d& Scale(double factor);
246
253 inline Vector2d& Rotate(double angle);
254
260 inline Vector2d& Normalize();
261
268 inline Vector2d Perpendicular(bool clockwise = false) const;
269
277 static inline Vector2d FromPolar(double r, double fi);
278
285 inline double Distance(const Vector2d& v) const;
286
294 static inline double Distance(const Vector2d& v1, const Vector2d& v2)
295 {
296 double x = v1.x - v2.x, y = v1.y - v2.y;
297 return static_cast<double>(sqrt((x * x) + (y * y)));
298 }
299
307 inline bool IsEqual(const Vector2d& v, double tolerance = 0.0) const;
308
316 inline bool IsLess(const Vector2d& v, double tolerance = 0.0) const;
317
324 double GetAngle(const Vector2d& v) const;
325
332 void ProjectToLine(const Vector2d& point, const Vector2d& line);
333
342 Vector2d alongLineNormalMoveDistancePt(const Vector2d& rclP0, const Vector2d& rclP1, double d) const;
343
352 Vector2d moveAlongLine(const Vector2d& rclP0, const Vector2d& rclP1, double d) const;
353
363 Vector2d alongLineAngleMoveDistancePt(const Vector2d& rclP0, const Vector2d& rclP1, double angle, double d) const;
364
372 Vector2d alongLineNormalDir(const Vector2d& rclP0, const Vector2d& rclP1) const;
373
382
383 public:
384 double x, y;
386 };
387
389 : x(0.0), y(0.0)
390 {
391 }
392
393 inline Vector2d::Vector2d(float x, float y)
394 : x(double(x)), y(double(y))
395 {
396 }
397
398 inline Vector2d::Vector2d(double x, double y)
399 : x(x), y(y)
400 {
401 }
402
404 : x(v.x), y(v.y)
405 {
406 }
407
409 {
410 x = v.x;
411 y = v.y;
412 return *this;
413 }
414
415 inline bool Vector2d::operator== (const Vector2d& v) const
416 {
417 return (x == v.x) && (y == v.y);
418 }
419
421 {
422 return Vector2d(x, y);
423 }
424
426 {
427 return Vector2d(x + v.x, y + v.y);
428 }
429
431 {
432 x += v.x;
433 y += v.y;
434 return *this;
435 }
436
438 {
439 return Vector2d(-x, -y);
440 }
441
443 {
444 return Vector2d(x - v.x, y - v.y);
445 }
446
448 {
449 x -= v.x;
450 y -= v.y;
451 return *this;
452 }
453
454 inline Vector2d Vector2d::operator* (double c) const
455 {
456 return Vector2d(c * x, c * y);
457 }
458
460 {
461 x *= c;
462 y *= c;
463 return *this;
464 }
465
466 inline double Vector2d::operator* (const Vector2d& v) const
467 {
468 return x * v.x + y * v.y;
469 }
470
471 inline Vector2d operator* (double c, const Vector2d& v)
472 {
473 return Vector2d(c * v.x, c * v.y);
474 }
475
476 inline Vector2d Vector2d::operator/ (double c) const
477 {
478 return Vector2d(x / c, y / c);
479 }
480
482 {
483 x /= c;
484 y /= c;
485 return *this;
486 }
487
488 inline bool Vector2d::IsNull(double tolerance) const
489 {
490 return x * x + y * y <= tolerance * tolerance;
491 }
492
493 inline double Vector2d::Length() const
494 {
495 return sqrt(x * x + y * y);
496 }
497
498 inline double Vector2d::Angle() const
499 {
500 return atan2(y, x);
501 }
502
503 inline double Vector2d::Sqr() const
504 {
505 return x * x + y * y;
506 }
507
508 inline Vector2d& Vector2d::Set(double x, double y)
509 {
510 this->x = x;
511 this->y = y;
512 return *this;
513 }
514
516 {
517 x = -x;
518 y = -y;
519 return *this;
520 }
521
522 inline Vector2d& Vector2d::Scale(double factor)
523 {
524 x *= factor;
525 y *= factor;
526 return *this;
527 }
528
529 inline Vector2d& Vector2d::Rotate(double angle)
530 {
531 Vector2d cPt(*this);
532 x = cPt.x * cos(angle) - cPt.y * sin(angle);
533 y = cPt.x * sin(angle) + cPt.y * cos(angle);
534 return *this;
535 }
536
538 {
539 double length = Length();
540 if (length > 0.0)
541 {
542 x /= length;
543 y /= length;
544 }
545
546 return *this;
547 }
548
549 inline Vector2d Vector2d::Perpendicular(bool clockwise) const
550 {
551 return clockwise ? Vector2d(y, -x) : Vector2d(-y, x);
552 }
553
554 inline Vector2d Vector2d::FromPolar(double r, double fi)
555 {
556 return Vector2d(r * cos(fi), r * sin(fi));
557 }
558
559 inline double Vector2d::Distance(const Vector2d& v) const
560 {
561 double dx = (x - v.x);
562 double dy = (y - v.y);
563
564 return sqrt(dx * dx + dy * dy);
565 }
566
567 inline bool Vector2d::IsEqual(const Vector2d& v, double tolerance) const
568 {
569 return Distance(v) <= tolerance;
570 }
571} // namespace Base
572
573#endif // BIMHOMEBASE_VECTOR3D_H
A
Definition ISnapProcessedBase.h:49
B
Definition ISnapProcessedBase.h:49
Vector2d & Set(double x, double y)
设置向量的坐标
Definition Vector2D.h:508
Vector2d operator+(void) const
加法运算符
Definition Vector2D.h:420
double Distance(const Vector2d &v) const
计算两个向量之间的距离
Definition Vector2D.h:559
Vector2d & Rotate(double angle)
旋转向量
Definition Vector2D.h:529
Vector2d & Negate()
取向量的相反数
Definition Vector2D.h:515
Vector2d moveAlongLine(const Vector2d &rclP0, const Vector2d &rclP1, double d) const
计算沿直线 P0P1 移动距离 d 的点
Vector2d & Normalize()
归一化向量
Definition Vector2D.h:537
double Sqr() const
计算向量的平方长度
Definition Vector2D.h:503
Vector2d & operator=(const Vector2d &v)
赋值运算符
Definition Vector2D.h:408
Vector2d alongLineNormalMoveDistancePt(const Vector2d &rclP0, const Vector2d &rclP1, double d) const
计算沿直线法线方向移动距离 d 的点
Vector2d Perpendicular(bool clockwise=false) const
计算向量的法向量
Definition Vector2D.h:549
Vector2d & Scale(double factor)
缩放向量
Definition Vector2D.h:522
bool IsLess(const Vector2d &v, double tolerance=0.0) const
比较两个向量是否小于
Vector2d perpendicularPointOnLine(const Vector2d &A, const Vector2d &B)
计算当前点到直线 AB 的垂足
bool IsEqual(const Vector2d &v, double tolerance=0.0) const
比较两个向量是否相等
Definition Vector2D.h:567
Vector2d & operator*=(double c)
乘法赋值运算符
Definition Vector2D.h:459
bool IsNull(double tolerance=0.0) const
检查向量是否为零向量
Definition Vector2D.h:488
Vector2d & operator/=(double c)
除法赋值运算符
Definition Vector2D.h:481
double Length() const
计算向量的长度
Definition Vector2D.h:493
Vector2d operator*(double c) const
乘法运算符
Definition Vector2D.h:454
void ProjectToLine(const Vector2d &point, const Vector2d &line)
将向量投影到一条直线上
Vector2d & operator-=(const Vector2d &v)
减法赋值运算符
Definition Vector2D.h:447
Vector2d operator/(double c) const
除法运算符
Definition Vector2D.h:476
double y
x 和 y 坐标
Definition Vector2D.h:384
Vector2d()
默认构造函数
Definition Vector2D.h:388
static Vector2d FromPolar(double r, double fi)
从极坐标创建向量
Definition Vector2D.h:554
Vector2d operator-(void) const
取反运算符
Definition Vector2D.h:437
bool operator==(const Vector2d &v) const
等于运算符
Definition Vector2D.h:415
Vector2d alongLineAngleMoveDistancePt(const Vector2d &rclP0, const Vector2d &rclP1, double angle, double d) const
计算与直线 P0P1 夹角为 angle 的线上一点,使得该点到直线 P0P1 的距离为 d
static double Distance(const Vector2d &v1, const Vector2d &v2)
计算两个向量之间的距离
Definition Vector2D.h:294
Vector2d & operator+=(const Vector2d &v)
加法赋值运算符
Definition Vector2D.h:430
Vector2d alongLineNormalDir(const Vector2d &rclP0, const Vector2d &rclP1) const
计算直线 P0P1 的法线方向
double Angle() const
计算向量与 x 轴的夹角
Definition Vector2D.h:498
double x
Definition Vector2D.h:384
double GetAngle(const Vector2d &v) const
计算两个向量之间的夹角
Vector2d 类,表示二维空间中的向量
Definition Vector2D.h:58
_Precision Distance(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
计算三维空间中两个点之间的欧几里得距离
Definition Vector3D.h:851
DualNumber operator*(DualNumber a, DualNumber b)
将两个对偶数相乘
Definition DualNumber.h:188
Definition BaseFigureFactory.h:24