| 28 | } |
| 29 | |
| 30 | void cropPolygonSide(VectorFloatPoint& _verticies, int _sideCoord, Side _side) |
| 31 | { |
| 32 | VectorFloatPoint newVerticies; |
| 33 | int invert = (_side == Right || _side == Bottom) ? -1 : 1; |
| 34 | for (size_t i = 0; i < _verticies.size(); ++i) |
| 35 | { |
| 36 | FloatPoint& v0 = _verticies[i]; |
| 37 | FloatPoint& v1 = _verticies[(i + 1) % _verticies.size()]; |
| 38 | switch (_side) |
| 39 | { |
| 40 | case Left: |
| 41 | case Right: |
| 42 | // both inside |
| 43 | if (invert * v0.left >= invert * _sideCoord && invert * v1.left >= invert * _sideCoord) |
| 44 | newVerticies.push_back(v0); |
| 45 | // intersect side (1st vertex in) |
| 46 | else if (invert * v0.left >= invert * _sideCoord && invert * v1.left < invert * _sideCoord) |
| 47 | { |
| 48 | newVerticies.push_back(v0); |
| 49 | float c = (v0.left - _sideCoord) / (_sideCoord - v1.left); |
| 50 | newVerticies.emplace_back((float)_sideCoord, (v0.top + c * v1.top) / (c + 1)); |
| 51 | } |
| 52 | // intersect side (2nd vertex in) |
| 53 | else if (invert * v0.left <= invert * _sideCoord && invert * v1.left > invert * _sideCoord) |
| 54 | { |
| 55 | float c = (v0.left - _sideCoord) / (_sideCoord - v1.left); |
| 56 | newVerticies.emplace_back((float)_sideCoord, (v0.top + c * v1.top) / (c + 1)); |
| 57 | } |
| 58 | // else don't add any verticies |
| 59 | break; |
| 60 | case Top: |
| 61 | case Bottom: |
| 62 | // both inside |
| 63 | if (invert * v0.top >= invert * _sideCoord && invert * v1.top >= invert * _sideCoord) |
| 64 | newVerticies.push_back(v0); |
| 65 | // intersect side (1st vertex in) |
| 66 | else if (invert * v0.top >= invert * _sideCoord && invert * v1.top < invert * _sideCoord) |
| 67 | { |
| 68 | newVerticies.push_back(v0); |
| 69 | float c = (v0.top - _sideCoord) / (_sideCoord - v1.top); |
| 70 | newVerticies.emplace_back((v0.left + c * v1.left) / (c + 1), (float)_sideCoord); |
| 71 | } |
| 72 | // intersect side (2nd vertex in) |
| 73 | else if (invert * v0.top <= invert * _sideCoord && invert * v1.top > invert * _sideCoord) |
| 74 | { |
| 75 | float c = (v0.top - _sideCoord) / (_sideCoord - v1.top); |
| 76 | newVerticies.emplace_back((v0.left + c * v1.left) / (c + 1), (float)_sideCoord); |
| 77 | } |
| 78 | // else don't add any verticies |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | _verticies = newVerticies; |
| 84 | } |
| 85 | |
| 86 | FloatPoint getPositionInsideRect( |
| 87 | const FloatPoint& _point, |
no test coverage detected