大皮

去见、去学、去写。

南昌航空大学 软件学院 pta Java 第五次作业 蔡珂 opp

opp系列最难的一次作业来了
国庆玩high了,导致最后几天突击也只写了一题
甚至没写完QAQ

drawing

题目列表

7-1 点线形系列5-凸五边形的计算-1

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。

以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后

代码如下



import java.util.Scanner; // 需要导入 util 包
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //  String ch = input.nextLine();
        PointTaking a = new PointTaking();
        a.ch = input.nextLine();//读入

        double ans =a.Solve();
        if (ans == -1)
            System.out.printf("Wrong Format\n");
        else if (ans == -2)
            System.out.printf("wrong number of points\n");
        else if (ans == -3)
            System.out.printf("data error\n");
        else if (ans==-4)
            System.out.printf("not a pentagon\n");
        else if (ans==-5)
            System.out.printf("not a polygon");
        else if (ans == 1)
            System.out.printf("true\n");
        else if (ans == 2)
            System.out.printf("false\n");
        else  if(ans==4)
            System.out.printf("points coincide\n");

    }
}

 class Line {
    Point a ,b;
    public Line(){

    }
    public Line(Point point1,Point point2){
        a = new Point(point1);
        b = new Point(point2);
    }
    public Line(Line line){
        a = new Point();
        b = new Point();
        a.x = line.a.x;a.y = line.a.y;
        b.x=line.b.x;b.y = line.b.y;
    }
    boolean judgeIntersection(Line line2){
        if(slope()==line2.slope())
            return false;
        Point point = intersection(line2);
        //System.out.printf("\n\n"+point.x+" "+point.y+"\n\n");
        if(dotLineJudgment(point)&&line2.dotLineJudgment(point))
            return true;
        return false;
    }
    double slope(){//斜率
        if(this.a.x==this.b.x)
            return 9521462;

        //System.out.printf("\n1511565sada\n");
        return (this.a.y-this.b.y)/(this.a.x-this.b.x);
    }
    double intercept() {//截距
        if(slope()==9521462)
            return this.a.x;
        if(slope()==0)
            return 0;
        return a.x-(this.slope())*a.y;
    }
    double pointVerticalDistanc(Point c){//点到线距离
        if(this.a.x==this.b.x)
            return Math.abs(c.x-this.a.x);
        return (Math.abs((this.a.y-this.b.y)*c.x+(this.b.x-this.a.x)*c.y+this.a.x*this.b.y-this.a.y*this.b.x)/
                (Math.sqrt((this.a.y-this.b.y)*(this.a.y-this.b.y)+(this.a.x-this.b.x)*(this.a.x-this.b.x))));
    }
    boolean sameIntersection(Line line1,Line line2){//判断相交点是否相同
        Point point1=this.intersection(line1),point2=this.intersection(line2);
        return point2.x == point1.x && point1.y == point2.y&&this.dotLineJudgment(point1)&&dotLineJudgment(point2)&&
                line1.dotLineJudgment(point1)&&line2.dotLineJudgment(point2);
    }
    double dis(){
        return a.DistanceCalculation(b);
    }//线长

    boolean judgeParallelism(Line line) {
        return  Math.abs((line.a.x-line.b.x )*(a.y-b.y)- (a.x-b.x )*(line.a.y-line.b.y))<0.05;
    }
    Point intersection(Line line){//求相交点
        Point point1 = new Point();
        point1.x=((line.a.x-line.b.x)*(this.a.y*this.b.x-this.a.x*this.b.y)-(this.a.x-this.b.x)*(line.a.y*line.b.x-line.a.x*line.b.y))/
                ((this.b.y-this.a.y)*(line.b.x-line.a.x)-(this.b.x-this.a.x)*(line.b.y-line.a.y));
        point1.y=((line.a.y-line.b.y)*(this.a.y*this.b.x-this.a.x*this.b.y)-(this.a.y-this.b.y)*(line.a.y*line.b.x-line.a.x*line.b.y))/
                ((this.b.y-this.a.y)*(line.b.x-line.a.x)-(this.b.x-this.a.x)*(line.b.y-line.a.y));
        return point1;
    }

    boolean dotLineJudgment(Point point){//判断点是否在直线上
        Line line2 = new Line(a,point);
        if(a.x==b.x)
            return  this.judgeParallelism(line2) &&Math.max(a.y,b.y)>=point.y&&Math.min(a.y,b.y)<=point.y;
        else {
            return this.judgeParallelism(line2) &&Math.max(a.x,b.x)>=point.x&&Math.min(a.x,b.x)<=point.x;
        }

    }
    boolean vertical(Line line){
        return (a.x - b.x) * (line.a.x - line.b.x) + (a.y - b.y) * (line.a.y - line.b.y) == 0;
    }
    boolean overlap(Line line){
        if(a.x==b.x)
            return (line.slope()==slope()&&line.intercept()==intercept())&&((line.a.y<=Math.max(a.y,b.y)&&line.a.y>=Math.min(a.y,b.y))||
                    (line.b.y<=Math.max(a.y,b.y)&&line.b.y>=Math.min(a.y,b.y)));
        return (line.slope()==slope()&&line.intercept()==intercept())&&((line.a.x<=Math.max(a.x,b.x)&&line.a.x>=Math.min(a.x,b.x))||
                (line.b.x<=Math.max(a.x,b.x)&&line.b.x>=Math.min(a.x,b.x)));

    }
}


