目录
采用面向对象的方法设计
题目
己知一个正三角形ABC,其内分点A1、 B1、C1,使得2AC1=C1B、2BA1=A1C、 2CB1=B1A,连接CC1、BB1,、AA1,三条直线分别相交于A2、B2、C2 (见图)。
要求采用面向对象的方法设计并编写程序验证:三角形A2,B2,C2,面积是三角形ABC面积的七分之一。
1.分析
要验证面积,需求得面积S-ABC和S-A2B2C2 (可用海伦公式求得).
要求得面积,需求得边长AB,AC,BC和A2B2,A2C2,B2C2 (由两点可确定长度).
要求得边长,需要先构造三角形ABC和A2B2C2 (由三个点确定).
要构造三角形,需求得端点A1,B1,C1,A2,B2,C2 .
端点A2,B2,C2是由直线AA1,BB1,CC1相交得到的,即要求得端点A2,B2,C2,需要求直线交点.
端点A1,B1,C1是直线BC,AC,AB的三等分点,即要求得端点A1,B1,C1,需要先假设端点A,B,C,由两点式可得到直线.
2.需要的类及主要功能的声明以及思路
2.1点类
2.1.1构造方法
声明
Point(double x, double y);
2.1.2两点求距离的方法
声明
double getDistance(Point p);
思路
假设有Pa,Pb两点,那么
2.1.3求三等分点的方法
声明
public Point getThreeEqualPoint(Point B);
2.2直线类 (两点式确定直线)
2.2.1构造方法
声明
public Straight(Point p1, Point p2);
2.2.2求两直线交点的方法
声明
public Point getIntersection(Straight s);
思路
2.3三角形类 (由三点确定)
2.3.1构造方法
声明
public Triangle(Point p1, Point p2, Point p3);
2.3.2 求面积
声明
public double getArea();
思路
海伦公式
3.代码
//点类
public class Point
{
protected double x;
protected double y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public Point() { }
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 double getDistance(Point p)
{
double s;
s=Math.sqrt(Math.pow(this.x-p.getX(), 2)
+Math.pow(this.y-p.getY(), 2));
return s;
}
//求三等分点(靠近A点)
public Point getThreeEqualPoint(Point B)
{
Point A=this;
double x,y;
if(A.getX()>B.getX())
x=A.getX()-(A.getX()-B.getX())/3;
else
x=A.getX()+(B.getX()-A.getX())/3;
if(A.getY()>B.getY())
y=A.getY()-(A.getY()-B.getY())/3;
else
y=A.getY()+(B.getY()-A.getY())/3;
return new Point(x,y);
}
//输出点
public String showPoint()
{ return "("+x+","+y+")"; }
}
//直线类
public class Straight
{
//两点式确定直线
protected Point p1;
protected Point p2;
public Straight() { }
public Straight(Point p1, Point p2)
{
this.p1 = p1;
this.p2 = p2;
}
public Point getP1() { return p1; }
public void setP1(Point p1) { this.p1 = p1; }
public Point getP2() { return p2; }
public void setP2(Point p2) { this.p2 = p2; }
//求两直线交点
public Point getIntersection(Straight s)
{
double x,y;double k1,b1,k2,b2;
k1=(p2.getY()-p1.getY())
/(p2.getX()-p1.getX());
b1=p1.getY()-p1.getX()
*(p2.getY()-p1.getY())
/(p2.getX()-p1.getX());
k2=(s.p2.getY()-s.p1.getY())
/(s.p2.getX()-s.p1.getX());
b2=s.p1.getY()-s.p1.getX()
*(s.p2.getY()-s.p1.getY())
/(s.p2.getX()-s.p1.getX());
x=(b2-b1)/(k1-k2);
y=k1*x+b1;
return new Point(x,y);
}
}
//三角形类
public class Triangle
{
protected Point p1;
protected Point p2;
protected Point p3;
public Triangle() { }
public Triangle(Point p1, Point p2, Point p3)
{
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public Point getP1() { return p1; }
public void setP1(Point p1) { this.p1 = p1; }
public Point getP2() { return p2; }
public void setP2(Point p2) { this.p2 = p2; }
public Point getP3() { return p3; }
public void setP3(Point p3) { this.p3 = p3; }
//求面积
public double getArea()
{
double p,s,a,b,c;
a=p1.getDistance(p2);
b=p1.getDistance(p3);
c=p3.getDistance(p2);
p=(a+b+c)/2;
s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
}
//测试类
public class Test
{
public static void main(String[]args)
{
Point pa,pb,pc,pa1,pb1,pc1,pa2,pb2,pc2;
//测试样例点A(0,Math.sqrt(100-25)),B(-5,0),C(5,0)
pa=new Point(0,Math.sqrt(100-25));
pb=new Point(-5,0);
pc=new Point(5,0);
//构建三角形ABC
Triangle abc=new Triangle(pa,pb,pc);
//求点A1,B1,C1
pa1=pb.getThreeEqualPoint(pc);
pb1=pc.getThreeEqualPoint(pa);
pc1=pa.getThreeEqualPoint(pb);
//求直线AA1,BB1,CC1
Straight s1=new Straight(pa,pa1);
Straight s2=new Straight(pb,pb1);
Straight s3=new Straight(pc,pc1);
//求点A2,B2,C2
pa2=s1.getIntersection(s2);
pb2=s1.getIntersection(s3);
pc2=s2.getIntersection(s3);
//构建三角形A2B2C2
Triangle a2b2c2=new Triangle(pa2,pb2,pc2);
//输出三角形信息
System.out.println("三角形ABC: 顶点:"
+abc.getP1().showPoint()+","
+abc.getP2().showPoint()+","
+abc.getP3().showPoint()
+" \n面积:"+abc.getArea());
System.out.println("三角形A2B2C2: 顶点:"
+a2b2c2.getP1().showPoint()+","
+a2b2c2.getP2().showPoint()+","
+a2b2c2.getP3().showPoint()
+" \n面积:"+a2b2c2.getArea());
System.out.println();
//验证结论
System.out.println(abc.getArea()+"/7="+(double)(abc.getArea()/7));
}
}