| 52 | } |
| 53 | |
| 54 | std::array<ImVec2, 4> VHMBBCalculate(std::vector<ImVec2> hull, double psz) { |
| 55 | std::array<ImVec2, 4> box; |
| 56 | |
| 57 | double mbAngle = 0, cumulative_angle = 0; |
| 58 | double mbArea = DBL_MAX; // fake area to initialise |
| 59 | ImVec2 mbb, mba, origin; // Box bottom left, box top right |
| 60 | |
| 61 | // Find the lowest hull point, if it's below the x-axis just bring it up to |
| 62 | // compensate |
| 63 | // NOTE: we're not modifying the actual hull point, just a copy |
| 64 | origin.x = DBL_MAX; |
| 65 | origin.y = DBL_MAX; |
| 66 | |
| 67 | // find bottom corner |
| 68 | for (size_t i = 0; i < hull.size(); i++) { |
| 69 | if (hull[i].y < origin.y) origin.y = hull[i].y; |
| 70 | if (hull[i].x < origin.x) origin.x = hull[i].x; |
| 71 | } |
| 72 | |
| 73 | // transpose |
| 74 | for (size_t i = 0; i < hull.size(); i++) { |
| 75 | hull[i].x -= origin.x; |
| 76 | hull[i].y -= origin.y; |
| 77 | } |
| 78 | |
| 79 | // rotate hull on each side and work out the smallest area |
| 80 | for (size_t i = 0; i < hull.size(); i++) { |
| 81 | int ni = i + 1; |
| 82 | double area; |
| 83 | |
| 84 | ImVec2 current = hull[i]; |
| 85 | ImVec2 next = hull[ni % hull.size()]; |
| 86 | |
| 87 | double angle = VHAngleToX(current, next); // angle formed between current and next hull points; |
| 88 | cumulative_angle += angle; |
| 89 | |
| 90 | double top, bot, left, right; // bounding rect limits |
| 91 | top = right = DBL_MIN; |
| 92 | bot = left = DBL_MAX; |
| 93 | |
| 94 | for (size_t x = 0; x < hull.size(); x++) { |
| 95 | ImVec2 rp = VHRotateV(hull[x], -angle); |
| 96 | |
| 97 | hull[x] = rp; |
| 98 | |
| 99 | if (rp.y > top) top = rp.y; |
| 100 | if (rp.y < bot) bot = rp.y; |
| 101 | if (rp.x > right) right = rp.x; |
| 102 | if (rp.x < left) left = rp.x; |
| 103 | } |
| 104 | area = (right - left) * (top - bot); |
| 105 | |
| 106 | if (area < mbArea) { |
| 107 | mbArea = area; |
| 108 | mbAngle = cumulative_angle; // total angle we've had to rotate the board |
| 109 | // to get to this orientation; |
| 110 | mba = ImVec2(left, bot); |
| 111 | mbb = ImVec2(right, top); |
no test coverage detected