class Pentagon {
    Line[] line = new Line [6];
    public Pentagon(){}

    public Pentagon(Point point1,Point point2,Point point3,Point point4,Point point5) {
        for(int i=1;i<=5;i++){
            line[i] = new Line();
            line[i].a = new Point();
            line[i].b = new Point();
        }
        line[1].a.x = point1.x;line[1].a.y = point1.y;
        line[1].b.x = point2.x;line[1].b.y = point2.y;

        line[2].a.x = point2.x;line[2].a.y = point2.y;
        line[2].b.x = point3.x;line[2].b.y = point3.y;

        line[3].a.x = point3.x;line[3].a.y = point3.y;
        line[3].b.x = point4.x;line[3].b.y = point4.y;

        line[4].a.x = point4.x;line[4].a.y = point4.y;
        line[4].b.x = point5.x;line[4].b.y = point5.y;

        line[5].a.x = point5.x;line[5].a.y = point5.y;
        line[5].b.x = point1.x;line[5].b.y = point1.y;
    }

    boolean legal(){
        for(int i=1;i<=5;i++) {
         //   System.out.printf("%f %f\n",line[i].a.x,line[i].a.y);
            if ((line[i].a.x-line[i].b.x )*(line[i%5+1].a.y-line[i%5+1].b.y)== (line[i%5+1].a.x-line[i%5+1].b.x )*(line[i].a.y-line[i].b.y))
                return false;
        }
        for(int i=1;i<=5;i++){
            for (int j=1;j<=5;j++){

                if(j!=(i%5+1)&&j!=(i+3)%5+1&&i!=j) {
            //        System.out.printf("%f %f %f %f\n",line[i].a.x,line[i].a.y,line[j].a.x,line[j].a.y);
                    if (line[i].judgeIntersection(line[j]))
                        return false;
                }
            }
        }
        return true;
    }
    double area(){
        Triangle []tri=new Triangle[5];
        Line[] lines= new Line[4];
        lines[1] = new Line(line[1].a,line[2].b);
        lines[2] = new Line(line[1].a,line[3].b);
        tri[1]= new Triangle(line[1],line[2],lines[1]);
        tri[2]= new Triangle(lines[1],line[3],lines[2]);
        tri[3]= new Triangle(lines[2],line[4],line[5]);
        return tri[1].area()+tri[2].area()+tri[3].area();
    }
    boolean bump(){//判断凹凸,凹为true
        Triangle []tri=new Triangle[5];
        Line[] lines= new Line[4];
        double num = 0,temp = 0;
        lines[1] = new Line(line[1].a,line[2].b);
        lines[2] = new Line(line[1].a,line[3].b);
        tri[1]= new Triangle(line[1],line[2],lines[1]);
        tri[2]= new Triangle(lines[1],line[3],lines[2]);
        tri[3]= new Triangle(lines[2],line[4],line[5]);
        num = tri[1].area()+tri[2].area()+tri[3].area();
        for(int i = 2;i<=5;i++){

            lines[1]=new Line(line[i].a,line[i%5+1].b);
            lines[2]=new Line(line[i].a,line[(i+1)%5+1].b);
            tri[1]= new Triangle(line[i],line[i%5+1],lines[1]);
            tri[2]= new Triangle(lines[1],line[(i+1)%5+1],lines[2]);
            tri[3]=new Triangle(lines[2],line[(i+2)%5+1],line[(i+3)%5+1]);
            temp = tri[1].area()+tri[2].area()+tri[3].area();
            if(Math.abs(temp-num)>0.005)
                return true;
        }
        return false;
    }
    double perimeter(){
        return line[1].dis()+line[2].dis()+line[3].dis()+line[4].dis()+line[5].dis();
    }
    void partition(Line line1){
        Quadrilateral qua = new Quadrilateral();
        System.out.printf("2 ");
        double s = area(),s2=0;
        boolean f= false;
        Triangle tri = new Triangle();
        A:for(int i= 1;i <=5;i++){
            if((line1.judgeIntersection(line[i])&&line1.judgeIntersection(line[i%5+1])&&!line1.judgeIntersection(line[(i+1)%5+1])&&
                    line1.judgeIntersection(line[(i+2)%5+1])&&line1.judgeIntersection(line[(i+3)%5+1])&&
                    (line1.sameIntersection(line[i],line[i%5+1])&&line1.sameIntersection(line[(i+3)%5+1],line[(i+2)%5+1])))||
                    (line1.judgeIntersection(line[i])&&line1.judgeIntersection(line[i%5+1])&&!line1.judgeIntersection(line[(i+1)%5+1])
                            &&!line1.judgeIntersection(line[(i+2)%5+1]) &&line1.judgeIntersection(line[(i+3)%5+1])
                            &&(line1.sameIntersection(line[i],line[i%5+1])))||
                    (line1.judgeIntersection(line[i])&&!line1.judgeIntersection(line[i%5+1])&&!line1.judgeIntersection(line[(i+1)%5+1])
                            &&line1.judgeIntersection(line[(i+2)%5+1]) &&line1.judgeIntersection(line[(i+3)%5+1])
                            &&(line1.sameIntersection(line[(i+3)%5+1],line[(i+2)%5+1])))||
                    (line1.judgeIntersection(line[i])&&!line1.judgeIntersection(line[i%5+1])&&!line1.judgeIntersection(line[(i+1)%5+1])&&!line1.judgeIntersection(line[(i+2)%5+1])&&line1.judgeIntersection(line[(i+3)%5+1]))){
                tri = new Triangle(line[i].a,line1.intersection(line[i]),line1.intersection(line[(i+3)%5+1]));
                s2 = tri.area();
                f = true;
                break A;
            }
        }

       
           B:for(int i=1;i<=5;i++){
               if((!line1.judgeIntersection(line[i])&&line1.judgeIntersection(line[i%5+1])&&!line1.judgeIntersection(line[(i+1)%5+1])&&//1
                       !line1.judgeIntersection(line[(i+2)%5+1])&&line1.judgeIntersection(line[(i+3)%5+1]))||
                       (!line1.judgeIntersection(line[i])&&line1.judgeIntersection(line[i%5+1])&&line1.judgeIntersection(line[(i+1)%5+1])&&//5
                               !line1.judgeIntersection(line[(i+2)%5+1])&&line1.judgeIntersection(line[(i+3)%5+1])&&
                               line1.sameIntersection(line[(i+1)%5+1],line[i%5+1]))){
                   qua = new Quadrilateral(line[i].a,line[i].b,line1.intersection(line[i%5+1]),line1.intersection(line[(i+3)%5+1]));
                   s2 = qua.area();
                   break B;
               }
           }


        System.out.printf(printDouble(Math.min(s2,s-s2))+" "+printDouble(Math.max(s2,s-s2)));
    }
    public static double printDouble(double num) {
        String str = String.format("%.3f",num);
        num = Double.parseDouble(str);
        return num;
    }
}




