Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class Box {
- private double x;
- private double y;
- private double z;
- private double border;
- private String s;
- private boolean rotating2D = false; // around y
- private boolean rotating3D = false; // 3d
- public Box(double x){
- this(x, x, x);
- s = "S";
- }
- public Box(double x, double border){
- this(x);
- setBorder(border);
- }
- public Box(double x, double y, double z){
- setX(x);
- setY(y);
- setZ(z);
- s = "R";
- }
- public Box(double x, double y, double z, double border){
- this(x, y, z);
- setBorder(border);
- }
- private void setX(double x){
- this.x = x;
- }
- public double getX(){
- return this.x;
- }
- public double getX(boolean getInner){
- return getInner ? getInnerX() : getOuterX();
- }
- public double getInnerX(){
- return getX() + ((border < 0) ? border : 0);
- }
- public double getOuterX(){
- return getX() + ((border > 0) ? border : 0);
- }
- private void setY(double y){
- this.y = y;
- }
- public double getY(){
- return this.y;
- }
- public double getY(boolean getInner){
- return getInner ? getInnerY() : getOuterY();
- }
- public double getInnerY(){
- return getY() + ((border < 0) ? border : 0);
- }
- public double getOuterY(){
- return getY() + ((border > 0) ? border : 0);
- }
- private void setZ(double z){
- this.z = z;
- }
- public double getZ(){
- return this.z;
- }
- public double getZ(boolean getInner){
- return getInner ? getInnerZ() : getOuterZ();
- }
- public double getInnerZ(){
- return getZ() + ((border < 0) ? border : 0);
- }
- public double getOuterZ(){
- return getZ() + ((border > 0) ? border : 0);
- }
- private void setBorder(double border){
- this.border = border;
- }
- public double getBorder(){
- return this.border;
- }
- public void canRotate2D(boolean value){
- this.rotating2D = value;
- }
- public boolean canRotate2D(){
- return this.rotating2D;
- }
- public void canRotate3D(boolean value){
- this.rotating3D = value;
- }
- public boolean canRotate3D(){
- return this.rotating3D;
- }
- public void rotateXZ(){ // around Y
- if (canRotate2D() || canRotate3D()){
- double tempX = getX();
- setX(getZ());
- setZ(tempX);
- }
- }
- public void rotateXY(){ // around Z
- if (canRotate3D()){
- double tempX = getX();
- setX(getY());
- setY(tempX);
- }
- }
- public void rotateYZ(){ // around X
- if (canRotate3D()){
- double tempY = getY();
- setY(getZ());
- setZ(tempY);
- }
- }
- public double getVolume(boolean countInner){
- return getX(countInner) * getY(countInner) * getZ(countInner);
- }
- private boolean canFitIn1D(Box c){
- boolean fitsX = c.getInnerX() > getOuterX();
- boolean fitsY = c.getInnerY() > getOuterY();
- boolean fitsZ = c.getInnerZ() > getOuterZ();
- if(fitsX && fitsY && fitsZ){
- return true;
- } else {
- return false;
- }
- }
- private boolean canFitIn2D(Box c){
- Box temp = new Box(c.getX(), c.getY(), c.getZ(), c.getBorder());
- temp.canRotate2D(c.canRotate2D() || canRotate2D());
- temp.canRotate3D(c.canRotate3D() || canRotate3D());
- temp.rotateXZ();
- return canFitIn1D(c) || canFitIn1D(temp);
- }
- private boolean canFitIn3D(Box c){
- Box temp1 = new Box(c.getX(), c.getY(), c.getZ(), c.getBorder());
- temp1.canRotate2D(c.canRotate2D() || canRotate2D());
- temp1.canRotate3D(c.canRotate3D() || canRotate3D());
- temp1.rotateXY();
- Box temp2 = new Box(c.getX(), c.getY(), c.getZ(), c.getBorder());
- temp2.canRotate2D(c.canRotate2D() || canRotate2D());
- temp2.canRotate3D(c.canRotate3D() || canRotate3D());
- temp2.rotateYZ();
- return canFitIn2D(c) || canFitIn2D(temp1) || canFitIn2D(temp2);
- }
- public boolean canFitIn(Box c){
- if(canRotate3D() || c.canRotate3D()){ // can rotate anywhere
- return canFitIn3D(c);
- } else if (canRotate2D() || c.canRotate2D()){ // can rotate xz
- return canFitIn2D(c);
- } else { // can not rotate
- return canFitIn1D(c);
- }
- }
- public String toString(){
- String temp = "";
- temp += s + " " + getX() + " " + getY() + " " + getZ();
- temp += " " + getBorder() + " " + canRotate2D() + " " + canRotate3D();
- return temp;
- }
- }
- public class Sandbox {
- public static Box getRandomBox(boolean isSquare, int range){
- if (isSquare){
- return new Box(getRandomValue(range));
- } else {
- return new Box(getRandomValue(range), getRandomValue(range), getRandomValue(range));
- }
- }
- public static Box getRandomBox(boolean isSquare, int range, double precision){
- if (isSquare){
- return new Box(getRandomValue(range, precision), 1);
- } else {
- double x = getRandomValue(range, precision);
- double y = getRandomValue(range, precision);
- double z = getRandomValue(range, precision);
- return new Box(x, y, z, 1);
- }
- }
- public static double getRandomValue(int range){
- return Math.random()*range;
- }
- public static double getRandomValue(int range, double precision){
- return getRoundedValue(Math.random()*range, precision);
- }
- public static double getRoundedValue(double value, double precision){
- return ((int)(precision*value))/precision;
- }
- public static void main(String args[]) {
- try {
- Box [] boxes = new Box[100];
- for(int i = 0; i < boxes.length; ++i){
- boxes[i] = getRandomBox(Math.random() > 0.8, 100, 10);
- }
- double totalVolume = 0;
- for(int i = 0; i < boxes.length; ++i){
- totalVolume += boxes[i].getVolume(true);
- }
- System.out.println(getRoundedValue(totalVolume, 10000));
- Box box = new Box(32, 42, 52, 3);
- box.canRotate3D(true);
- System.out.println(box + " fits in: \n");
- for(int i = 0; i < boxes.length; ++i){
- if(box.canFitIn(boxes[i])){
- System.out.println(boxes[i]);
- }
- }
- } catch(Exception e){
- System.out.println(e);
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement