mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[wpigui] Add GetDistSquared and MaxFit functions
These utility functions were moved from halsim_gui.
This commit is contained in:
@@ -413,4 +413,19 @@ bool gui::CreateTextureFromImage(const unsigned char* data, int len,
|
||||
return true;
|
||||
}
|
||||
|
||||
void gui::MaxFit(ImVec2* min, ImVec2* max, float width, float height) {
|
||||
float destWidth = max->x - min->x;
|
||||
float destHeight = max->y - min->y;
|
||||
if (width == 0 || height == 0) return;
|
||||
if (destWidth * height > destHeight * width) {
|
||||
float outputWidth = width * destHeight / height;
|
||||
min->x += (destWidth - outputWidth) / 2;
|
||||
max->x -= (destWidth - outputWidth) / 2;
|
||||
} else {
|
||||
float outputHeight = height * destWidth / width;
|
||||
min->y += (destHeight - outputHeight) / 2;
|
||||
max->y -= (destHeight - outputHeight) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -351,4 +351,29 @@ class Texture {
|
||||
int m_height = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get square of distance between two ImVec2's.
|
||||
*
|
||||
* @param a first ImVec
|
||||
* @param b second ImVec
|
||||
*
|
||||
* @return Distance^2 between a and b.
|
||||
*/
|
||||
inline float GetDistSquared(const ImVec2& a, const ImVec2& b) {
|
||||
float deltaX = b.x - a.x;
|
||||
float deltaY = b.y - a.y;
|
||||
return deltaX * deltaX + deltaY * deltaY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximize fit in "window" while preserving aspect ratio. Min and max
|
||||
* passed-in values are modified such that the object maximally fits.
|
||||
*
|
||||
* @param min upper left corner of window (modified to fit)
|
||||
* @param max lower right corner of window (modified to fit)
|
||||
* @param width width of object to fit
|
||||
* @param height height of object to fit
|
||||
*/
|
||||
void MaxFit(ImVec2* min, ImVec2* max, float width, float height);
|
||||
|
||||
} // namespace wpi::gui
|
||||
|
||||
Reference in New Issue
Block a user