Please help me! public class Triangle2D { MyPoint p1; MyPoint p2; MyPoint p3; pu
ID: 3753215 • Letter: P
Question
Please help me!
public class Triangle2D {
MyPoint p1;
MyPoint p2;
MyPoint p3;
public MyPoint getP1();
{
return p1;
}
public void setP1(MyPoint p1)
{
this.p1 = p1;
}
public MyPoint getP2();
{
return p2;
}
public void setP2(MyPoint p1)
{
this.p2 = p2;
}
public MyPoint getP3();
{
return p3;
}
public void setP3(MyPoint p3)
{
this.p3 = p3;
}
Triangle2D()
{
p1 = new MyPoint(0.0);
p2 = new MyPoint(1.1);
p3 = new MyPoint(2.5);
}
Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public double getArea(){
double side1 = Math.sqrt(Math.pow(p2.getX()
-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));
double side2 = Math.sqrt(Math.pow(p3.getX()
-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));
double side3 = Math.sqrt(Math.pow(p1.getX()
-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));
double s =(side1 + side2 + side3)/2;
double area = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));
return area;
}
public double getperimeter(){
double side1 = Math.sqrt(Math.pow(p2.getX()
-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));
double side2 = Math.sqrt(Math.pow(p3.getX()
-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));
double side3 = Math.sqrt(Math.pow(p1.getX()
-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));
double perimeter = side1 + side2 + side3;
return perimeter;
}
public boolean contains(MyPoint p){
double[][]points = new double[4][2];
points[0][0]=this.p1.getX();
points[0][1]=this.p1.getY();
points[1][0]=this.p2.getX();
points[1][1]=this.p1.getY();
points[2][0]=this.p2.getX();
points[2][1]=this.p1.getY();
points[3][0]= p.getX();
points[3][1]= p.getX();
if (getIntersectingPoint(points)! = null)
{
return true;
}
return false;
}
public boolean contains (Triangle2D t){
if((this.getArea()>t.getArea()))
{
return true;
}
else
{
return false;
}
}
public boolean overlaps (Triangle2D t){
double[][]points = new double[4][2];
points[0][0]=this.p1.getX();
points[0][1]=this.p1.getY();
points[1][0]=this.p2.getX();
points[1][1]=this.p1.getY();
points[2][0]=this.p2.getX();
points[2][1]=this.p1.getY();
points[3][0]= t.p1.getX();
points[3][1]= t.p1.getY();
int count = 0;
if(getIntersectingPoint(points)!=null){
count ++;
}
points[3][0] = t.p2.getX();
points[3][1] = t.p2.getY();
if(getIntersectingPoint(points)!=null)
{
count ++;
}
points[3][0] = t.p3.getX();
points[3][1] = t.p3.getY();
if(getIntersectingPoint(points)!=null)
{
count++;
}
if(count>1)
return true;
return false;
}
public double[] getIntersectingPoint(double[][] points){
double y12 = points[0][1] - points[1][1];
double x12 = -(points[0][0] - points[1][0]);
double y34 = points[2][1]- points[3][1];
double x34 = -(points[2][0] - points[3][0]);
double b0 = (points[0][1] - points[1][1])*points[0][0] - (points[0][0] -
points[1][0]) * points[0][1];
double b1 = (points[2][1] - points[3][1]) * points[2][0] - (points[2][0]
- points[3][0]) * points[2][1];
double denom = y12 * x34 - x12 *y34;
if(denom == 0)
{
return null;
}
double[] intersectingPoint = new double[2];
intersectingPoint[0] = (b0 * x34 - x12 * b1)/denom;
intersectingPoint[1] = (y12 * b1 - b0 * y34)/denom;
return intersectingPoint;
}
Explanation / Answer
package cheggSolution1;
public class Triangle2D {
MyPoint p1;
MyPoint p2;
MyPoint p3;
public MyPoint getP1()
{
return p1;
}
public void setP1(MyPoint p1)
{
this.p1 = p1;
}
public MyPoint getP2()
{
return p2;
}
public void setP2(MyPoint p1)
{
this.p2 = p2;
}
public MyPoint getP3()
{
return p3;
}
public void setP3(MyPoint p3)
{
this.p3 = p3;
}
Triangle2D()
{
p1 = new MyPoint(0,0);
p2 = new MyPoint(1,1);
p3 = new MyPoint(2,5);
}
Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public double getArea(){
double side1 = Math.sqrt(Math.pow(p2.getX()
-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));
double side2 = Math.sqrt(Math.pow(p3.getX()
-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));
double side3 = Math.sqrt(Math.pow(p1.getX()
-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));
double s =(side1 + side2 + side3)/2;
double area = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));
return area;
}
public double getArea(MyPoint p1, MyPoint p2, MyPoint p3){
double side1 = Math.sqrt(Math.pow(p2.getX()
-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));
double side2 = Math.sqrt(Math.pow(p3.getX()
-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));
double side3 = Math.sqrt(Math.pow(p1.getX()
-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));
double s =(side1 + side2 + side3)/2.0;
double area = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));
return area;
}
public double getperimeter(){
double side1 = Math.sqrt(Math.pow(p2.getX()
-p1.getX(),2)+Math.pow(p2.getY()-p1.getY(),2));
double side2 = Math.sqrt(Math.pow(p3.getX()
-p2.getX(),2)+Math.pow(p3.getY()-p2.getY(),2));
double side3 = Math.sqrt(Math.pow(p1.getX()
-p3.getX(),2)+Math.pow(p1.getY()-p3.getY(),2));
double perimeter = side1 + side2 + side3;
return perimeter;
}
public boolean contains(MyPoint p){
// if point p is inside traingle ABC then
// area of pab+pbc+pca should equal to area of abc
// System.out.println(getArea(p, p1, p2)+getArea(p, p2, p3)+getArea(p, p3, p1)+" "+getArea(p1, p2, p3));
if((int)(getArea(p, p1, p2)+getArea(p, p2, p3)+getArea(p, p3, p1))==(int)getArea(p1, p2, p3))
return true;
return false;
}
public boolean contains (Triangle2D t){
// if all points are inside then triangle is inside
if(contains(t.getP1()) && contains(t.getP2()) && contains(t.getP3()))
return true;
return false;
}
public boolean overlaps (Triangle2D t){
// if at least one point is inside triangle overlaps
int count=0;
if(contains(t.getP1()))
count++;
if(contains(t.getP2()))
count++;
if(contains(t.getP3()))
count++;
if(count>1 && count<3)
return true;
return false;
}
public double[] getIntersectingPoint(double[][] points){
double y12 = points[0][1] - points[1][1];
double x12 = -(points[0][0] - points[1][0]);
double y34 = points[2][1]- points[3][1];
double x34 = -(points[2][0] - points[3][0]);
double b0 = (points[0][1] - points[1][1])*points[0][0] - (points[0][0] -
points[1][0]) * points[0][1];
double b1 = (points[2][1] - points[3][1]) * points[2][0] - (points[2][0]
- points[3][0]) * points[2][1];
double denom = y12 * x34 - x12 *y34;
if(denom == 0)
{
return null;
}
double[] intersectingPoint = new double[2];
intersectingPoint[0] = (b0 * x34 - x12 * b1)/denom;
intersectingPoint[1] = (y12 * b1 - b0 * y34)/denom;
return intersectingPoint;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.