Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1.  
  2. /**
  3. * Write a description of class Polygon here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. public class Polygon
  9. {
  10. private PointNode _head;
  11.  
  12. public Polygon()
  13. {
  14. _head = null;
  15. }
  16.  
  17. public boolean addVertex ( Point p , int pos)
  18. {
  19. PointNode p1= new PointNode(p);
  20. if(pos <=0 )
  21. return false;
  22. if(_head==null){
  23. _head = new PointNode(p,null);
  24. return true;
  25. }
  26. if(_head.getNext()==null){
  27. _head.setNext(p1);
  28. return true;
  29. }
  30. else
  31. {
  32. PointNode t,prev;
  33. t=_head;
  34. prev=null;
  35. for (int i = 0;i!= pos-1;i++){
  36. prev=t;
  37. t=t.getNext();
  38. }
  39. if(prev != null){
  40. p1.setNext(t);
  41. prev.setNext(p1);
  42. }
  43. return true;
  44. }
  45. }
  46.  
  47. public Point highestVertex()
  48. {
  49. if ( _head == null)
  50. return null;
  51. Point Higest= _head.getPoint();
  52. for(PointNode p= _head ; p != null; p=p.getNext()){
  53. if ((p.getPoint()).isAbove(Higest))
  54. Higest = p.getPoint();
  55. }
  56. return new Point(Higest);
  57. }
  58.  
  59. public String toString()
  60. {
  61. if(_head!=null)
  62. {
  63. int count = 0;
  64. String toReturn = " " ;
  65. toReturn = "(" +(_head.getPoint()).toString();
  66. for (PointNode p= _head.getNext() ; p != null; p=p.getNext())
  67. {
  68. toReturn+= ","+ p.getPoint() ;
  69. count++;
  70. }
  71. return "The polygon has " + count + " vertices:\n" + toReturn + ")" ;
  72. }
  73. else
  74. return "The polygon has 0 vertices.";
  75.  
  76. }
  77.  
  78. public double calcPerimeter ()
  79. {
  80. if(_head==null || _head.getNext()== null)
  81. return 0;
  82. if((_head.getNext()).getNext()== null)
  83. return (_head.getPoint()).distance((_head.getNext()).getPoint());
  84. else
  85. {
  86. double Perimeter =0;
  87. PointNode p= _head;
  88. while ( p.getNext()!= null)
  89. {
  90. Perimeter += (p.getPoint()).distance((p.getNext()).getPoint());
  91. p=p.getNext();
  92. }
  93. Perimeter+=p.getPoint().distance(_head.getPoint());
  94. return Perimeter;
  95. }
  96. }
  97.  
  98. public double calcArea()
  99. {
  100. if(_head==null || _head.getNext()== null)
  101. return 0;
  102. else
  103. {
  104. double area = 0;
  105. PointNode p= _head;
  106. while ((p.getNext()).getNext()!= null){
  107. //Parameters for Heron's Formula
  108. //********************
  109. double a = (_head.getPoint()).distance (((p.getNext()).getPoint())) ; // Side of the triangle
  110. double b = (p.getNext().getPoint()).distance ((p.getNext()).getNext().getPoint()) ; // Side of the triangle
  111. double c = (p.getNext()).getNext().getPoint().distance (_head.getPoint());// Side of the triangle
  112. double s = (a+b+c)/2; // Half of perimeter
  113. //********************
  114. area+=Math.sqrt(s * (s - a) * (s - b) * (s - c)); // Calculates area using Heron's Formula
  115. p=p.getNext();
  116. } // end of for loop
  117. return area;
  118. } // end of if
  119.  
  120. }
  121. public boolean isBigger(Polygon other)
  122. {
  123. if (this.calcArea() > other.calcArea())
  124. return true;
  125. else
  126. return false;
  127. }
  128.  
  129. public int findVertex(Point p)
  130. {
  131. PointNode t= _head;
  132. int count =0;
  133. while (t!= null){
  134. count++;
  135. {
  136. if((t.getPoint()).equals(p))
  137. return count;
  138. }
  139. t=t.getNext();
  140. }
  141. return -1;
  142. }
  143.  
  144.  
  145. public Point getNextVertex(Point p)
  146. {
  147. if(this.findVertex (p)== -1)
  148. return null;
  149. else{
  150. PointNode t= _head;
  151. while (t.getNext()!= null){
  152. if((t.getPoint()).equals(p)){
  153. return new Point( t.getNext().getPoint());
  154. }
  155. t=t.getNext();
  156. }
  157. }
  158. return new Point(_head.getPoint());
  159. }
  160.  
  161. public Polygon getBoundingBox()
  162. {
  163. if (_head.getNext().getNext() == null|| _head== null)
  164. return null;
  165. Point Down = _head.getPoint();
  166. Point Up = this.highestVertex();
  167. Point Right = _head.getPoint();
  168. Point Left = _head.getPoint();
  169. PointNode t= _head.getNext();
  170. while (t.getNext() != null){
  171. if( t.getPoint().isUnder(Down))
  172. Down= t.getPoint();
  173. if( t.getPoint().isRight(Right))
  174. Right= t.getPoint();
  175. if( t.getPoint().isLeft(Left))
  176. Left= t.getPoint();
  177. t = t.getNext();
  178. }
  179. Polygon BoundingBox = new Polygon();
  180. BoundingBox.addVertex(new Point(Left.getX(),Down.getY()), 1);
  181. BoundingBox.addVertex(new Point(Right.getX(),Down.getY()), 2);
  182. BoundingBox.addVertex(new Point(Right.getX(),Up.getY()), 3);
  183. BoundingBox.addVertex(new Point(Left.getX(),Up.getY()),4);
  184. return BoundingBox;
  185. } // end of method getBoundingBox
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement