Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Polygon here.
- *
- * @author (Liron Mor-Yosef)
- * @version (22/06/17)
- */
- public class Polygon
- {
- private PointNode _head;
- /**
- * 1st constructor empty constructor
- */
- public Polygon(){
- _head = null;
- }
- public boolean addVertex(Point p, int pos){
- if (pos<1)
- return false;
- if(_head == null)
- {
- if (pos == 1){
- _head=new PointNode(p);
- return true;
- }
- return false;
- }
- else{
- if (pos == 1){
- PointNode temp = _head;
- _head = new PointNode(p);
- _head.setNext(temp);
- return true;
- }
- PointNode curr = _head;
- int count = 1;
- while ( curr.getNext() != null && count+1 < pos) {
- curr = curr.getNext();
- count++;
- }
- if(count+1 <pos)
- return false;
- else{
- // בודק אם הקודקוד שהתקבל צריך להתווסף לסוף הרשימה
- if ( curr.getNext() == null ){
- PointNode temp = new PointNode(p);
- curr.setNext(temp);
- }
- // בודק אם הקודקוד שהתקבל צריך להתווסף לאמצע הרשימה
- else{
- curr = curr.getNext();
- PointNode temp = new PointNode(curr.getPoint(),curr.getNext()); //הקודקוד הנוכחי נשמר במצביע זמני
- curr.setPoint(p); // הקודקוד שאני מוסיף שמתי במצביע הנוכחי
- curr.setNext(temp);// הקודקוד החדש יצביע על הקודקוד הישן שזז קדימה
- }
- return true;
- }
- }
- }
- public Point highestVertex(){
- // if the list empty
- if ( _head == null)
- return null;
- // default value
- PointNode curr = _head;
- PointNode highestPoint = curr;
- while (curr.getNext() != null){
- curr = curr.getNext();
- if (curr.getPoint().isAbove( highestPoint.getPoint())){
- highestPoint.setPoint(curr.getPoint());
- }
- }
- return new Point (highestPoint.getPoint());
- }
- public String toString(){
- PointNode temp = _head;
- int count= 0;
- if ( _head == null)
- return "The polygon has 0 vertices.";
- String str2= "(" ;
- while (temp != null){
- str2 += "" + temp.getPoint();
- if (temp.getNext()!= null)
- str2 += ",";
- temp = temp.getNext();
- count++;
- }
- str2 = "The polygon has " + count + " vertices:" + "\n" +str2 + ")" ;
- return str2;
- }
- public double calcPerimeter(){
- double perimeter = 0;
- int count = length();
- // variables
- PointNode curr = _head;
- Point firstPoint = _head.getPoint();
- while (curr != null) {
- curr = curr.getNext();
- count++;
- }
- // If there is no line between two points there is no perimeter
- if (curr == null || count == 1)
- return perimeter;
- // The perimeter is only the distance between two points
- if (count == 2 ){
- perimeter = curr.getPoint().distance(curr.getNext().getPoint());
- return perimeter; }
- //Summation the polygon lengths to the perimeter calculation
- if(count >= 3)
- while (curr != null){
- perimeter += curr.getPoint().distance(curr.getNext().getPoint());
- curr = curr.getNext();
- }
- perimeter += curr.getPoint().distance(firstPoint);
- return perimeter;
- }
- public double calcArea(){
- if (_head == null || _head.getNext()==null || _head.getNext().getNext() == null )
- return 0;
- PointNode p1 = _head;
- PointNode p2 = p1.getNext();
- PointNode p3 = p1.getNext().getNext();
- double area = heron(p1.getPoint(),p2.getPoint(),p3.getPoint());
- while( p3.getNext()!=null){
- p2 = p3;
- p3 = p3.getNext();
- area += heron(p1.getPoint(),p2.getPoint(),p3.getPoint());
- }
- return area;
- }
- /*
- public double calcArea(){
- PointNode curr = _head.getNext();
- double area = 0;
- int count = length();
- //If no triangle is formed there is no area
- if (count < 3 )
- return 0;
- /* Calculating the area of the triangles representing the polygon
- using the Heron formula for calculating the polygon area
- while (curr.getNext() != null ){
- area = area + heron( _head.getPoint(),curr.getPoint(),curr.getNext().getPoint());
- curr = curr.getNext();
- }
- return area;
- }
- */
- public boolean isBigger(Polygon other) {
- if ( this.calcArea() > other.calcArea() )
- return true;
- return false;
- }
- public int findVertex(Point p){
- // if the list empty
- if ( _head == null)
- return -1;
- PointNode temp = _head;
- int count = 1;
- //Search for the received point if it is the same as one of the array points
- while ( temp.getNext() != null ){
- if (p.equals(temp.getPoint()))
- return count;
- count++;
- temp = temp.getNext();
- }
- // If the point is not found...
- return -1;
- }
- public Point getNextVertex(Point p ){
- PointNode firstPoint = _head;
- PointNode temp = _head;
- // if the list empty
- if ( _head == null)
- return null;
- // The point is the only point in the array
- if ( length() == 1)
- return new Point (p);
- // If the point is the last member of the array we will return the first point
- if ( length() > 0 && temp.getNext().getPoint() == null && p.equals(temp.getPoint()))
- return new Point(firstPoint.getPoint());
- // Return the point after the point which recieved in the array
- while(temp != null ){
- if (p.equals(temp.getPoint()))
- return new Point( temp.getNext().getPoint());
- temp = temp.getNext();
- }
- //If the point is not a vertex in the polygon
- return null;
- }
- // צריך לממש
- /* public Polygon getBoundingBox(){
- int count = length();
- PointNode curr = _head;
- if (count < 3)
- return null;
- // Declaration & Initialization Default value
- Point leftVert = curr.getPoint();
- Point rightVert = curr.getPoint();
- Point lowVert = curr.getPoint();
- double highVert = curr.getPoint().getY();
- // Finding the extreme points of the polygon
- while (curr != null){
- //Finding the leftmost point
- if (leftVert.isRight(curr.getPoint()))
- leftVert = curr.getPoint();
- //Finding the rightmost point
- if (rightVert.isLeft(curr.getPoint()))
- rightVert = curr.getPoint();
- //Finding the lowest point
- if (lowVert.isAbove(curr.getPoint()))
- lowVert = curr.getPoint();
- // Finding the highest point
- highVert = highestVertex().getY();
- curr = curr.getNext();
- }
- // Create Bounding Box
- Polygon Box = new Polygon();
- Box.addVertex( leftVert)); // The lowest leftmost point
- Box.addVertex( rightVert); // The lowest rightmost point
- Box.addVertex( rightVert.getX(),highVert); // The highest rightmost point
- Box.addVertex( leftVert.getX(),highVert); // The highest leftmost point
- return Box;
- }*/
- // private method
- private int length (){
- PointNode temp = _head;
- int count = 0;
- // if(_head == null)
- // return count;
- while (temp != null ){
- count++;
- temp = temp.getNext();
- }
- return count;
- }
- // Private method
- // A method that calculates the area of a triangle by applying the Heron formula.
- private double heron( Point p1, Point p2, Point p3){
- //Calculation of sides lengths of the triangle
- double a = p1.distance(p2); // p1 = _vertices[0]
- double b = p2.distance(p3); // p2 = _vertices[i]
- double c = p3.distance(p1); // p3 = _vertices[i+1]
- // Using Heron formula
- double s = (a+b+c) / 2;
- double heronCal;
- heronCal = Math.sqrt( s * (s-a)*(s-b)*(s-c));
- return heronCal;
- }
- /*
- // Private method
- // A method that calculates the area of a triangle by applying the Heron formula.
- private double heron( PointNode p1, PointNode p2, PointNode p3){
- //Calculation of sides lengths of the triangle
- double a = getLength(p1,p2); // p1 = _vertices[0]
- double b = getLength(p2,p3); // p2 = _vertices[i]
- double c = getLength(p1,p3); // p3 = _vertices[i+1]
- // Using Heron formula
- double s = (a+b+c) / 2;
- double heronCal = Math.sqrt( s * (s-a)*(s-b)*(s-c));
- return heronCal;
- }
- /*
- private double getLength(PointNode a, PointNode b ){
- double deltaX = a.getPoint().getX()- b.getPoint().getX();
- double deltaY = a.getPoint().getY()- b.getPoint().getY();
- return Math.sqrt(Math.pow(deltaX,2) +Math.pow(deltaY,2));
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement