Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在任意原点调整矩形形状的大小_C++_Math_Geometry_Shape_Shapes - Fatal编程技术网

C++ 在任意原点调整矩形形状的大小

C++ 在任意原点调整矩形形状的大小,c++,math,geometry,shape,shapes,C++,Math,Geometry,Shape,Shapes,我有一个名为Rectangle的类,它是一个二维形状,原点位于其中心。 Vec2f是一个float二维向量 struct Rectangle { Vec2f position, halfSize; float getTop() { return position.y - halfSize.y; } float getBottom() { return position.y + halfSize.y; } // ... getLeft(), getRight(),

我有一个名为
Rectangle
的类,它是一个二维形状,原点位于其中心。
Vec2f
是一个
float
二维向量

struct Rectangle {
    Vec2f position, halfSize;
    float getTop() { return position.y - halfSize.y; }
    float getBottom() { return position.y + halfSize.y; }
    // ... getLeft(), getRight(), ...
    void setWidth(float mWidth) { halfSize.x = mWidth / 2.f; }
    // ... setHeight(), setHalfWidth(), ...
};
使用
Rectangle::setWidth
调整形状的大小显然会将原点保持在中心。但是,我需要从不同的点任意调整矩形的大小。示例:我想从右下角调整矩形的大小,使用左上角作为其原点

理想的API应该是这样的:

Rectangle rect;
rect.resizeFrom(rect.getTopLeft(), Vec2f{150.f, 45.f});
//              ^ new origin       ^ new size

我的问题是:如何在调整矩形大小时任意选择原点?我显然需要执行平移和调整大小(考虑到实际原点是矩形的中心),但我找不到适用于每个任意原点的正确组合。

我建议使用OpenGL

无论如何,我的建议是

resizeFrom(FROM, Vec2f){
  resize in the normal way
  getTop(), getBottom(), etc.. //get all new corner positions
  Difference = FROM-newLeftCorner
  Move all corner positions according to the Difference
  commit
}

我希望我很清楚

这似乎可行,但并不能解决我原来的问题:我想从底部调整矩形的大小(想想快门):顶部的Y坐标应该保持不变,而应该更改的只是底部的Y坐标。要做到这一点,可能需要完全重新构造,如果没有看到完整的代码,很难说。如果直到所有计算完成后才显示新矩形,那么您的操作方式可能无关紧要