diff --git a/src/Control/AlgorithmsHandler.java b/src/Control/AlgorithmsHandler.java index 8bb8e61..7eb85df 100644 --- a/src/Control/AlgorithmsHandler.java +++ b/src/Control/AlgorithmsHandler.java @@ -16,7 +16,7 @@ public class AlgorithmsHandler public AlgorithmsHandler() { dataBase = DataBase.getInstance(); - dataBase.createListOfKandE(); + dataBase.initializeKandE(); grahamScan = new GrahamScan(); grahamScan.initialize(); diff --git a/src/Control/GraphGUIHandler.java b/src/Control/GraphGUIHandler.java index 8d71e01..374c5d5 100644 --- a/src/Control/GraphGUIHandler.java +++ b/src/Control/GraphGUIHandler.java @@ -28,7 +28,7 @@ private void makeCircleGraph(String title, int x, int y) circleGraphGUI.setAllPoints(dataBase.getAllPoints()); circleGraphGUI.setConvexPoints(dataBase.getConvexPoints()); circleGraphGUI.setCirclePoints(dataBase.getCirclePoints()); - circleGraphGUI.setCircleObject(dataBase.findCircle()); + circleGraphGUI.setCircleObject(dataBase.getCircleShape()); circleGraphGUI.initialize(x, y); } diff --git a/src/Model/DataBase.java b/src/Model/DataBase.java index 7b0ae30..e843e49 100644 --- a/src/Model/DataBase.java +++ b/src/Model/DataBase.java @@ -23,14 +23,14 @@ public class DataBase private ArrayList K; private ArrayList> E; - private HashMap> neighbours2; - private TreeMap radiusForEachNode2; - private HashMap existingRadius2; - private Point2D currCustom2; - private Point2D prevCustom2; - private Point2D nextCustom2; - private double maxAngleCustom2; - private double maxRadiusCustom2; + private HashMap> neighbours; + private TreeMap radiusForEachNode; + private HashMap existingRadius; + private Point2D currCustom; + private Point2D prevCustom; + private Point2D nextCustom; + private double maxAngleCustom; + private double maxRadiusCustom; private ArrayList> allK; private ArrayList>> allE; @@ -63,13 +63,11 @@ public ArrayList getCirclePoints() public ArrayList getK(int mode) { return allK.get(mode); -// return K; } public ArrayList> getE(int mode) { return allE.get(mode); -// return E; } public void setAllPoints(ArrayList allPoints) @@ -99,9 +97,9 @@ private void loadMaps() int size = getConvexPointsSize(); double radius; - neighbours2 = new HashMap>(); - radiusForEachNode2 = new TreeMap<>(); - existingRadius2 = new HashMap(); + neighbours = new HashMap>(); + radiusForEachNode = new TreeMap<>(); + existingRadius = new HashMap(); for (int i = 0; i < size; i++) { @@ -114,16 +112,16 @@ private void loadMaps() kati.add(prev); kati.add(next); - neighbours2.put(curr, kati); //O(1) - radiusForEachNode2.put(radius, curr); //O(log(n)) - existingRadius2.put(radius, curr); + neighbours.put(curr, kati); //O(1) + radiusForEachNode.put(radius, curr); //O(log(n)) + existingRadius.put(radius, curr); } } //----------O(log(n)) for each calling--------------- - public Point2D findMaxP2() + public Point2D findMaxP() { - int size = neighbours2.size(); //O(1) + int size = neighbours.size(); //O(1) if (size <= 2) { @@ -132,121 +130,122 @@ public Point2D findMaxP2() if(mode == 0 || mode == 2) //circle or nearest { - maxRadiusCustom2 = radiusForEachNode2.lastKey(); //O(1) + maxRadiusCustom = radiusForEachNode.lastKey(); //O(1) }else //mode == 1 farthest { - maxRadiusCustom2 = radiusForEachNode2.firstKey(); //O(1) + maxRadiusCustom = radiusForEachNode.firstKey(); //O(1) } - currCustom2 = radiusForEachNode2.get(maxRadiusCustom2); //O(log(n)) - prevCustom2 = neighbours2.get(currCustom2).get(0); //O(1) - nextCustom2 = neighbours2.get(currCustom2).get(1); //O(1) - maxAngleCustom2 = getAngle(prevCustom2, currCustom2, nextCustom2); //O(1) + currCustom = radiusForEachNode.get(maxRadiusCustom); //O(log(n)) + prevCustom = neighbours.get(currCustom).get(0); //O(1) + nextCustom = neighbours.get(currCustom).get(1); //O(1) + maxAngleCustom = getAngle(prevCustom, currCustom, nextCustom); //O(1) if(mode == 0) { - return new Point2D(maxAngleCustom2, maxAngleCustom2); + return new Point2D(maxAngleCustom, maxAngleCustom); } - return currCustom2; + return currCustom; + } + +//----------5*O(log(n)) for each calling--------------- + public void deleteMaxP2() + { + ArrayList left = new ArrayList(); + ArrayList right = new ArrayList(); + + //---I found that has changed + Point2D leftCurr = neighbours.get(currCustom).get(0); + Point2D leftPrev = neighbours.get(leftCurr).get(0); + Point2D leftNext = neighbours.get(leftCurr).get(1); + + Point2D rightCurr = neighbours.get(currCustom).get(1); //O(1) + Point2D rightPrev = neighbours.get(rightCurr).get(0); //O(1) + Point2D rightNext = neighbours.get(rightCurr).get(1); //O(1) + + radiusForEachNode.remove(maxRadiusCustom); //O(log(n)) + radiusForEachNode.remove(getRadius(leftPrev, leftCurr, leftNext)); //O(log(n)) + radiusForEachNode.remove(getRadius(rightPrev, rightCurr, rightNext)); //O(log(n)) + + existingRadius.remove(maxRadiusCustom);//O(1) + existingRadius.remove(getRadius(leftPrev, leftCurr, leftNext)); //O(1) + existingRadius.remove(getRadius(rightPrev, rightCurr, rightNext)); //O(1)) + + neighbours.remove(leftCurr); //O(1) + neighbours.remove(currCustom); //O(1) + neighbours.remove(rightCurr); //O(1) + + left.add(leftPrev); //O(1) + left.add(rightCurr); //O(1) + + right.add(leftCurr); //O(1) + right.add(rightNext); //O(1) + + neighbours.put(leftCurr, left); //O(1) + neighbours.put(rightCurr, right); //O(1) + + double beforeRadius = getRadius(leftPrev, leftCurr, rightCurr); + double nextRadius = getRadius(leftCurr, rightCurr, rightNext); + Point2D currPoint; + + if(!existingRadius.containsKey(beforeRadius)) //O(1) + { + radiusForEachNode.put(beforeRadius, leftCurr); //O(log(n)) + existingRadius.put(beforeRadius, leftCurr);//O(1) + + }else + { + currPoint = existingRadius.get(beforeRadius);//O(1) + + if(getAngle(neighbours.get(leftCurr).get(0), leftCurr, neighbours.get(leftCurr).get(1)) >= getAngle(neighbours.get(currPoint).get(0), currPoint, neighbours.get(currPoint).get(1))) + { + radiusForEachNode.put(getRadius(neighbours.get(currPoint).get(0), currPoint, neighbours.get(currPoint).get(1)), leftCurr); //O(log(n)) + existingRadius.put(getRadius(neighbours.get(currPoint).get(0), currPoint, neighbours.get(currPoint).get(1)), leftCurr); //O(1) + } + } + if(!existingRadius.containsKey(nextRadius)) //O(1) + { + radiusForEachNode.put(nextRadius, rightCurr); //O(log(n)) + existingRadius.put(nextRadius, rightCurr);//O(1) + }else + { + currPoint = existingRadius.get(nextRadius);//O(1) + + if(getAngle(neighbours.get(rightCurr).get(0), rightCurr, neighbours.get(rightCurr).get(1)) >= getAngle(neighbours.get(currPoint).get(0), currPoint, neighbours.get(currPoint).get(1))) + { + radiusForEachNode.put(getRadius(neighbours.get(currPoint).get(0), currPoint, neighbours.get(currPoint).get(1)), rightCurr); //O(log(n)) + existingRadius.put(getRadius(neighbours.get(currPoint).get(0), currPoint, neighbours.get(currPoint).get(1)), rightCurr); //O(1) + } + } } public int getHashCirclePointsSize() { - return neighbours2.size(); + return neighbours.size(); } public void moveToCircleArray() { HashMap neighboursTemp = new HashMap(); - for (Point2D key : neighbours2.keySet()) + for (Point2D key : neighbours.keySet()) { neighboursTemp.put(key, key); } circlePoints = new ArrayList(neighboursTemp.values()); } - public Point2D getPrev() - { - return prevCustom2; - } - - public Point2D getNext() - { - return nextCustom2; - } + public Point2D getPrev() + { + return prevCustom; + } + + public Point2D getNext() + { + return nextCustom; + } - //----------5*O(log(n)) for each calling--------------- - public void deleteMaxP2() - { - ArrayList left = new ArrayList(); - ArrayList right = new ArrayList(); - - //---I found that has changed - Point2D leftCurr = neighbours2.get(currCustom2).get(0); - Point2D leftPrev = neighbours2.get(leftCurr).get(0); - Point2D leftNext = neighbours2.get(leftCurr).get(1); - - Point2D rightCurr = neighbours2.get(currCustom2).get(1); //O(1) - Point2D rightPrev = neighbours2.get(rightCurr).get(0); //O(1) - Point2D rightNext = neighbours2.get(rightCurr).get(1); //O(1) - - radiusForEachNode2.remove(maxRadiusCustom2); //O(log(n)) - radiusForEachNode2.remove(getRadius(leftPrev, leftCurr, leftNext)); //O(log(n)) - radiusForEachNode2.remove(getRadius(rightPrev, rightCurr, rightNext)); //O(log(n)) - - existingRadius2.remove(maxRadiusCustom2);//O(1) - existingRadius2.remove(getRadius(leftPrev, leftCurr, leftNext)); //O(1) - existingRadius2.remove(getRadius(rightPrev, rightCurr, rightNext)); //O(1)) - - neighbours2.remove(leftCurr); //O(1) - neighbours2.remove(currCustom2); //O(1) - neighbours2.remove(rightCurr); //O(1) - - left.add(leftPrev); //O(1) - left.add(rightCurr); //O(1) - - right.add(leftCurr); //O(1) - right.add(rightNext); //O(1) - - neighbours2.put(leftCurr, left); //O(1) - neighbours2.put(rightCurr, right); //O(1) - - double beforeRadius = getRadius(leftPrev, leftCurr, rightCurr); - double nextRadius = getRadius(leftCurr, rightCurr, rightNext); - Point2D currPoint; - - if(!existingRadius2.containsKey(beforeRadius)) //O(1) - { - radiusForEachNode2.put(beforeRadius, leftCurr); //O(log(n)) - existingRadius2.put(beforeRadius, leftCurr);//O(1) - - }else - { - currPoint = existingRadius2.get(beforeRadius);//O(1) - - if(getAngle(neighbours2.get(leftCurr).get(0), leftCurr, neighbours2.get(leftCurr).get(1)) >= getAngle(neighbours2.get(currPoint).get(0), currPoint, neighbours2.get(currPoint).get(1))) - { - radiusForEachNode2.put(getRadius(neighbours2.get(currPoint).get(0), currPoint, neighbours2.get(currPoint).get(1)), leftCurr); //O(log(n)) - existingRadius2.put(getRadius(neighbours2.get(currPoint).get(0), currPoint, neighbours2.get(currPoint).get(1)), leftCurr); //O(1) - } - } - if(!existingRadius2.containsKey(nextRadius)) //O(1) - { - radiusForEachNode2.put(nextRadius, rightCurr); //O(log(n)) - existingRadius2.put(nextRadius, rightCurr);//O(1) - }else - { - currPoint = existingRadius2.get(nextRadius);//O(1) - - if(getAngle(neighbours2.get(rightCurr).get(0), rightCurr, neighbours2.get(rightCurr).get(1)) >= getAngle(neighbours2.get(currPoint).get(0), currPoint, neighbours2.get(currPoint).get(1))) - { - radiusForEachNode2.put(getRadius(neighbours2.get(currPoint).get(0), currPoint, neighbours2.get(currPoint).get(1)), rightCurr); //O(log(n)) - existingRadius2.put(getRadius(neighbours2.get(currPoint).get(0), currPoint, neighbours2.get(currPoint).get(1)), rightCurr); //O(1) - } - } - } public int getAllPointsSize() { @@ -263,7 +262,7 @@ public int getCirclePointsSize() return circlePoints.size(); } - public Ellipse2D findCircle() + public Ellipse2D getCircleShape() { int n = getCirclePointsSize(); Ellipse2D circle; @@ -435,7 +434,7 @@ private double getAngle(Point2D p, Point2D q, Point2D r) public Point2D getUp(Point2D p) { - Point2D nextP = neighbours2.get(p).get(1); + Point2D nextP = neighbours.get(p).get(1); double mx = (p.getX() + nextP.getX()) / 2.0; double my = (p.getY() + nextP.getY()) / 2.0; double dx = nextP.getY() - p.getY(); @@ -478,14 +477,11 @@ public void makeKandE() public void moveKandE() { -// allK = new ArrayList>(); allK.add(K); - -// allE = new ArrayList>>(); allE.add(E); } - public void createListOfKandE() + public void initializeKandE() { allK = new ArrayList>(); allE = new ArrayList>>(); diff --git a/src/Model/SmallestEnclosingCirlceAlgorithm1.java b/src/Model/SmallestEnclosingCirlceAlgorithm1.java index f7f13d6..588cb88 100644 --- a/src/Model/SmallestEnclosingCirlceAlgorithm1.java +++ b/src/Model/SmallestEnclosingCirlceAlgorithm1.java @@ -36,7 +36,7 @@ public void computeSmallestEnclosingCircle() finish = false; do { - maxAngle = dataBase.findMaxP2().getX(); + maxAngle = dataBase.findMaxP().getX(); if(maxAngle>myPi2) { dataBase.deleteMaxP2(); diff --git a/src/Model/VoronoiAlgorithm2and3.java b/src/Model/VoronoiAlgorithm2and3.java index 669db4d..b63cae8 100644 --- a/src/Model/VoronoiAlgorithm2and3.java +++ b/src/Model/VoronoiAlgorithm2and3.java @@ -30,7 +30,7 @@ public void computeVoronoiDiagram() do { //find p maximizing radius and angle - p = dataBase.findMaxP2(); //=0 for maximizing + p = dataBase.findMaxP(); //=0 for maximizing before_p = dataBase.getPrev(); next_p = dataBase.getNext(); c = dataBase.getCenter(before_p, p, next_p);