class Quadrilateral {
    Line []line = new Line[5];
    public Quadrilateral(){}
    public Quadrilateral(Point []points){
        for(int i=1;i<=4;i++){
            line[i]=new Line(points[i],points[i%4+1]);
        }
    }
    public Quadrilateral(Point point1,Point point2,Point point3,Point point4){
        line[1] = new Line(point1,point2);
        line[2]= new Line(point2,point3);
        line[3] = new Line(point3,point4);
        line[4]= new Line(point4,point1);
    }
    boolean quadrilateralOrNot(){//非法三角形之大小大类型
        return  line[1].judgeIntersection(line[3])|| line[2].judgeIntersection(line[4])  || line[1].dotLineJudgment(line[3].a) || line[1].dotLineJudgment(line[3].b) || line[3].dotLineJudgment(line[1].a) || line[3].dotLineJudgment(line[1].b);
    }
    boolean parallel(){//判断平行四边形
        return line[1].slope() == line[3].slope() && line[2].slope() == line[4].slope();
    }
    boolean judgeDiamond(){//判断是否菱形
        return parallel()&&Math.abs(line[1].dis()-line[2].dis())<0.05;
    }
    boolean judgeRectangle(){//判断矩形
        return line[1].vertical(line[2])&&line[1].judgeParallelism(line[3])&&line[2].judgeParallelism(line[4]);
    }
    boolean fourPointsCollinear(){//判断不能组成三角形或者四边形
     //   System.out.printf(line[1].slope()+" "+line[2].slope()+" "+line[3].slope()+" "+line[4].slope()+"\n");
        return line[1].slope()==line[2].slope()&&line[2].slope()==line[3].slope()&&line[3].slope()==line[4].slope();
    }

    double perimeter(){//周长
        return line[1].dis()+line[2].dis()+line[3].dis()+line[4].dis();
    }
    Triangle[] division(){//划分三角形
        Line a = new Line(),b = new Line();
        Triangle [] tri = new Triangle[5];
        for(int i=1;i<=4;i++)
            tri[i]= new Triangle();
        a.a=line[1].a;a.b=line[3].a;
        b.a=line[1].b;b.b=line[3].b;
        tri[1].line[1]=line[1];
        tri[1].line[2]=line[2];
        tri[1].line[3]=a;

        tri[2].line[1]=line[3];
        tri[2].line[2]=line[4];
        tri[2].line[3]=a;

        tri[3].line[1]=line[1];
        tri[3].line[2]=line[4];
        tri[3].line[3]=b;

        tri[4].line[1]=line[3];
        tri[4].line[2]=line[2];
        tri[4].line[3]=b;
        return tri;
    }

    double area(){//面积
        Triangle [] tri = division();
        return Math.min(tri[1].area()+tri[2].area(),tri[3].area()+tri[4].area());
    }
    boolean bump(){//判断凹凸
        Triangle [] tri = division();

        return tri[1].area() + tri[2].area() == tri[3].area() + tri[4].area();
    }
    int focalPoints(Line line2) {//求线与其焦点数量
        int num=0;
        boolean []flag={false,false,false,false,false};
        for(int i=1;i<=4;i++) {
            if (line2.judgeIntersection(line[i]) ) {
                num++;
                flag[i] = true;
            }
        }
        for(int i=1;i<=4;i++){
            if(flag[i]&&line2.sameIntersection(line[i],line[i%4+1]))
                num--;
        }
        return num;
    }//4:-1,-1 1,1 0,0 2,0 3,0 0,5
    //4:-1,-1 1,1 0,0 5,0 5,5 0,5
    Triangle generateTriangle(){
        Triangle tri =new Triangle();
        if(line[1].slope()==line[2].slope())
            tri=new Triangle(line[3],line[4],line[1].a,line[2].b);
        else if(line[3].slope()==line[2].slope())
            tri=new Triangle(line[4],line[1],line[2].a,line[3].b);
        else if(line[3].slope()==line[4].slope())
            tri=new Triangle(line[1],line[2],line[3].a,line[4].b);
        else if(line[1].slope()==line[4].slope())
            tri=new Triangle(line[2],line[3],line[4].a,line[1].b);

        return tri;
    }
    Triangle returnTriangle(){
        Triangle tri = new Triangle();
        if(line[1].judgeParallelism(line[2])){
            tri = new Triangle(line[3],line[4],line[1].a,line[2].b);
        }
        else if(line[2].judgeParallelism(line[3])){
            tri = new Triangle(line[4],line[1],line[2].a,line[3].b);
        }
        else if(line[3].judgeParallelism(line[4])){
            tri = new Triangle(line[1],line[2],line[3].a,line[4].b);
        }
        else if(line[4].judgeParallelism(line[1])){
            tri = new Triangle(line[2],line[3],line[4].a,line[1].b);
        }
        return  tri;

    }
    int parallelNum(){
        int num = 0;
        for(int i=1;i<=4;i++)
            if(line[i].judgeParallelism(line[i%4+1])){
                num++;
            }
        return num;
    }
    void pointRelation(Point point){
        Triangle a=new Triangle(),b= new Triangle(),c= new Triangle(),d= new Triangle();
        b= new Triangle(point,line[3].a,line[3].b);c= new Triangle(point,line[2].a,line[2].b);d= new Triangle(point,line[1].a,line[1].b);
        a = new Triangle(point,line[4].a,line[4].b);
        if(d.area()==0||b.area()==0||c.area()==0||a.area()==0)
            System.out.println("on the quadrilateral");
        else if(Math.abs(area()-a.area()-b.area()-c.area()-d.area())<0.0005)
            System.out.printf("in the quadrilateral");
        else
            System.out.println("outof the quadrilateral");
    }
//    Quadrilateral[] division(Quadrilateral q,Line line2){
//        return q;
//    }
    boolean illegalShape(){
        for (int i=1;i<=4;i++)
            if(line[i].slope()==line[i%4+1].slope()){
              //  System.out.printf(i+" "+(boolean)(line[i].a.x>line[i].b.x&&line[i%4+1].b.x>line[i%4+1].a.x)+" "+(boolean)(line[i].a.x<line[i].b.x&&line[i%4+1].b.x<line[i%4+1].a.x)+" "+(boolean)(line[i].a.y>line[i].b.y&&line[i%4+1].b.y>line[i%4+1].a.y)+" "+(boolean)(line[i].a.y<line[i].b.y&&line[i%4+1].b.y<line[i%4+1].a.y)+"\n");
                if((line[i].a.x>line[i].b.x&&line[i%4+1].b.x>line[i%4+1].a.x)||
                        (line[i].a.x<line[i].b.x&&line[i%4+1].b.x<line[i%4+1].a.x)||
                        (line[i].a.y>line[i].b.y&&line[i%4+1].b.y>line[i%4+1].a.y)||
                        (line[i].a.y<line[i].b.y&&line[i%4+1].b.y<line[i%4+1].a.y))
                    return true;
            }
        return false;
    }
    boolean lineEdgeIntersection(Line line2){//线和线重叠
    //    System.out.printf("%f %f %f %f\n",line[1].slope(),line[2].slope(),line[3].slope(),line[4].slope());
        return line2.overlap(line[1])  || line2.overlap(line[2]) ||
                line2.overlap(line[3]) || line2.overlap(line[4]);
    }
    void partition(Line line1){
        int num = 0;
        Quadrilateral qua = new Quadrilateral();
        System.out.printf("2 ");
        double s = area(),s2=0;
        Triangle tri = new Triangle();
        if((line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])&&
                line1.judgeIntersection(line[4])&&(line1.sameIntersection(line[1],line[2])&&line1.sameIntersection(line[3],line[4])))||
                (line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&!line1.judgeIntersection(line[3])
                        &&line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[1],line[2])))||
                (line1.judgeIntersection(line[1])&&!line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])
                        &&line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[3],line[4])))||
                (line1.judgeIntersection(line[1])&&!line1.judgeIntersection(line[2])&&!line1.judgeIntersection(line[3])&&line1.judgeIntersection(line[4]))){
            tri = new Triangle(line[1].a,line1.intersection(line[1]),line1.intersection(line[4]));
            s2 = tri.area();
        }
        else if((line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])&&
                line1.judgeIntersection(line[4])&&(line1.sameIntersection(line[2],line[3])&&line1.sameIntersection(line[1],line[4])))||
                (line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])
                        &&!line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[3],line[2])))||
                (line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&!line1.judgeIntersection(line[3])
                        &&line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[1],line[4])))||
                (line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&!line1.judgeIntersection(line[3])&&!line1.judgeIntersection(line[4]))){
            tri = new Triangle(line[2].a,line1.intersection(line[2]),line1.intersection(line[1]));
            s2 = tri.area();
        }
        else if((!line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])
                        &&line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[3],line[4])))||
                (line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])
                        &&!line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[1],line[2])))||
                (!line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])&&!line1.judgeIntersection(line[4]))){
            tri = new Triangle(line[3].a,line1.intersection(line[3]),line1.intersection(line[2]));
            s2 = tri.area();
        }
        else if((line1.judgeIntersection(line[1])&&!line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])
                        &&line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[1],line[4])))||
                (!line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])
                        &&line1.judgeIntersection(line[4]) &&(line1.sameIntersection(line[2],line[3])))||
                (!line1.judgeIntersection(line[1])&&!line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])&&line1.judgeIntersection(line[4]))){
            tri = new Triangle(line[4].a,line1.intersection(line[4]),line1.intersection(line[3]));
            s2 = tri.area();
        }
        else if(line1.judgeIntersection(line[1])&&!line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3])&&
                !line1.judgeIntersection(line[4])){
            qua = new Quadrilateral(line[1].a,line1.intersection(line[1]),line1.intersection(line[3]),line[3].b);
            s2 = qua.area();
        }
        else if(!line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&!line1.judgeIntersection(line[3])&&
                line1.judgeIntersection(line[4])){
            qua = new Quadrilateral(line[2].a,line1.intersection(line[2]),line1.intersection(line[4]),line[4].b);
            s2 = qua.area();
        }
        System.out.printf(printDouble(Math.min(s2,s-s2))+" "+printDouble(Math.max(s2,s-s2)));
    }
    public static double printDouble(double num) {
        String str = String.format("%.3f",num);
        num = Double.parseDouble(str);
        return num;
    }
}





class Triangle {
    Line []line = new Line[8];
    void printTri(){
        System.out.printf("line[1] = %f %f %f %f \nline[2] = %f %f %f %f \nline[3] = %f %f %f %f\n",
                line[1].a.x,line[1].a.y,line[1].b.x,line[1].b.y,
                line[2].a.x,line[2].a.y,line[2].b.x,line[2].b.y,
                line[3].a.x,line[3].a.y,line[3].b.x,line[3].b.y);
    }
    double area() {//三角形面积
        double p= (Math.sqrt((line[1].a.x-line[1].b.x)*(line[1].a.x-line[1].b.x)+(line[1].a.y-line[1].b.y)*(line[1].a.y-line[1].b.y))+Math.sqrt((line[2].a.x-line[2].b.x)*(line[2].a.x-line[2].b.x)+(line[2].a.y-line[2].b.y)*(line[2].a.y-line[2].b.y))+ Math.sqrt((line[3].a.x-line[3].b.x)*(line[3].a.x-line[3].b.x)+(line[3].a.y-line[3].b.y)*(line[3].a.y-line[3].b.y)) )/2;
        return Math.sqrt(p*(p- Math.sqrt((line[1].a.x-line[1].b.x)*(line[1].a.x-line[1].b.x)+(line[1].a.y-line[1].b.y)*(line[1].a.y-line[1].b.y)) )*(p - Math.sqrt((line[2].a.x-line[2].b.x)*(line[2].a.x-line[2].b.x)+(line[2].a.y-line[2].b.y)*(line[2].a.y-line[2].b.y)) )*(p- Math.sqrt((line[3].a.x-line[3].b.x)*(line[3].a.x-line[3].b.x)+(line[3].a.y-line[3].b.y)*(line[3].a.y-line[3].b.y)) ));
    }
    public Triangle(){

    }
    public Triangle(Line line1,Line line2,Point point1,Point point2){
        line[1] = new Line(line1);
        line[2] = new Line(line2);
        line[3] = new Line(point1,point2);
    }
    public Triangle(Line line1,Line line2,Line line3){
        line[1] = new Line(line1);
        line[2] = new Line(line2);
        line[3] = new Line(line3);
    }
    public Triangle(Point point1,Point point2,Point point3) {
        for(int i=1;i<=3;i++){
            line[i] = new Line();
            line[i].a= new Point();
            line[i].b = new Point();
        }
        line[1].a.x=point1.x;line[1].a.y=point1.y;
        line[1].b.x=point2.x;line[1].b.y=point2.y;

        line[2].a.x=point2.x;line[2].a.y=point2.y;
        line[2].b.x=point3.x;line[2].b.y=point3.y;

        line[3].a.x=point3.x;line[3].a.y=point3.y;
        line[3].b.x=point1.x;line[3].b.y=point1.y;
    }
    boolean legal(){
        for(int i=1;i<=3;i++) {
            if ((line[i].a.x-line[i].b.x )*(line[i%3+1].a.y-line[i%3+1].b.y)== (line[i%3+1].a.x-line[i%3+1].b.x )*(line[i].a.y-line[i].b.y))
                return false;
        }
        return true;
    }
    void pointRelation(Point point){//5:1,4 0,0 5,5 0,0 5,0
        Triangle b= new Triangle(),c= new Triangle(),d= new Triangle();
  //      System.out.printf("%f %f %f %f %f %f\n",line[1].a.x,line[1].a.y,line[2].a.x,line[2].a.y,line[3].a.x,line[3].a.y);
        b= new Triangle(point,line[3].a,line[3].b);c= new Triangle(point,line[2].a,line[2].b);d= new Triangle(point,line[1].a,line[1].b);
        if(d.area()==0||b.area()==0||c.area()==0)
            System.out.println("on the triangle");
        else if(Math.abs(area()-b.area()-c.area()-d.area())<0.0005)
            System.out.printf("in the triangle");
        else
            System.out.println("outof the triangle");
    }
    void division(Line line1){
        int num = 0;

        System.out.printf("2 ");
        double s = area(),s2=0;
        Triangle tri = new Triangle();
        if((line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3]
        )&&(line1.sameIntersection(line[1],line[2])||line1.sameIntersection(line[2],line[3])))||
                (line1.judgeIntersection(line[1])&&!line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3]))){
            tri = new Triangle(line[1].a,line1.intersection(line[1]),line1.intersection(line[3]));
        }
        if((line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3]
        )&&(line1.sameIntersection(line[1],line[3])))||
                (line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&!line1.judgeIntersection(line[3]))){
            tri = new Triangle(line[2].a,line1.intersection(line[2]),line1.intersection(line[1]));
        }
        if((line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3]
        ))|| (!line1.judgeIntersection(line[1])&&line1.judgeIntersection(line[2])&&line1.judgeIntersection(line[3]))){
            tri = new Triangle(line[1].a,line1.intersection(line[1]),line1.intersection(line[3]));
        }s2 = tri.area();
        System.out.printf(printDouble(Math.min(s2,s-s2))+" "+printDouble(Math.max(s2,s-s2)));

    }
    public  static  double areaCalculation(Point a,Point b,Point c,Line d,double s) {//计算面积case4
        Line aa = new Line(a,b);
        Line bb = new Line(a,c);
        Point x1 = aa.intersection(d);
        Point x2 = bb.intersection(d);
        double q = (a.DistanceCalculation(b)*a.DistanceCalculation(c));
        double p = (a.DistanceCalculation(x1)*a.DistanceCalculation(x2));
//        System.out.printf(p+" "+q+" ");
//        System.out.printf(s+" ");
        return (s*p)/q;
    }
    public static double printDouble(double num) {
        String str = String.format("%.3f",num);
        num = Double.parseDouble(str);
        return num;
    }

}





