Advertisement
irmantas_radavicius

Untitled

Mar 16th, 2022
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.83 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Box {
  5.     private double x;
  6.     private double y;
  7.     private double z;
  8.     private double border;
  9.     private String s;
  10.     private boolean rotating2D = false; // around y
  11.     private boolean rotating3D = false; // 3d
  12.    
  13.    
  14.     public Box(double x){
  15.         this(x, x, x);
  16.         s = "S";
  17.     }
  18.     public Box(double x, double border){
  19.         this(x);
  20.         setBorder(border);
  21.     }
  22.     public Box(double x, double y, double z){
  23.         setX(x);
  24.         setY(y);
  25.         setZ(z);
  26.         s = "R";
  27.     }
  28.     public Box(double x, double y, double z, double border){
  29.         this(x, y, z);
  30.         setBorder(border);
  31.     }
  32.    
  33.     private void setX(double x){
  34.         this.x = x;
  35.     }  
  36.     public double getX(){
  37.         return this.x;
  38.     }
  39.     public double getX(boolean getInner){
  40.         return getInner ? getInnerX() : getOuterX();
  41.     }
  42.     public double getInnerX(){
  43.         return getX() + ((border < 0) ? border : 0);
  44.     }
  45.     public double getOuterX(){
  46.         return getX() + ((border > 0) ? border : 0);
  47.     }
  48.    
  49.     private void setY(double y){
  50.         this.y = y;
  51.     }  
  52.     public double getY(){
  53.         return this.y;
  54.     }
  55.     public double getY(boolean getInner){
  56.         return getInner ? getInnerY() : getOuterY();
  57.     }
  58.     public double getInnerY(){
  59.         return getY() + ((border < 0) ? border : 0);
  60.     }
  61.     public double getOuterY(){
  62.         return getY() + ((border > 0) ? border : 0);
  63.     }
  64.    
  65.     private void setZ(double z){
  66.         this.z = z;
  67.     }  
  68.     public double getZ(){
  69.         return this.z;
  70.     }
  71.     public double getZ(boolean getInner){
  72.         return getInner ? getInnerZ() : getOuterZ();
  73.     }
  74.     public double getInnerZ(){
  75.         return getZ() + ((border < 0) ? border : 0);
  76.     }
  77.     public double getOuterZ(){
  78.         return getZ() + ((border > 0) ? border : 0);
  79.     }
  80.    
  81.     private void setBorder(double border){
  82.         this.border = border;
  83.     }  
  84.     public double getBorder(){
  85.         return this.border;
  86.     }
  87.    
  88.     public void canRotate2D(boolean value){
  89.         this.rotating2D = value;
  90.     }  
  91.     public boolean canRotate2D(){
  92.         return this.rotating2D;
  93.     }
  94.    
  95.    
  96.     public void canRotate3D(boolean value){
  97.         this.rotating3D = value;
  98.     }  
  99.     public boolean canRotate3D(){
  100.         return this.rotating3D;
  101.     }
  102.        
  103.    
  104.     public void rotateXZ(){ // around Y
  105.         if (canRotate2D() || canRotate3D()){           
  106.             double tempX = getX();  
  107.             setX(getZ());
  108.             setZ(tempX);
  109.         }
  110.     }
  111.     public void rotateXY(){ // around Z
  112.         if (canRotate3D()){
  113.             double tempX = getX();
  114.             setX(getY());
  115.             setY(tempX);
  116.         }
  117.     }
  118.     public void rotateYZ(){ // around X
  119.         if (canRotate3D()){
  120.             double tempY = getY();
  121.             setY(getZ());
  122.             setZ(tempY);
  123.         }
  124.     }
  125.    
  126.    
  127.     public double getVolume(boolean countInner){
  128.         return getX(countInner) * getY(countInner) * getZ(countInner); 
  129.     }
  130.    
  131.     private boolean canFitIn1D(Box c){
  132.         boolean fitsX = c.getInnerX() > getOuterX();
  133.         boolean fitsY = c.getInnerY() > getOuterY();
  134.         boolean fitsZ = c.getInnerZ() > getOuterZ();
  135.         if(fitsX && fitsY && fitsZ){
  136.             return true;
  137.         } else {
  138.             return false;
  139.         }  
  140.     }
  141.     private boolean canFitIn2D(Box c){
  142.         Box temp = new Box(c.getX(), c.getY(), c.getZ(), c.getBorder());
  143.         temp.canRotate2D(c.canRotate2D() || canRotate2D());
  144.         temp.canRotate3D(c.canRotate3D() || canRotate3D());
  145.         temp.rotateXZ();
  146.         return canFitIn1D(c) || canFitIn1D(temp);          
  147.     }
  148.     private boolean canFitIn3D(Box c){
  149.         Box temp1 = new Box(c.getX(), c.getY(), c.getZ(), c.getBorder());
  150.         temp1.canRotate2D(c.canRotate2D() || canRotate2D());
  151.         temp1.canRotate3D(c.canRotate3D() || canRotate3D());
  152.         temp1.rotateXY();          
  153.         Box temp2 = new Box(c.getX(), c.getY(), c.getZ(), c.getBorder());
  154.         temp2.canRotate2D(c.canRotate2D() || canRotate2D());
  155.         temp2.canRotate3D(c.canRotate3D() || canRotate3D());
  156.         temp2.rotateYZ();      
  157.         return canFitIn2D(c) || canFitIn2D(temp1) || canFitIn2D(temp2);        
  158.     }
  159.    
  160.     public boolean canFitIn(Box c){    
  161.         if(canRotate3D() || c.canRotate3D()){ // can rotate anywhere
  162.             return canFitIn3D(c);                      
  163.         } else if (canRotate2D() || c.canRotate2D()){ // can rotate xz     
  164.             return canFitIn2D(c);          
  165.         } else { // can not rotate
  166.             return canFitIn1D(c);
  167.         }          
  168.     }
  169.    
  170.     public String toString(){
  171.         String temp = "";
  172.         temp += s + " " + getX() + " " + getY() + " " + getZ();
  173.         temp += " " + getBorder() + " " + canRotate2D() + " " + canRotate3D();
  174.         return temp;
  175.     }
  176. }
  177.  
  178. public class Sandbox {  
  179.     public static Box getRandomBox(boolean isSquare, int range){       
  180.         if (isSquare){
  181.             return new Box(getRandomValue(range));
  182.         } else {
  183.             return new Box(getRandomValue(range), getRandomValue(range), getRandomValue(range));
  184.         }                  
  185.     }
  186.    
  187.     public static Box getRandomBox(boolean isSquare, int range, double precision){     
  188.         if (isSquare){
  189.             return new Box(getRandomValue(range, precision), 1);
  190.         } else {
  191.             double x = getRandomValue(range, precision);
  192.             double y = getRandomValue(range, precision);
  193.             double z = getRandomValue(range, precision);
  194.             return new Box(x, y, z, 1);
  195.         }                  
  196.     }
  197.     public static double getRandomValue(int range){
  198.         return Math.random()*range;
  199.     }
  200.     public static double getRandomValue(int range, double precision){
  201.         return getRoundedValue(Math.random()*range, precision);
  202.     }
  203.     public static double getRoundedValue(double value, double precision){
  204.         return ((int)(precision*value))/precision;
  205.     }
  206.  
  207.     public static void main(String args[]) {       
  208.         try {          
  209.             Box [] boxes = new Box[100];           
  210.             for(int i = 0; i < boxes.length; ++i){
  211.                 boxes[i] = getRandomBox(Math.random() > 0.8, 100, 10);             
  212.             }          
  213.            
  214.             double totalVolume = 0;
  215.             for(int i = 0; i < boxes.length; ++i){     
  216.                 totalVolume += boxes[i].getVolume(true);               
  217.             }          
  218.             System.out.println(getRoundedValue(totalVolume, 10000));           
  219.                        
  220.             Box box = new Box(32, 42, 52, 3);      
  221.             box.canRotate3D(true);
  222.             System.out.println(box + " fits in: \n");          
  223.             for(int i = 0; i < boxes.length; ++i){
  224.                 if(box.canFitIn(boxes[i])){
  225.                     System.out.println(boxes[i]);
  226.                 }
  227.             }  
  228.  
  229.         } catch(Exception e){
  230.             System.out.println(e);         
  231.             System.out.println("Unexpected error, sorry!");
  232.         }          
  233.     }  
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement