Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Triangle extends Polygon{
- //constructor
- /**
- * constructor #1
- * @param p1
- * @param p2
- * @param p3
- * set a new array with the vertex
- */
- public Triangle(Point p1,Point p2,Point p3){
- super(new Point[] {p1,p2,p3});
- }
- /**
- * constructor #2
- * @param t
- * copy the points(deep copy)
- */
- public Triangle(Triangle t){
- super(t.getPoints());
- }
- //behavior
- /**
- *
- * @return the first point
- */
- public Point getP1(){
- return getPoints()[0];
- }
- /**
- *
- * @return the second point
- */
- public Point getP2(){
- return getPoints()[1];
- }
- /**
- *
- * @return the third point
- */
- public Point getP3(){
- return getPoints()[2];
- }
- /**
- * @param o
- * return true if two Triangles are equal(logic)
- */
- public boolean equals(Object o){
- if(o instanceof Triangle){
- Triangle otherO=(Triangle)o;
- boolean check=false;
- for(int i=0;i<3;i++){
- for(int j=0;j<3 && !check;j++){
- if(getPoints()[i].equals(otherO.getPoints()[j])){
- check=true;
- }
- }
- if(!check) return false;
- }
- return true;
- }
- else
- return false;
- }
- public double getArea(){
- double D=getPerimeter()/2;
- return Math.sqrt(D*(D-getSides()[0]))*(D-getSides()[1])*(D-getSides()[2]);
- }
- public boolean contains(Point p){
- Triangle t1=new Triangle(p,getPoints()[0],getPoints()[1]);
- Triangle t2=new Triangle(p,getPoints()[0],getPoints()[2]);
- Triangle t3=new Triangle(p,getPoints()[1],getPoints()[2]);
- double newArea=t1.getArea()+t2.getArea()+t3.getArea();
- return(getArea()-newArea<=Math.abs(0.001));
- }
- }
Add Comment
Please, Sign In to add comment