Guest User

Untitled

a guest
May 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public class Triangle extends Polygon{
  2. //constructor
  3. /**
  4. * constructor #1
  5. * @param p1
  6. * @param p2
  7. * @param p3
  8. * set a new array with the vertex
  9. */
  10. public Triangle(Point p1,Point p2,Point p3){
  11. super(new Point[] {p1,p2,p3});
  12. }
  13. /**
  14. * constructor #2
  15. * @param t
  16. * copy the points(deep copy)
  17. */
  18. public Triangle(Triangle t){
  19. super(t.getPoints());
  20. }
  21. //behavior
  22. /**
  23. *
  24. * @return the first point
  25. */
  26. public Point getP1(){
  27. return getPoints()[0];
  28. }
  29. /**
  30. *
  31. * @return the second point
  32. */
  33. public Point getP2(){
  34. return getPoints()[1];
  35. }
  36. /**
  37. *
  38. * @return the third point
  39. */
  40. public Point getP3(){
  41. return getPoints()[2];
  42. }
  43. /**
  44. * @param o
  45. * return true if two Triangles are equal(logic)
  46. */
  47. public boolean equals(Object o){
  48. if(o instanceof Triangle){
  49. Triangle otherO=(Triangle)o;
  50. boolean check=false;
  51. for(int i=0;i<3;i++){
  52. for(int j=0;j<3 && !check;j++){
  53. if(getPoints()[i].equals(otherO.getPoints()[j])){
  54. check=true;
  55. }
  56. }
  57. if(!check) return false;
  58. }
  59. return true;
  60. }
  61. else
  62. return false;
  63. }
  64. public double getArea(){
  65. double D=getPerimeter()/2;
  66. return Math.sqrt(D*(D-getSides()[0]))*(D-getSides()[1])*(D-getSides()[2]);
  67. }
  68. public boolean contains(Point p){
  69. Triangle t1=new Triangle(p,getPoints()[0],getPoints()[1]);
  70. Triangle t2=new Triangle(p,getPoints()[0],getPoints()[2]);
  71. Triangle t3=new Triangle(p,getPoints()[1],getPoints()[2]);
  72. double newArea=t1.getArea()+t2.getArea()+t3.getArea();
  73. return(getArea()-newArea<=Math.abs(0.001));
  74. }
  75.  
  76.  
  77. }
Add Comment
Please, Sign In to add comment