我发现自己定义了如下类:
struct AngleSize {
explicit AngleSize(double radians) : size(radians) {}
double size;
};
与将角度尺寸存储为普通双精度数相比,这具有许多优势。
优点 1,它可以定义这 2 个不同的构造函数:
Vec2::Vec2(double x, double y);
Vec2::Vec2(double length, AngleSize direction);
...后一个明确称为Vec2(1, AngleSize(5));
(尽管在这种情况下,静态工厂函数Vec2::fromPolar(1, 5)
可能同样好?)
优点 2,它更类型安全,捕获以下错误:
double temperature = air.getTemperature();
Vec2 v(someLength, temperature);
然而一切都不是很好。
缺点1、冗长且出人意料的语法
Vec2::Vec2(double length, AngleSize direction) {
x = length * cos(direction.size);
y = length * sin(direction.size);
}
呃,这真的应该说cos(direction)
在不允许不安全的隐式转换的情况下,我需要详细地访问实际值。
缺点2,每件事的课程太多,你在哪里画线?
struct Direction { // angle between line and horizontal axis
explicit Direction(AngleSize angleSize);
AngleSize angleSize;
};
struct Angle { // defined by "starting and ending angle" unlike AngleSize
Angle(Direction startingDir, Direction endingDir);
Angle(Direction startingDir, AngleSize size);
Angle(Line line1, Line line2);
};
struct PositionedAngle {
// angle defined by two rays. this is getting just silly
PositionedAngle(Ray firstRay, AngleSize angle);
PositionedAngle(Point center, AngleSize fromAngle, AngleSize toAngle);
// and so on, and so forth.
};
// each of those being a legit math concept with
// distinct operations possible on it.
在这种情况下,你自己会怎么做?
另外,我在哪里可以阅读有关此问题的信息?我想boost
可能有什么相关的?
请注意,这不仅仅是几何,它适用于任何地方。想想posix socklen_t ...