BoxPositionTool.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // BoxPositionTool.cpp
  3. // auto_fill_jewel_v3
  4. //
  5. // Created by Red on 2024/11/29.
  6. //
  7. #include "BoxPositionTool.hpp"
  8. #include <iostream>
  9. using std::cout;
  10. using std::endl;
  11. void BoxPositionTool::solve(const int visWidth,const int visHeight, vector<vector<int>>& boxSizeArr, vector<vector<int>>& resultPositions)
  12. {
  13. _boxes.clear() ;
  14. int numSolv = boxSizeArr.size() ;
  15. int boxIndex = 0 ;
  16. for(int y = 0 ; y < visHeight; ++y ) {
  17. for(int x = 0 ; x < visWidth; ++ x ) {
  18. vector<int>& box = boxSizeArr[boxIndex] ;
  19. if( withinContainer(x, y, box[0], box[1]) ) {
  20. BoostGeometryTools::BoostPolygon poly=BoostGeometryTools::makeBox(x, y, box[0], box[1]) ;
  21. if( interectsWithOthers(poly) == false ) {
  22. _boxes.push_back(poly) ;
  23. numSolv-- ;
  24. boxIndex++ ;
  25. resultPositions.push_back({x+box[0]/2, y+box[1]/2}) ;
  26. //cout<<"debug box at x,y "<<x<<","<<y<<endl;
  27. x = x+box[0]-1 ;
  28. }
  29. }
  30. if( numSolv==0 ) break ;
  31. }
  32. if( numSolv==0 ) break ;
  33. }
  34. }
  35. bool BoxPositionTool::withinContainer(const int llx,const int lly,const int wid,const int hei)
  36. {
  37. if( llx < 0 ) return false ;
  38. if( llx + wid > BOXPOSITIONTOOL_CONTAINER_WIDTH ) return false ;
  39. return true ;
  40. }
  41. bool BoxPositionTool::interectsWithOthers(BoostGeometryTools::BoostPolygon& poly)
  42. {
  43. for(int io = 0 ; io < _boxes.size();++ io ) {
  44. if( BoostGeometryTools::isAIntersectsB(poly, _boxes[io]) ) {
  45. return true ;
  46. }
  47. }
  48. return false ;
  49. }