(几何:Triangle2D类)定义Triangle2D类和MyPoint2D类(自己定义:2维点),要求如下:
A、三个MyPoint2D类数据域成员:p1、p2、p3;这个三个数据域都带有get和set方法。
B、一个无参构造方法,创建三个坐标为(0,0)、(1,1)、(2,5)的点组成的三角形。
C、一个带有指定点的三角形的构造方法。
D、一个返回三角形面积的方法getArea();
E、一个返回三角形周长的方法getPerimeter();
F、给定点p如果在当前三角形内返回true的方法contains(MyPoint p);
G、给定三角形t如果在当前三角形内返回true的方法contains(Triangle2D t);
H、给定三角形t如果与当前三角形有重叠部分返回true的方法overlaps(Triangle2D t);
最后编写主类测试以上所有方法。
注意:该题需要大家先相关了解几何知识。相关向量知识可以参考:http://blog.sina.com.cn/s/blog_906a5acc0101dlwg.html
提示:可以使用Java提供的Line2D类或者自己定义线段类:Line,在该类中完成方法:1、判断当前线段与线段a相交的方法:intersectionLine(Line a);
2、判断点a在本线段上的方法:contains(MyPoint2D a);;3、完成向量叉积p2p1xq2p1=(p2.x-p1.x)*(q2.y-p1.y)-(q2.x-p1.x)*(p2.y-p1.y)的方法:Multiply(p2,q2,p1);
package assignment;
public class MyPoint2D {
private double x,y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public MyPoint2D(double x,double y){
this.x=x;
this.y=y;
}
public double distance(MyPoint2D p) {
return Math.sqrt(Math.pow(x-p.getX(), 2)+Math.pow(y-p.getY(), 2));
}
}
package assignment;
public class Triangle2D {
private MyPoint2D p1,p2,p3;
public MyPoint2D getP1() {
return p1;
}
public void setP1(MyPoint2D p1) {
this.p1 = p1;
}
public MyPoint2D getP2() {
return p2;
}
public void setP2(MyPoint2D p2) {
this.p2 = p2;
}
public MyPoint2D getP3() {
return p3;
}
public void setP3(MyPoint2D p3) {
this.p3 = p3;
}
public Triangle2D() { //无参构造方法
this(new MyPoint2D(0, 0),new MyPoint2D(1, 1),new MyPoint2D(2,5));
}
public Triangle2D(MyPoint2D p1,MyPoint2D p2,MyPoint2D p3) {//有参构造方法
this.p1=p1;
this.p2=p2;
this.p3=p3;
}
public double getArea() { //面积(海伦公式)
double d1=p1.distance(p2);
double d2=p2.distance(p3);
double d3=p2.distance(p3);
double p=(d1+d2+d3)/2;
return Math.sqrt(p*(p-d1)*(p-d2)*(p-d3));
}
public double getPerimeter() {//周长
return p1.distance(p2)+p2.distance(p3)+p3.distance(p1);
}
public boolean contains(MyPoint2D p) {//给定的点p在三角形中返回true
double areaS=this.getArea();//当点在三角形内时,大三角形的面积等于三个小三角形的面积
double pA=(p.distance(p1)+p.distance(p2)+p1.distance(p2))/2;
double pB=(p.distance(p1)+p.distance(p3)+p1.distance(p3))/2;
double pC=(p.distance(p3)+p.distance(p2)+p3.distance(p2))/2;
double areaA=Math.sqrt(pA*(pA-p.distance(p1))*(pA-p.distance(p2))*(pA-p1.distance(p2)));//海伦公式
double areaB=Math.sqrt(pB*(pA-p.distance(p1))*(pB-p.distance(p3))*(pB-p1.distance(p3)));
double areaC=Math.sqrt(pC*(pA-p.distance(p3))*(pC-p.distance(p2))*(pC-p3.distance(p2)));
if(areaS==areaA+areaB+areaC)
return true;
else
return false;
}
public boolean contains(Triangle2D t) {//给定的三角形t在三角形中返回true
if(contains(t.p1)&&contains(t.p2)&&contains(t.p3))
return true;
else
return false;
}
}
package assignment;
public class testTriangle2D {
public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle2D triangle1=new Triangle2D();
System.out.println(triangle1.getArea());
System.out.println(triangle1.getPerimeter());
System.out.println(triangle1.contains(new MyPoint2D(1, 1)));
System.out.println(triangle1.contains(new Triangle2D(new MyPoint2D(0, 0), new MyPoint2D(1,1), new MyPoint2D(2, 2))));
}
}