Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Polygon here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Polygon
- {
- private PointNode _head;
- public Polygon()
- {
- _head = null;
- }
- public boolean addVertex ( Point p , int pos)
- {
- PointNode p1= new PointNode(p);
- if(pos <=0 )
- return false;
- if(_head==null){
- _head = new PointNode(p,null);
- return true;
- }
- if(_head.getNext()==null){
- _head.setNext(p1);
- return true;
- }
- else
- {
- PointNode t,prev;
- t=_head;
- prev=null;
- for (int i = 0;i!= pos-1;i++){
- prev=t;
- t=t.getNext();
- }
- if(prev != null){
- p1.setNext(t);
- prev.setNext(p1);
- }
- return true;
- }
- }
- public Point highestVertex()
- {
- if ( _head == null)
- return null;
- Point Higest= _head.getPoint();
- for(PointNode p= _head ; p != null; p=p.getNext()){
- if ((p.getPoint()).isAbove(Higest))
- Higest = p.getPoint();
- }
- return new Point(Higest);
- }
- public String toString()
- {
- if(_head!=null)
- {
- int count = 0;
- String toReturn = " " ;
- toReturn = "(" +(_head.getPoint()).toString();
- for (PointNode p= _head.getNext() ; p != null; p=p.getNext())
- {
- toReturn+= ","+ p.getPoint() ;
- count++;
- }
- return "The polygon has " + count + " vertices:\n" + toReturn + ")" ;
- }
- else
- return "The polygon has 0 vertices.";
- }
- public double calcPerimeter ()
- {
- if(_head==null || _head.getNext()== null)
- return 0;
- if((_head.getNext()).getNext()== null)
- return (_head.getPoint()).distance((_head.getNext()).getPoint());
- else
- {
- double Perimeter =0;
- PointNode p= _head;
- while ( p.getNext()!= null)
- {
- Perimeter += (p.getPoint()).distance((p.getNext()).getPoint());
- p=p.getNext();
- }
- Perimeter+=p.getPoint().distance(_head.getPoint());
- return Perimeter;
- }
- }
- public double calcArea()
- {
- if(_head==null || _head.getNext()== null)
- return 0;
- else
- {
- double area = 0;
- PointNode p= _head;
- while ((p.getNext()).getNext()!= null){
- //Parameters for Heron's Formula
- //********************
- double a = (_head.getPoint()).distance (((p.getNext()).getPoint())) ; // Side of the triangle
- double b = (p.getNext().getPoint()).distance ((p.getNext()).getNext().getPoint()) ; // Side of the triangle
- double c = (p.getNext()).getNext().getPoint().distance (_head.getPoint());// Side of the triangle
- double s = (a+b+c)/2; // Half of perimeter
- //********************
- area+=Math.sqrt(s * (s - a) * (s - b) * (s - c)); // Calculates area using Heron's Formula
- p=p.getNext();
- } // end of for loop
- return area;
- } // end of if
- }
- public boolean isBigger(Polygon other)
- {
- if (this.calcArea() > other.calcArea())
- return true;
- else
- return false;
- }
- public int findVertex(Point p)
- {
- PointNode t= _head;
- int count =0;
- while (t!= null){
- count++;
- {
- if((t.getPoint()).equals(p))
- return count;
- }
- t=t.getNext();
- }
- return -1;
- }
- public Point getNextVertex(Point p)
- {
- if(this.findVertex (p)== -1)
- return null;
- else{
- PointNode t= _head;
- while (t.getNext()!= null){
- if((t.getPoint()).equals(p)){
- return new Point( t.getNext().getPoint());
- }
- t=t.getNext();
- }
- }
- return new Point(_head.getPoint());
- }
- public Polygon getBoundingBox()
- {
- if (_head.getNext().getNext() == null|| _head== null)
- return null;
- Point Down = _head.getPoint();
- Point Up = this.highestVertex();
- Point Right = _head.getPoint();
- Point Left = _head.getPoint();
- PointNode t= _head.getNext();
- while (t.getNext() != null){
- if( t.getPoint().isUnder(Down))
- Down= t.getPoint();
- if( t.getPoint().isRight(Right))
- Right= t.getPoint();
- if( t.getPoint().isLeft(Left))
- Left= t.getPoint();
- t = t.getNext();
- }
- Polygon BoundingBox = new Polygon();
- BoundingBox.addVertex(new Point(Left.getX(),Down.getY()), 1);
- BoundingBox.addVertex(new Point(Right.getX(),Down.getY()), 2);
- BoundingBox.addVertex(new Point(Right.getX(),Up.getY()), 3);
- BoundingBox.addVertex(new Point(Left.getX(),Up.getY()),4);
- return BoundingBox;
- } // end of method getBoundingBox
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement