Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.90 KB | None | 0 0
  1.  
  2. /**
  3. * Write a description of class Polygon here.
  4. *
  5. * @author (Liron Mor-Yosef)
  6. * @version (22/06/17)
  7. */
  8. public class Polygon
  9. {
  10. private PointNode _head;
  11.  
  12. /**
  13. * 1st constructor empty constructor
  14. */
  15. public Polygon(){
  16. _head = null;
  17. }
  18.  
  19. public boolean addVertex(Point p, int pos){
  20. if (pos<1)
  21. return false;
  22.  
  23. if(_head == null)
  24. {
  25. if (pos == 1){
  26. _head=new PointNode(p);
  27. return true;
  28. }
  29. return false;
  30. }
  31. else{
  32. if (pos == 1){
  33. PointNode temp = _head;
  34. _head = new PointNode(p);
  35. _head.setNext(temp);
  36. return true;
  37. }
  38.  
  39. PointNode curr = _head;
  40. int count = 1;
  41. while ( curr.getNext() != null && count+1 < pos) {
  42. curr = curr.getNext();
  43. count++;
  44. }
  45. if(count+1 <pos)
  46. return false;
  47. else{
  48. // בודק אם הקודקוד שהתקבל צריך להתווסף לסוף הרשימה
  49. if ( curr.getNext() == null ){
  50. PointNode temp = new PointNode(p);
  51. curr.setNext(temp);
  52. }
  53. // בודק אם הקודקוד שהתקבל צריך להתווסף לאמצע הרשימה
  54. else{
  55. curr = curr.getNext();
  56. PointNode temp = new PointNode(curr.getPoint(),curr.getNext()); //הקודקוד הנוכחי נשמר במצביע זמני
  57. curr.setPoint(p); // הקודקוד שאני מוסיף שמתי במצביע הנוכחי
  58. curr.setNext(temp);// הקודקוד החדש יצביע על הקודקוד הישן שזז קדימה
  59. }
  60. return true;
  61.  
  62. }
  63. }
  64. }
  65.  
  66. public Point highestVertex(){
  67. // if the list empty
  68. if ( _head == null)
  69. return null;
  70.  
  71. // default value
  72. PointNode curr = _head;
  73. PointNode highestPoint = curr;
  74.  
  75. while (curr.getNext() != null){
  76. curr = curr.getNext();
  77. if (curr.getPoint().isAbove( highestPoint.getPoint())){
  78. highestPoint.setPoint(curr.getPoint());
  79. }
  80. }
  81. return new Point (highestPoint.getPoint());
  82.  
  83. }
  84.  
  85. public String toString(){
  86. PointNode temp = _head;
  87. int count= 0;
  88. if ( _head == null)
  89. return "The polygon has 0 vertices.";
  90.  
  91. String str2= "(" ;
  92. while (temp != null){
  93. str2 += "" + temp.getPoint();
  94. if (temp.getNext()!= null)
  95. str2 += ",";
  96. temp = temp.getNext();
  97. count++;
  98. }
  99. str2 = "The polygon has " + count + " vertices:" + "\n" +str2 + ")" ;
  100. return str2;
  101.  
  102. }
  103.  
  104. public double calcPerimeter(){
  105. double perimeter = 0;
  106. int count = length();
  107. // variables
  108. PointNode curr = _head;
  109.  
  110. Point firstPoint = _head.getPoint();
  111.  
  112. while (curr != null) {
  113. curr = curr.getNext();
  114. count++;
  115. }
  116. // If there is no line between two points there is no perimeter
  117. if (curr == null || count == 1)
  118. return perimeter;
  119.  
  120. // The perimeter is only the distance between two points
  121. if (count == 2 ){
  122. perimeter = curr.getPoint().distance(curr.getNext().getPoint());
  123. return perimeter; }
  124.  
  125. //Summation the polygon lengths to the perimeter calculation
  126. if(count >= 3)
  127. while (curr != null){
  128. perimeter += curr.getPoint().distance(curr.getNext().getPoint());
  129. curr = curr.getNext();
  130. }
  131.  
  132. perimeter += curr.getPoint().distance(firstPoint);
  133.  
  134. return perimeter;
  135. }
  136. public double calcArea(){
  137. if (_head == null || _head.getNext()==null || _head.getNext().getNext() == null )
  138. return 0;
  139. PointNode p1 = _head;
  140. PointNode p2 = p1.getNext();
  141. PointNode p3 = p1.getNext().getNext();
  142. double area = heron(p1.getPoint(),p2.getPoint(),p3.getPoint());
  143. while( p3.getNext()!=null){
  144. p2 = p3;
  145. p3 = p3.getNext();
  146. area += heron(p1.getPoint(),p2.getPoint(),p3.getPoint());
  147. }
  148. return area;
  149. }
  150.  
  151.  
  152.  
  153.  
  154. /*
  155. public double calcArea(){
  156. PointNode curr = _head.getNext();
  157. double area = 0;
  158. int count = length();
  159.  
  160. //If no triangle is formed there is no area
  161. if (count < 3 )
  162. return 0;
  163.  
  164. /* Calculating the area of the triangles representing the polygon
  165. using the Heron formula for calculating the polygon area
  166. while (curr.getNext() != null ){
  167. area = area + heron( _head.getPoint(),curr.getPoint(),curr.getNext().getPoint());
  168. curr = curr.getNext();
  169. }
  170.  
  171. return area;
  172. }
  173. */
  174.  
  175. public boolean isBigger(Polygon other) {
  176. if ( this.calcArea() > other.calcArea() )
  177. return true;
  178. return false;
  179.  
  180. }
  181.  
  182. public int findVertex(Point p){
  183. // if the list empty
  184. if ( _head == null)
  185. return -1;
  186.  
  187. PointNode temp = _head;
  188. int count = 1;
  189. //Search for the received point if it is the same as one of the array points
  190. while ( temp.getNext() != null ){
  191.  
  192. if (p.equals(temp.getPoint()))
  193. return count;
  194. count++;
  195. temp = temp.getNext();
  196.  
  197. }
  198.  
  199. // If the point is not found...
  200. return -1;
  201. }
  202.  
  203. public Point getNextVertex(Point p ){
  204. PointNode firstPoint = _head;
  205. PointNode temp = _head;
  206. // if the list empty
  207. if ( _head == null)
  208. return null;
  209.  
  210. // The point is the only point in the array
  211. if ( length() == 1)
  212. return new Point (p);
  213.  
  214. // If the point is the last member of the array we will return the first point
  215. if ( length() > 0 && temp.getNext().getPoint() == null && p.equals(temp.getPoint()))
  216. return new Point(firstPoint.getPoint());
  217.  
  218. // Return the point after the point which recieved in the array
  219. while(temp != null ){
  220. if (p.equals(temp.getPoint()))
  221. return new Point( temp.getNext().getPoint());
  222. temp = temp.getNext();
  223. }
  224. //If the point is not a vertex in the polygon
  225. return null;
  226. }
  227.  
  228. // צריך לממש
  229. /* public Polygon getBoundingBox(){
  230. int count = length();
  231. PointNode curr = _head;
  232. if (count < 3)
  233. return null;
  234.  
  235. // Declaration & Initialization Default value
  236. Point leftVert = curr.getPoint();
  237. Point rightVert = curr.getPoint();
  238. Point lowVert = curr.getPoint();
  239. double highVert = curr.getPoint().getY();
  240.  
  241. // Finding the extreme points of the polygon
  242. while (curr != null){
  243.  
  244. //Finding the leftmost point
  245. if (leftVert.isRight(curr.getPoint()))
  246. leftVert = curr.getPoint();
  247.  
  248. //Finding the rightmost point
  249. if (rightVert.isLeft(curr.getPoint()))
  250. rightVert = curr.getPoint();
  251.  
  252. //Finding the lowest point
  253. if (lowVert.isAbove(curr.getPoint()))
  254. lowVert = curr.getPoint();
  255.  
  256. // Finding the highest point
  257. highVert = highestVertex().getY();
  258. curr = curr.getNext();
  259. }
  260.  
  261. // Create Bounding Box
  262. Polygon Box = new Polygon();
  263.  
  264. Box.addVertex( leftVert)); // The lowest leftmost point
  265. Box.addVertex( rightVert); // The lowest rightmost point
  266. Box.addVertex( rightVert.getX(),highVert); // The highest rightmost point
  267. Box.addVertex( leftVert.getX(),highVert); // The highest leftmost point
  268.  
  269. return Box;
  270.  
  271. }*/
  272.  
  273. // private method
  274. private int length (){
  275. PointNode temp = _head;
  276. int count = 0;
  277. // if(_head == null)
  278. // return count;
  279.  
  280. while (temp != null ){
  281. count++;
  282. temp = temp.getNext();
  283. }
  284. return count;
  285. }
  286.  
  287. // Private method
  288. // A method that calculates the area of a triangle by applying the Heron formula.
  289. private double heron( Point p1, Point p2, Point p3){
  290.  
  291. //Calculation of sides lengths of the triangle
  292. double a = p1.distance(p2); // p1 = _vertices[0]
  293. double b = p2.distance(p3); // p2 = _vertices[i]
  294. double c = p3.distance(p1); // p3 = _vertices[i+1]
  295.  
  296. // Using Heron formula
  297. double s = (a+b+c) / 2;
  298. double heronCal;
  299. heronCal = Math.sqrt( s * (s-a)*(s-b)*(s-c));
  300.  
  301. return heronCal;
  302. }
  303.  
  304. /*
  305. // Private method
  306. // A method that calculates the area of a triangle by applying the Heron formula.
  307. private double heron( PointNode p1, PointNode p2, PointNode p3){
  308.  
  309. //Calculation of sides lengths of the triangle
  310. double a = getLength(p1,p2); // p1 = _vertices[0]
  311. double b = getLength(p2,p3); // p2 = _vertices[i]
  312. double c = getLength(p1,p3); // p3 = _vertices[i+1]
  313.  
  314. // Using Heron formula
  315. double s = (a+b+c) / 2;
  316. double heronCal = Math.sqrt( s * (s-a)*(s-b)*(s-c));
  317.  
  318. return heronCal;
  319. }
  320. /*
  321. private double getLength(PointNode a, PointNode b ){
  322. double deltaX = a.getPoint().getX()- b.getPoint().getX();
  323. double deltaY = a.getPoint().getY()- b.getPoint().getY();
  324. return Math.sqrt(Math.pow(deltaX,2) +Math.pow(deltaY,2));
  325.  
  326. }*/
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement