Advertisement
bokoness

mmn 15 q3

Jun 23rd, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.85 KB | None | 0 0
  1. //TODO: add time and space comlexity to all methods in all classes
  2. //TODO: remove
  3. package Mavo2020.Playground;
  4.  
  5. /**
  6.  * Represent a polygon
  7.  */
  8. public class Polygon {
  9.     private PointNode _head;
  10.  
  11.     public Polygon() {
  12.         this._head = null;
  13.     }
  14.  
  15.     /**
  16.      * Adds a new PointNode to the Polygon list
  17.      * @param p the new point
  18.      * @param pos the wanted position of the new node
  19.      * @return true if the node added successfully
  20.      * O(?)
  21.      */
  22.     public boolean addVertex(Point p, int pos) {
  23.         int polyLen = this.listLength();
  24.         int count;
  25.         PointNode pointer, next, prev;
  26.  
  27.         if(p == null)
  28.             return false;
  29.         //if position if higher then the list length + 1 or its smaller then 1
  30.         if(pos > polyLen + 1 || pos < 1)
  31.             return false;
  32.  
  33.         //if list is empty
  34.         if (this._head == null) {
  35.             this._head = new PointNode(p);
  36.             return true;
  37.         }
  38.         // if position is higher then the current list length (but no more then 1)
  39.         if (pos > polyLen) {
  40.             pointer = this._head;
  41.             while(pointer.getNext() != null)
  42.                 pointer = pointer.getNext();
  43.             prev = pointer;
  44.             pointer = new PointNode(p);
  45.             prev.setNext(pointer);
  46.             return true;
  47.         }
  48.  
  49.         // if position is already taken - push the new point there, and push the rest of the list forward
  50.         count = 1;
  51.         prev = this._head;
  52.         while(count != pos -1) {
  53.             prev = prev.getNext();
  54.             count ++;
  55.         }
  56.  
  57.         pointer = new PointNode(p);
  58.         next = prev.getNext();
  59.         prev.setNext(pointer);
  60.         pointer.setNext(next);
  61.  
  62.         return true;
  63.     }
  64.  
  65.     /**
  66.      * Finds the highest point in the y axis of the polygon list
  67.      * @return the highest point in the list
  68.      * O(?)
  69.      */
  70.     public Point highestVertex() {
  71.         int len = this.listLength();
  72.         if (len == 0)
  73.             return null;
  74.         if(len == 1)
  75.             return this._head.getPoint();
  76.  
  77.         PointNode pointer = this._head, next = this._head.getNext();
  78.         Point highest = this._head.getPoint();
  79.         while(next != null) {
  80.             if(next.getPoint().getY() > highest.getY())
  81.                 highest = next.getPoint();
  82.             next = next.getNext();
  83.         }
  84.         return new Point(highest);
  85.     }
  86.  
  87.     /**
  88.      * creates a string representation of the polygon
  89.      * @return the representation string of the polygon
  90.      * O(?)
  91.      */
  92.     public String toString() {
  93.         int len = this.listLength();
  94.         if(len == 0)
  95.             return "The polygon has 0 vertices.";
  96.         else {
  97.             PointNode pointer = this._head;
  98.             String str = "The polygon has " + len + " vertices:\n(";
  99.             while (pointer != null) {
  100.                 str += pointer.getPoint().toString();
  101.                 //add "," to all items except the last one
  102.                 str += pointer.getNext() == null ? "" : ",";
  103.                 pointer = pointer.getNext();
  104.             }
  105.             str += ")";
  106.             return str;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Calculates the perimeter of the polygon
  112.      * @return the perimeter of the polygon
  113.      * O(?)
  114.      */
  115.     public double calcPerimeter() {
  116.         int len = this.listLength();
  117.         double perimeter = 0;
  118.  
  119.         if(len == 0 || len == 1)
  120.             return 0;
  121.  
  122.         if(len == 2)
  123.             return _head.getPoint().distance(_head.getNext().getPoint());
  124.  
  125.         PointNode pointer = this._head, next = this._head;
  126.         while(pointer.getNext() != null) {
  127.             next = pointer.getNext();
  128.             perimeter += pointer.getPoint().distance(next.getPoint());
  129.             pointer = pointer.getNext();
  130.         }
  131.         perimeter += pointer.getPoint().distance(this._head.getPoint());
  132.         return perimeter;
  133.     }
  134.  
  135.     /**
  136.      * calculates the area of the polygon
  137.      * @return the area of the polygon
  138.      * O(?)
  139.      */
  140.     public double calcArea() {
  141.         int len = this.listLength();
  142.  
  143.         if (len < 3)
  144.             return 0;
  145.  
  146.         double totalSum = 0, sumA = 0, sumB = 0, xa, ya, xb, yb;
  147.  
  148.         PointNode pointer = this._head, next = this._head;
  149.         while(pointer.getNext() != null) {
  150.             next = pointer.getNext();
  151.             /*xa = pointer.getPoint().getX();
  152.             ya = next.getPoint().getY();
  153.             yb = pointer.getPoint().getY();
  154.             xb = next.getPoint().getX();*/
  155.             sumA = pointer.getPoint().getX() * next.getPoint().getY();
  156.             sumB = pointer.getPoint().getY() * next.getPoint().getX();
  157.             totalSum += sumA - sumB;
  158.             pointer = next;
  159.         }
  160.         next = this._head;
  161.         sumA = pointer.getPoint().getX() * next.getPoint().getY();
  162.         sumB = pointer.getPoint().getY() * next.getPoint().getX();
  163.         totalSum += sumA -sumB;
  164.         totalSum /= 2;
  165.         return totalSum < 0 ? totalSum * -1 : totalSum;
  166.     }
  167.  
  168.     /**
  169.      * Check if this polygon's area is bigger the the other polygon's area
  170.      * @param other the other polygon
  171.      * @return true if this polygon's area is bigger then the other polygon's area
  172.      * O(?)
  173.      */
  174.     public boolean isBigger(Polygon other) {
  175.         return this.calcArea() > other.calcArea();
  176.     }
  177.  
  178.     /**
  179.      * finds a vertex in the polygon
  180.      * @param p the wanted vertex
  181.      * @return the vertex's number in the list if it is found, and -1 if not
  182.      * O(?)
  183.      */
  184.     public int findVertex(Point p) {
  185.         int len = this.listLength(), idx = 1;
  186.         PointNode pointer = this._head;
  187.  
  188.         if(len == 0)
  189.             return -1;
  190.  
  191.         while(pointer != null) {
  192.             if(pointer.getPoint().equals(p))
  193.                 return idx;
  194.             pointer = pointer.getNext();
  195.             idx ++;
  196.         }
  197.         return -1;
  198.     }
  199.  
  200.     /**
  201.      * Finds the point that comes after the wanted vertex
  202.      * @param p the wanted vertex
  203.      * @return the next point after p if found
  204.      * O(?)
  205.      */
  206.     public Point getNextVertex(Point p) {
  207.         int len = this.listLength();
  208.  
  209.         if(len == 0)
  210.             return null;
  211.         if(len == 1 && this._head.getPoint().equals(p))
  212.             return new Point(this._head.getPoint());
  213.  
  214.         PointNode pointer = this._head;
  215.         while(pointer != null) {
  216.             if(pointer.getPoint().equals(p))
  217.                 //if pointer is the last item in list, return a copy of the head's point
  218.                 //else return the copy of pointer's point
  219.                 return pointer.getNext() == null ? new Point(this._head.getPoint()) : new Point(pointer.getNext().getPoint());
  220.             pointer = pointer.getNext();
  221.         }
  222.         //if point didn't found return null
  223.         return null;
  224.     }
  225.  
  226.     /**
  227.      * Creates a rectangle polygon that surrounds and block our polygon
  228.      * @return a rectangle polygon that surrounds and block our polygon
  229.      * O(?)
  230.      */
  231.     public Polygon getBoundingBox() {
  232.         int len = this.listLength();
  233.         if(len < 3)
  234.             return null;
  235.  
  236.         PointNode pointer = this._head;
  237.         double currentX = pointer.getPoint().getX();
  238.         double currentY = pointer.getPoint().getY();
  239.         //those points are the 4 points of the rectangle
  240.         double highestX = currentY, lowestX = currentX, highestY = currentY, lowestY = currentY;
  241.  
  242.         while(pointer != null) {
  243.             currentX = pointer.getPoint().getX();
  244.             currentY = pointer.getPoint().getY();
  245.  
  246.             //searching for the lowest and highest X
  247.             if(currentX > highestX)
  248.                 highestX = currentX;
  249.             else if(currentX < lowestX)
  250.                 lowestX = currentX;
  251.  
  252.             //searching for the lowest and highest Y
  253.             if(currentY > highestY)
  254.                 highestY = currentY;
  255.             else if(currentY < lowestY)
  256.                 lowestY = currentY;
  257.  
  258.             pointer = pointer.getNext();
  259.         }
  260.         //the top points of the rectangle
  261.         Point topLeft = new Point(lowestX, highestY), topRight = new Point(highestX, highestY);
  262.         //the bottom points of the rectangle
  263.         Point bottomLeft = new Point(lowestX, lowestY), bottomRight = new Point(highestX, lowestY);
  264.  
  265.         //creating the new rectangle polygon
  266.         Polygon blockRect = new Polygon();
  267.         blockRect.addVertex(topLeft, 1);
  268.         blockRect.addVertex(topRight, 2);
  269.         blockRect.addVertex(bottomRight, 3);
  270.         blockRect.addVertex(bottomLeft, 4);
  271.  
  272.         return blockRect;
  273.     }
  274.  
  275.     //return the length of the list
  276.     private int listLength() {
  277.         int len = 0;
  278.         PointNode temp = this._head;
  279.         while(temp != null) {
  280.             temp = temp.getNext();
  281.             len++;
  282.         }
  283.  
  284.         return len;
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement