Advertisement
bokoness

ממן 15 שאלה 3

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