class Point {//点的类
    double x = 0 , y = 0 ;
    public Point(){}
    public Point(Point point){
        x= point.x;
        y= point.y;
    }
    double DistanceCalculation(Point b) {
        return Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
    }//返回两点距离
}


class PointTaking {
    String ch;
    int num2=0;

    public static double printDouble(double num) {
        String str = String.format("%.3f",num);
        num = Double.parseDouble(str);
        return num;
    }
    public static double vectorComputation(Point a,Point b,Point c){//计算点线是否相交
        double x1 = a.x-c.x, y1=a.y-c.y,x2=b.x-c.x,y2=b.y-c.y;
        return  x1*y2-x2*y1;
    }//&ensp;&ensp;

    boolean repetitionPointJudgment(Point a[],int x,int y){//相同点
        for(int i=x;i<=y;i++)//如果一样则返回
            for(int j = i+1;j<=y;j++)
                if(a[i].x==a[j].x&&a[i].y==a[j].y)
                    return true;
        return false;
    }

    public  static  double areaCalculation(Point a,Point b,Point c,Line d,double s) {//计算面积case4
        Line aa = new Line(a,b);
        Line bb = new Line(a,c);
        Point x1 = aa.intersection(d);
        Point x2 = bb.intersection(d);
        double q = (a.DistanceCalculation(b)*a.DistanceCalculation(c));
        double p = (a.DistanceCalculation(x1)*a.DistanceCalculation(x2));
//        System.out.printf(p+" "+q+" ");
//        Systemystem.out.printf(s+" ");
        return (s*p)/q;
    }
    Point [] duplicate(Point[] points){
        Point []point = new Point[10];Point []point2 = new Point[10];
        Triangle tri = new Triangle();
        boolean []flag = new boolean[]{false, false, false, false, false, false, false, false};
        boolean []flag2 = new boolean[]{false, false, false, false, false, false, false, false};
        num2 = 5;
        for(int i=3;i<=7;i++) {
            for(int j=i+1;j<=7;j++)
                if(!flag[j]&&points[i].x==points[j].x&&points[i].y==points[j].y){
                  //  System.out.printf(j+"\n");
                    flag[j]=true;
                    num2 -- ;
                }
        }
        int pos=1;
        for(int i=3;i<=7;i++)
            if(!flag[i]){
                point[pos++]= new Point(points[i]);
            }
        pos--;
        for(int i=1;i<=pos;i++){
            Line line1 = new Line(point[i],point[i%pos+1]);
            Line line2 = new Line(point[i%pos+1],point[(i+1)%pos+1]);
            if(line1.judgeParallelism(line2)) {
                if(!flag2[i%pos+1])
                {
                //    System.out.printf((i%pos+1)+"\n");
                    flag2[i%pos+1]=true;
                    num2--;
                }
            }
        }

        int pos2=1;
        for(int i=1;i<=pos;i++)
            if(!flag2[i]) {
                point2[pos2++] = new Point(point[i]);
                }
        return point2;

    }


    double Solve() {
        Point[] pointSet = new Point[15];//储存点
        int pos = 0;
        //  String[]
        String[] splitSet1 = this.ch.split(":");//将字符串以冒号分割,剥离选项与坐标
        if (splitSet1.length != 2)
            return -1;
        if (!splitSet1[0].matches("[1-5]"))//判断选项是否合规
            return -1;
        int x = Integer.parseInt(splitSet1[0]);//剥离出第选项
        int[] digitalDeposit = {0, 5, 5, 7 ,6, 5};//定义选项要的点数
        for (int i = 1; i <= 10; i++)
            pointSet[i] = new Point();//new多个点

        String[] splitSet2 = splitSet1[1].split(" ");
        for (String i : splitSet2) {//对分割的字符串遍历
            int count=2;
            String[] splitSet3 = i.split(",");
            for (String j : splitSet3) {
                if (!j.matches("^[+-]?(([1-9]\\d*\\.\\d+)|(0\\.\\d+)|([1-9]\\d*)|0)$"))//正则表达式判断是否合法
                    return -1;//返回输入错误
                int pos2 = pos / 2 + 1;
                //      System.out.printf(pos2+"");
                if ((pos & 1) == 0)//按位读入坐标
                    pointSet[pos2].x = Double.parseDouble(j);
                else if ((pos & 1) == 1)
                    pointSet[pos2].y = Double.parseDouble(j);
                pos++;
                count--;
            }
            if(count!=0)
                return -1;
        }
        if (splitSet2.length != digitalDeposit[x])
            return -2;//返回错误点数量
        return juSample(x,pointSet);
    }
    double juSample(int x, Point[] points){
        if(x==1)
            return sample1(points);
        else if(x==2)
            return sample2(points);
        else if(x==3)
            return sample3(points);
        return -1;
    }

    double sample1(Point[] points){
        Pentagon pentagon  = new Pentagon(points[1],points[2],points[3],points[4],points[5]);
        if(!pentagon.legal()||repetitionPointJudgment(points,1,5))
            return 2;
        return 1;
    }
    double sample2(Point[] points){
        if(sample1(points)==2)
            return -4;
        Pentagon pentagon  = new Pentagon(points[1],points[2],points[3],points[4],points[5]);
        if(pentagon.bump())
            return 2;
       // System.out.printf(q.bump()+"\n");
        System.out.printf("true "+printDouble(pentagon.perimeter())+" "+printDouble(pentagon.area()));

        return 5;
    }
    double sample3(Point[] points){
        if(points[1].x==points[2].x&&points[1].y == points[2].y)
            return 4;
        Line line = new Line(points[1],points[2]);
        Triangle tri = new Triangle();
        Quadrilateral qua = new Quadrilateral();
        Point []point = duplicate(points);
        if(num2==3) {
            tri = new Triangle(point[1],point[2],point[3]);
            tri.division(line);
        }
        else if(num2==4){
            qua = new Quadrilateral(point);
            qua.partition(line);
        }
        else{
            Pentagon pent = new Pentagon(points[3],points[4],points[5],points[6],points[7]);
            pent.partition(line);
        }

        return 5;
    }


}




代码可以放心食用,什么?7-2?什么7-2

drawing

posted on 2022-10-26 23:24  大皮QAQ  阅读(89)  评论(0编辑  收藏  举报

导航

回到顶部叭QAQ