Advertisement
Guest User

Untitled

a guest
Mar 1st, 2010
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package ATC;
  7.  
  8. /**
  9. *
  10. * @author zootreeves
  11. */
  12.  
  13. public class Triangle {
  14. // state data for a triangle with vertices a, b, c
  15. private Coordinate a, b, c;
  16.  
  17. // constructs a triangle with the given vertices
  18. public Triangle(Coordinate newA, Coordinate newB, Coordinate newC) {
  19. a = newA;
  20. b = newB;
  21. c = newC;
  22. }
  23.  
  24. // access each vertex
  25. public Coordinate getVertexA() {
  26. return a;
  27. }
  28. public Coordinate getVertexB() {
  29. return b;
  30. }
  31. public Coordinate getVertexC() {
  32. return c;
  33. }
  34.  
  35. // calculate the length of the side opposite vertex a
  36. public double getSideA() {
  37. // get the distance between vertices b and c
  38. double length = b.distance(c);
  39. return length;
  40. }
  41.  
  42. // calculate the length of the side opposite vertex b
  43. public double getSideB() {
  44. // get the distance between vertices a and c
  45. double length = a.distance(c);
  46. return length;
  47. }
  48.  
  49. // calculate the length of the side opposite vertex c
  50. public double getSideC() {
  51. // get the distance between vertices a and b
  52. double length = a.distance(b);
  53. return length;
  54. }
  55.  
  56. // calculate the angle at vertex a
  57. public double getAngleA() {
  58. // get the length of each side
  59. double as = getSideA(), bs = getSideB(), cs = getSideC();
  60. // apply the law of cosines
  61. double angle = Math.acos((bs*bs + cs*cs - as*as) / (2*bs*cs));
  62. return angle;
  63. }
  64.  
  65. // calculate the angle at vertex b
  66. public double getAngleB() {
  67. // get the length of each side
  68. double as = getSideA(), bs = getSideB(), cs = getSideC();
  69. // apply the law of cosines
  70. double angle = Math.acos((as*as + cs*cs - bs*bs) / (2*as*cs));
  71. return angle;
  72. }
  73.  
  74. // calculate the angle at vertex c
  75. public double getAngleC() {
  76. // get the length of each side
  77. double as = getSideA(), bs = getSideB(), cs = getSideC();
  78. // apply the law of cosines
  79. double angle = Math.acos((as*as + bs*bs - cs*cs) / (2*as*bs));
  80. return angle;
  81. }
  82.  
  83. // check whether no side is longer than the other two sides put together
  84. public boolean isValid()
  85. {
  86. // get the length of each side
  87. double as = getSideA(), bs = getSideB(), cs = getSideC();
  88. if (as > (bs + cs))
  89. return false;
  90. if (bs > (as + cs))
  91. return false;
  92. if (cs > (as + bs))
  93. return false;
  94. return true;
  95. }
  96.  
  97. // calculate the sum of the lengths of the sides
  98. public double getPerimeter() {
  99. // get the length of each side
  100. double as = getSideA(), bs = getSideB(), cs = getSideC();
  101. return (as + bs + cs);
  102. }
  103.  
  104. // calculate the signed area
  105. double getSignedArea() {
  106. double signedArea = 0.5 * (a.getX() * (b.getY() - c.getY()) +
  107. b.getX() * (c.getY() - a.getY()) +
  108. c.getX() * (a.getY() - b.getY()));
  109. return signedArea;
  110. }
  111.  
  112. // calculate the absolute area
  113. public double getArea() {
  114. return Math.abs(getSignedArea());
  115. }
  116.  
  117. // determine orientation based on the signed area
  118. public int getOrientation() {
  119. double signedArea = getSignedArea();
  120. if (signedArea > 0.0)
  121. return 1;
  122. if (signedArea < 0.0)
  123. return -1;
  124. return 0;
  125. }
  126.  
  127. // pretty-print the coordinates inside square brackets
  128. public String toString() {
  129. return ("["+a+",\n "+b+",\n "+c+"]");
  130. }
  131.  
  132. // do the coordinates of the vertices a, b, c match up in order?
  133. // note that we are not checking all 6 orderings of a, b, c
  134. public boolean equals(Object o)
  135. {
  136. Triangle triangle = (Triangle) o;
  137. if (triangle.getVertexA() != a)
  138. return false;
  139. if (triangle.getVertexB() != b)
  140. return false;
  141. if (triangle.getVertexC() != c)
  142. return false;
  143. return true;
  144. }
  145.  
  146. // check whether a given point falls inside the triangle
  147. public boolean contains(Coordinate p)
  148. {
  149. int orientation = (new Triangle(b, c, p)).getOrientation();
  150. if ((new Triangle(a, b, p)).getOrientation() != orientation)
  151. return false;
  152. if (orientation != (new Triangle(b, c, p)).getOrientation())
  153. return false;
  154. return true;
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement