Advertisement
gluten_free_feeling

Untitled

Feb 24th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1.  
  2. import java.awt.geom.*;
  3. import java.util.ArrayList;
  4. import gpdraw.*;
  5. public class IrregularPolygonAZ
  6. {
  7. private ArrayList <Point2D.Double> myPolygon;
  8. private DrawingTool myPencil;
  9. private SketchPad myPaper;
  10. /**
  11. * Constructor for objects of class IrregularPolygonAZ
  12. */
  13. public IrregularPolygonAZ()
  14. {
  15. myPolygon = new ArrayList <Point2D.Double> ();
  16. myPaper = new SketchPad(500, 500);
  17. myPencil = new DrawingTool(myPaper);
  18. }
  19.  
  20. public void add(Point2D.Double aPoint)
  21. {
  22. myPolygon.add(aPoint);
  23. }
  24.  
  25. public void draw()
  26. {
  27. myPencil.up();
  28. myPencil.move(myPolygon.get(0).x, myPolygon.get(0).y );
  29. myPencil.down();
  30. for(int x = 1; x < myPolygon.size(); x++)
  31. {
  32. myPencil.move(myPolygon.get(x).x, myPolygon.get(x).y);
  33. }
  34. myPencil.move(myPolygon.get(0).x, myPolygon.get(0).y);
  35. }
  36.  
  37. public double perimeter()
  38. {
  39. double total = 0;
  40. int cnt;
  41. for(cnt = 0; cnt < myPolygon.size() - 1; cnt++)
  42. {
  43. total += Math.sqrt( Math.pow((myPolygon.get(cnt + 1).x - myPolygon.get(cnt).x), 2) + Math.pow((myPolygon.get(cnt + 1).y - myPolygon.get(cnt).y), 2));
  44. }
  45. total += Math.sqrt( Math.pow((myPolygon.get(0).x - myPolygon.get(cnt).x), 2) + Math.pow((myPolygon.get(0).y - myPolygon.get(cnt).y), 2));
  46. return total;
  47. }
  48.  
  49. public double area()
  50. {
  51. double total = 0;
  52. for(int ct = 0; ct < myPolygon.size() - 1; ct++)
  53. {
  54. total += ( (myPolygon.get(ct).x * myPolygon.get(ct+1).y) - (myPolygon.get(ct).y * myPolygon.get(ct+1).x) );
  55. }
  56. total = total / 2.0;
  57. return total;
  58. }
  59.  
  60. public double distance(int c, int v)
  61. {
  62. double value = 0.0;
  63. value = Math.sqrt( Math.pow((myPolygon.get(v).x - myPolygon.get(c).x), 2) + Math.pow((myPolygon.get(v).y - myPolygon.get(c).y), 2));
  64. return value;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement