Skip to content

Instantly share code, notes, and snippets.

@alekseytimoshchenko
Last active May 25, 2019 12:46
Show Gist options
  • Save alekseytimoshchenko/91156773ed2551ef6c88f59a54cccea8 to your computer and use it in GitHub Desktop.
Save alekseytimoshchenko/91156773ed2551ef6c88f59a54cccea8 to your computer and use it in GitHub Desktop.
/**
* This class presents collection of {@link Box3D} objects
* in sorter consequences from the most little to the most biggest
*
* @author Aleksey Timoshchenko
* @version 15.04.19
*/
public class Collection
{
private Box3D[] _boxes;
private int _noOfBoxes;
private final static int MAX_NUM_BOXES = 100;
/**
* Default constructor
*/
public Collection()
{
_noOfBoxes = 0;
_boxes = new Box3D[MAX_NUM_BOXES];
}
/**
* Method add a new object of {@link Box3D} according to his size(sorting rule)
*
* @param base base point of the box
* @param length length of the box
* @param width width of the box
* @param height height of the box
*
* @return true if new object was successfully added to the collection otherwise false
*/
/*# Joni: bug caused an exception. see soluion below. */
/*
public boolean addBox(Point3D base, int length, int width, int height)
{
if (_boxes != null && _noOfBoxes != MAX_NUM_BOXES)
{
Box3D boxToInsert = new Box3D(base, length, width, height);
//if collection has no box just add the box like first one.
if (_noOfBoxes == 0)
{
_boxes[0] = boxToInsert;
_noOfBoxes++;
return true;
}
else
{
Box3D lastBox = _boxes[_noOfBoxes - 1];
//if box that going to be added bigger than last box in the collection
//immediately add it to the last position of the collection
if (lastBox != null && boxToInsert.getVolume() > lastBox.getVolume())
{
_boxes[_noOfBoxes++] = boxToInsert;
_noOfBoxes++;
return true;
}
else
{
int indexToInsert = 0;
for (int i = 0 ; i < _noOfBoxes ; i++)
{
Box3D currentBox = _boxes[i];
int currentVolume = currentBox.getVolume();
if (boxToInsert.getVolume() < currentVolume || boxToInsert.getVolume() == currentVolume)
{
indexToInsert = i;
break;
}
}
Box3D[] tmpCollection = new Box3D[MAX_NUM_BOXES];
//Box should be added according to the size.
for (int i = 0 ; i < _noOfBoxes + 1 ; i++)
{
if (i < indexToInsert)
{
tmpCollection[i] = _boxes[i];
}
else if (i == indexToInsert)
{
tmpCollection[i] = boxToInsert;
}
else
{
tmpCollection[i] = _boxes[i - 1];
}
}
_boxes = tmpCollection;
_noOfBoxes++;
return true;
}
}
}
else
{
return false;
}
}
*/
public boolean addBox(Point3D base, int length, int width, int height)
{
if (_noOfBoxes == MAX_NUM_BOXES)
{
return false;
}
Box3D newBox = new Box3D(new Point3D(base), length, width, height);
// Find the correct place for the new box
int i;
for (i = 0; i < _noOfBoxes && newBox.getVolume() > _boxes[i].getVolume() ; i++)
{
;
}
int index = i;
// Move other boxes to make room for the new box
for (i = _noOfBoxes; i > index ; i--)
{
_boxes[i] = _boxes[i - 1];
}
// Insert the new box in the correct place
_boxes[index] = newBox;
_noOfBoxes++;
return true;
}
/**
* Method find the {@link Box3D} with the most height base point.
*
* @return {@link Box3D} with the most height base point if collection empty return null
*/
public Box3D mostUpperBaseCorner()
{
Box3D result = _boxes[0];
for (int i = 0 ; i < _noOfBoxes ; i++)
{
Box3D box3D = _boxes[i];
Point3D basePoint = box3D.getBase();
if (basePoint.isAbove(result.getBase()))
{
result = box3D;
}
}
if (result != null)
{
result = new Box3D(result);
}
return result;
}
/**
* @return total surface area of all {@link Box3D} in the collection
*/
public double totalSurfaceArea()
{
double result = 0;
for (int i = 0 ; i < _noOfBoxes ; i++)
{
result += _boxes[i].getSurfaceArea();
}
return result;
}
/**
* Method retrieve {@link Box3D} for searching within collection and return number of coincidence
*
* @param box desired object for checking
*
* @return number of found boxes in collection
*/
public int howManyContains(Box3D box)
{
int result = 0;
for (int i = 0 ; i < _noOfBoxes ; i++)
{
// if (_boxes[i].equals(box))
if (_boxes[i].contains(box))
{
result++;
}
}
return result;
}
/**
* @return number of the {@link Box3D} objects in collection
*/
public int getNumOfBoxes()
{
/*# Joni */
// return _boxes.length;
return _noOfBoxes;
}
/**
* Method return current collection
*
* @return current collection of {@link Box3D} objects
*/
public Box3D[] getBoxes()
{
Box3D[] result = new Box3D[_noOfBoxes];
for (int i = 0 ; i < _noOfBoxes ; i++)
{
/*# Joni: aliasing */
// result[i] = _boxes[i];
result[i] = new Box3D(_boxes[i]);
}
return result;
}
public static void main(String[] args)
{
final int SIZE = 10;
Collection c = new Collection();
for (int i = 0 ; i < SIZE ; i++)
{
Point3D base = new Point3D(1 + i, 2 + i, 3 + i);
c.addBox(base, 1 + i, 1 + i, 3 + i);
}
Box3D[] boxes = c.getBoxes();
for (int i = 0 ; i < boxes.length ; i++)
{
System.out.println(String.format("Volume : %s :: %s", i, boxes[i].getVolume()));
}
final int FROM = 0;
final int TO = 3;
int smallestVol1 = c.volumeOfSmallestBox(FROM, TO);
int smallestVol2 = c.volumeOfSmallestBoxx(FROM, TO);
System.out.println(String.format("THE SMALLEST BOX VOLUME IN RANGE FROM INDEX :: %s TO INDEX :: %s IS", FROM, TO));
System.out.println("SMALLEST JONI SOLUTION Volume : " + smallestVol1);
System.out.println("SMALLEST MY SOLUTION Volume : " + smallestVol2);
}
/**
* Method should find in closed range the smallest volume of box
*
* @param i start index
* @param j end index
*
* @return volume of the smallest box
*/
/*# Joni: bug caused an exception. see soluion below. */
public int volumeOfSmallestBoxx(int i, int j)
{
int result = 0;
if (_noOfBoxes >= j + 1)
{
result = _boxes[j].getVolume();
for (int k = i ; k < j + 1 ; k++)
{
int boxVolume = _boxes[k].getVolume();
if (boxVolume < result)
{
result = boxVolume;
}
}
}
return result;
}
/*# Joni: solution */
public int volumeOfSmallestBox(int i, int j)
{
// make sure i and j are in boundaries
if (i < 0 || i >= _noOfBoxes || j < 0 || j >= _noOfBoxes)
{
return 0;
}
int h = 1, w = 1, l = 1;
// make sure j is bigger
if (i > j)
{
int temp = i;
i = j;
j = temp;
}
for ( ; i <= j ; i++)
{
if (_boxes[i].getLength() > l)
{
l = _boxes[i].getLength();
}
if (_boxes[i].getWidth() > w)
{
w = _boxes[i].getWidth();
}
if (_boxes[i].getHeight() > h)
{
h = _boxes[i].getHeight();
}
}
return (h + 1) * (l + 1) * (w + 1);
}
/**
* @return longest distance between base points
*/
public double longestDistance()
{
double result = 0;
if (_noOfBoxes > 1)
{
for (int i = 0 ; i < _noOfBoxes - 1 ; i++)
{
for (int j = i + 1 ; j < _noOfBoxes ; j++)
{
Box3D oneBox = _boxes[i];
Box3D anotherBox = _boxes[j];
double distance = oneBox.distance(anotherBox);
if (distance > result)
{
result = distance;
}
}
}
}
return result;
}
@Override
public String toString()
{
String result = "";
if (_boxes != null)
{
for (int i = 0 ; i < _noOfBoxes ; i++)
{
result += "Box no. " + (i + 1) + ": " + _boxes[i].toString() + "\n";
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment