面向对象程序设计第五次大作业心得

  通过这几次的大作业,已经加强了封装函数和算法的实现,我的第五次作业对前面的类和方法进行了重写,使得代码更具普适性和更加简洁,去除了三角形类四边形类等,新增一个多边形类代替之,后续代码全为面向多边形类,废话也不多说,我们直接看题!

题目集四 7-1

7-1 点线形系列5-凸五边形的计算-1
分数 50
全屏浏览题目
切换布局
作者 蔡轲
单位 南昌航空大学

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例1:

选项1,点重合。例如:

1:-1,-1 1,2 -1,1 1,0

输出样例:

在这里给出相应的输出。例如:

wrong number of points

核心思路:

新增多边形类:

  多边形类具有的属性为number(用于记录端点的数量),point数组(用于存端点坐标)(采用数组也方便对点进行遍历),line数组(用于存边的信息)

class Polygon{//多边形类
    int number;//记录端点的数量
    point[] p=new point[10];
    line[] l=new line[10];
}

 功能一:

  首先接收入点的个数和所有点的坐标并存入多边形类wu中去,接着判断是否出现点重合,如果出现点重合则无法构成五边形,如果判断为五点不重合则继续继续判断,调用panduan方法去判断五点能否构成五边形.

  功能一主体代码如下:

    public static void gongneng1(String[] str){
        pentagon wu=new pentagon();
        for(int a=0;a<5;a++)
            wu.p[a]=new point();
        for(int a=0;a<5;a++)
            wu.p[a].get(str[a]);
           if(point.chonghe(wu.p[0],wu.p[1])||point.chonghe(wu.p[0],wu.p[2])||point.chonghe(wu.p[0],wu.p[3])||point.chonghe(wu.p[0],wu.p[4])||point.chonghe(wu.p[1],wu.p[2])||point.chonghe(wu.p[1],wu.p[3])||point.chonghe(wu.p[1],wu.p[4])||point.chonghe(wu.p[2],wu.p[3])||point.chonghe(wu.p[2],wu.p[4])||point.chonghe(wu.p[3],wu.p[4]))
            System.out.println("false");
        else {//五点不重合
            if(!wu.pandaun(wu.p[0],wu.p[1],wu.p[2],wu.p[3],wu.p[4])) System.out.print("false");
            else System.out.print("true");
        }//五点不重合
    }

  多边形构成判断条件:

  1.端点数大于3

  2.任意临边不平行

  3.任意对边无交点

  任意临边不平行比较好理解,任意对边无交点的意思是,任意不相邻的边都无交点,如下图所示,假设当前遍历的边为L0,则需要判断L0与L2,L3是否有交点,如果没交点则L0边合法,当所有边合法才能构成多边形

 

  panduan方法具体代码如下:

boolean panduan(){
        if(this.number<3) return false;
        point p=new point();
        for(int a=0;a<this.number;a++){//将边的信息存入多边形类
            this.l[a]=new line();
            this.l[a].panduan(this.p[a],this.p[(a+1)%this.number]);
        }//将边的信息存入多边形类
        for(int a=0;a<this.number;a++){
            if(line.pingxing(this.l[a],this.l[(a+1)%this.number])) return false;//临边不能平行 否则返回false
            for(int b=(a+2)%this.number;b!=(a-1+this.number)%this.number;b=(b+1)%this.number){//对边不能有交点 否则返回false
                if(!line.pingxing(this.l[a],this.l[b])&&line.jiaodian(this.l[a],this.l[b],p)&&line.jiaodian(this.l[b],this.l[a],p)) return false;

            }//对边无交点 否则返回false
        }
        return true;
    }

  功能二:

   功能二较为简单,接收入多边形的所有点后,判断凹凸性即可,通过对边进行遍历,用当前遍历边的向量点乘下一条边向量,如果出现点乘结果为负数,则说明出现大于90°的边(即凹多边型)

    public static void gongneng2(String[] str){
        pentagon wu=new pentagon();
        for(int a=0;a<5;a++)
            wu.p[a]=new point();
        for(int a=0;a<5;a++)
            wu.p[a].get(str[a]);
        if(!wu.pandaun(wu.p[0],wu.p[1],wu.p[2],wu.p[3],wu.p[4])) System.out.print("not a pentagon");
        else{//可以构成五边形
            if(!wu.aotu()) System.out.print("false");
            else {//凸五边形
                System.out.print("true ");
                way.shuchu(wu.zhouchang());;
                System.out.print(" ");
                way.shuchu(wu.mianji());

            }//凸五边形
        }//可以构成五边形
    }
    boolean aotu(){//判断凹凸性
        boolean tu=true;
        for(int a=0;a<5;a++){
            if(!line.diancheng(this.l[a],this.l[(a+1)%5])) return false;
        }
        return true;
    }//判断凹凸性
    public static boolean diancheng(line d,line e)
    {//点乘结果为负则返回false
        double x1,y1,x2,y2;
        x1=d.p2.x-d.p1.x;
        y1=d.p2.y-d.p1.y;
        x2=e.p2.x-e.p1.x;
        y2=e.p2.y-e.p1.y;
        if(x1*x2+y1*y2<-1e-6) return false;
        else return true;
    }//点乘结果为负则返回false

功能三:

  功能三主题思路为,先接收入多边形的所有点,以及直线l的端点,然后对多边形对象调用qudian方法,qudian方法的功能为通过多边形的点集去除所有冗余点(具体实现在下文),去除冗余点后,对多边形进行遍历,判断当前遍历线段的左端点是否在直线l上,如果在,则存入点集jd(该点集存的是算面积的点,需要保证有序,因为要构成多边形,所以是遍历存点),如果该点不在直线l上,则调用shangxia方法(具体实现在下文)判断该点是否在直线的左侧(右侧也行,统一选择一侧即可),如果在,则也存入点集,然后再判断当前遍历线段与直线是否有交点,如果有交点则存入交点集,(一定要先判断当前线段的左侧端点再判断当前直线的交点,保证点集的有序性),最后点集jd调用mianji方法(具体实现再下文)计算出面积s1.

public static void gongneng3(String[] str){//功能三
        point[] p=new point[5];
        int jiaodiannum=0;
        Polygon i=new Polygon();
        line l=new line();
        l.p1=new point();l.p2=new point();
        l.p1.get(str[0]);l.p2.get(str[1]);
        for(int a=0;a<5;a++)
            p[a]=new point();
        for(int a=0;a<5;a++)
            p[a].get(str[a+2]);

       if(l.panduan(l.p1,l.p2)) System.out.println("points coincide");
       else{//直线两点不重合
           i.number=5;
           i.qudian(p,5);//去除冗余点

           if(!i.panduan()) System.out.println("not a polygon");
           else{//能构成多边形
               int numjd=0;
               boolean cf=false;//用于记录是否重复
               point u=new point();//暂存交点
               point[] jd=new point[5];//存储算面积的点
               for(int a=0;a<i.number;a++){//取线段一侧的端点集 取交点集
                   if(l.shangxia(i.p[a])){//取线段一侧的端点
                       jd[numjd]=new point();
                       jd[numjd]=i.p[a];
                       numjd++;
                   }//取线段一侧的端点
                   if(line.jiaodian(l,i.l[a],u)) {//线段上有交点
                       for(int b=0;b<numjd&&!cf;b++){//查找是否重复
                           if(point.chonghe(jd[b],u)) cf=true;
                       }//查找是否重复
                       if(!cf){//新交点出现
                           jd[numjd]=new point();
                           jd[numjd].x=u.x;
                           jd[numjd].y=u.y;
                           numjd++;
                           jiaodiannum++;
                       }//新交点出现
                       else cf=false;
                   }//线段上有交点
               }//取交点集 取线段下方的点集


               System.out.print(jiaodiannum);
               if(jiaodiannum==2){
                   double s1,sz,s2;
                   sz=Polygon.mianji(i.p,i.number);
                   s1=Polygon.mianji(jd,numjd);

                   if(sz-s1<s1) s2=sz-s1;
                   else{
                       s2=s1;
                       s1=sz-s1;
                   }
                   System.out.print(" ");
                   way.shuchu(s2);
                   System.out.print(" ");
                   way.shuchu(s1);
               }
           }//能构成多边形
       }//直线两点不重合
    }//功能三

  首先对点进行遍历,去除重合的点,然后再对新的点集进行遍历,如果当前点在前一个点和后一个点所构成的线段上,则说明当前点为冗余点

    void qudian(point[] w,int num) {//取出冗余点
        point[] z=new point[5];
        point front, next;
        int n = 0,number=0;
        boolean chonghe=false;

        for(int a=0;a<num;a++){//外循环 去除重复的点
            for(int b=0;b<number&&!chonghe;b++){//内循环
                if(point.chonghe(w[a],z[b])) chonghe=true;
            }//内循环
            if(!chonghe) {
                z[number]=new point();
                z[number]=w[a];
                number++;
            }
            else chonghe=false;
        }//外循环
        this.number=number;
        for (int a = 0; a < number; a++) {//点遍历
            front = z[(a - 1 + number) % number];
            next = z[(a + 1) % number];
            if(line.pointon(front,z[a],next)) this.number--;//冗余点
            else {
                this.p[n] = z[a];
                n++;
            }
        }//点遍历
    }

  把点坐标带入直线方程的一般式,(ax+by+c),如果结果大于0,说明该点在直线的左侧,则返回true

    boolean shangxia(point p){
        if(this.a*p.x+this.b*p.y+this.c>0) return true;
        return false;
    }

  多边形面积算法:

使用的是一个鞋带定理,具体证明见下述网站:

【国际数学竞赛】任意多边形面积计算公式 - 知乎 (zhihu.com)

    public static double mianji(point[] p,int number){
        double mix=0;
        for(int a=0;a<number;a++){
            mix+=p[a].x*p[(a+1)%number].y-p[a].y*p[(a+1)%number].x;
        }
        mix=0.5*Math.abs(mix);
        return mix;
    }

题目集五 7-2

7-2 点线形系列5-凸五边形的计算-2
作者 蔡轲
单位 南昌航空大学

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是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坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例:

在这里给出一组输入。例如:

4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6

输出样例:

在这里给出相应的输出。例如:

the previous pentagon coincides with the following pentagon

思路:

 功能四:

  对两个多边形初始化后,依次判断两多边形的关系然后输出结果,我的判断优先顺序为:重合,包含,被包含,分离,交错,剩余的情况就全是相交,具体情况判断方法见下文

    public static void gongneng1(String[] str){
        String[] st=new String[3];
        st[0]="triangle";
        st[1]="quadrilateral";
        st[2]="pentagon";
        Polygon i1=new Polygon(),i2=new Polygon();
        point[] p1=new point[5];
        point[] p2=new point[5];
        for(int a=0;a<5;a++){//初始化p1 p2数组
            p1[a]=new point();
            p2[a]=new point();
        }//初始化
        for(int a=0;a<5;a++)
            p1[a].get(str[a]);
        for(int a=0;a<5;a++)
            p2[a].get(str[a+5]);
        i1.number=5;
        i1.qudian(p1,5);//去除冗余点
        i2.number=5;
        i2.qudian(p2,5);//去除冗余点
        i1.biancsh();
        i2.biancsh();
        if(Polygon.chonghe(i1,i2)) {
            System.out.print("the previous "+st[i1.number-3]+" coincides with the following "+st[i2.number-3]);
        }
        else if(Polygon.baohan(i1,i2)){
            System.out.print("the previous "+st[i1.number-3]+" contains the following "+st[i2.number-3]);
        }
        else if(Polygon.baohan(i2,i1)){
            System.out.print("the previous "+st[i1.number-3]+" is inside the following "+st[i2.number-3]);
        }
        else if(Polygon.fenli(i1,i2)){
            System.out.print("no overlapping area between the previous "+st[i1.number-3]+" and the following "+st[i2.number-3]);
        }
        else if(Polygon.jiaocuo(i1,i2)){
            System.out.print("the previous "+st[i1.number-3]+" is interlaced with the following "+st[i2.number-3]);
        }
        else {
            System.out.print("the previous "+st[i1.number-3]+" is connected to the following "+st[i2.number-3]);
        }
    }

  判断是否重合,需要对一个四边形进行遍历,因为无法确定相同的起始点(因为存储顺序可能不同),还要分顺逆时针进行遍历(可能一个是顺时针存的另一个是逆时针存的),只要一种情况完全重合即可

    public static boolean chonghe(Polygon i1,Polygon i2){
        if(i1.number!=i2.number) return false;
        boolean xiangdeng=false;
        int a=0;
        for(;a<i1.number&&!xiangdeng;a++){//遍历找到一对重合的点
            if(point.chonghe(i1.p[0],i2.p[a])) xiangdeng=true;
        }//遍历找到一对重合的点
        a--;
        if(!xiangdeng) return false;//没找到重合的点 说明不是完全重合
        else{
            for(int b=0;b<i1.number&&xiangdeng;b++){//遍历检测顺时针是否重合
                if(point.chonghe(i1.p[b],i2.p[(a+b)%i1.number])) xiangdeng=true;
                else xiangdeng=false;
            }
            if(xiangdeng) return true;
            else xiangdeng=true;
            for(int b=0;b<i1.number&&xiangdeng;b++){//遍历检测逆时针是否重合
                if(point.chonghe(i1.p[b],i2.p[(a-b+i1.number)%i1.number])) xiangdeng=true;
                else xiangdeng=false;
            }
            if(xiangdeng) return true;
            else return false;
        }
    }

  判断包含关系也非常简单,对第二个多边形的点进行遍历,如果每个点均在第一个多边形内,则包含成立

    public static boolean baohan(Polygon i1,Polygon i2){//判断i1是否包含i2
        for(int a=0;a<i2.number;a++){
            if(!i1.inPolygon(i2.p[a])) return false;
        }
        return true;
    }
    boolean inPolygon(point p){//判断point是否在多边形内
        double mix=0;
        point[] sp=new point[3];
        sp[0]=p;
        for(int a=0;a<this.number;a++){
            sp[1]=this.p[a];
            sp[2]=this.p[(a+1)%this.number];
            mix+=Polygon.mianji(sp,3);
        }
        if(mix==Polygon.mianji(this.p,this.number)) return true;
        else return false;
    }

  分离:对两个多边形进行遍历,每个多边形均无端点在另一个多边形内即可

    public static boolean fenli(Polygon i1,Polygon i2){
        for(int a=0;a<i2.number;a++){
            if(i1.inPolygon(i2.p[a])) return false;
        }
        for(int a=0;a<i1.number;a++){
            if(i2.inPolygon(i1.p[a])) return false;
        }
        return true;
    }

  交错:

    public static boolean jiaocuo(Polygon i1,Polygon i2){
        boolean cf=false;
        point[] jdj=new point[10];
        int numjd=0;
        point p=new point();
        for(int a=0;a<i1.number;a++){
            for(int b=0;b<i2.number;b++){
                if(line.jiaodian2(i1.l[a],i2.l[b],p)&&line.jiaodian2(i2.l[b],i1.l[a],p)) return true;
            }
        }
        for(int a=0;a<i1.number;a++){//外循环
            for(int b=0;b<i2.number;b++){//内循环
                if(line.pointon(i2.l[b].p1,i1.p[a],i2.l[b].p2)) {
                    for(int c=0;c<numjd&&!cf;c++){//查找是否重复
                        if(point.chonghe(jdj[c],i1.p[a])) cf=true;
                    }//查找是否重复
                    if(!cf){//新交点出现
                        jdj[numjd]=new point();
                        jdj[numjd].x=i1.p[a].x;
                        jdj[numjd].y=i1.p[a].y;
                        numjd++;
                    }//新交点出现
                    else cf=false;
                }
            }//内循环
        }//外循环
        for(int a=0;a<i2.number;a++){//外循环
            for(int b=0;b<i1.number;b++){//内循环
                if(line.pointon(i1.l[b].p1,i2.p[a],i1.l[b].p2)) {
                    for(int c=0;c<numjd&&!cf;c++){//查找是否重复
                        if(point.chonghe(jdj[c],i2.p[a])) cf=true;
                    }//查找是否重复
                    if(!cf){//新交点出现
                        jdj[numjd]=new point();
                        jdj[numjd].x=i2.p[a].x;
                        jdj[numjd].y=i2.p[a].y;
                        numjd++;
                    }//新交点出现
                    else cf=false;
                }
            }//内循环
        }//外循环
        if(numjd>2) return true;
        return false;
    }

功能五:

 功能五较为复杂,简单的处理完多边形后,要求重合部分的面积,所以我们需要取出点集,用两层循环控制取出所有两个多边形的交点,但是这里取出的多边形的点集是无序的,无法调用多边形面积公式,所以需要对点集进行排序,

定义:点A在点B的逆时针方向,则点A大于点B

1.计算点集的重心O,以重心作为逆时针旋转的中心点。

2.计算点之间的大小关系。

大小关系的计算,可由两种方法进行计算。

方法1:

以重心O作一条平行于X轴的单位向量OX,然后依次计算OPi和OX的夹角,根据夹角的大小,确定点之间的大小关系。

OPi和OX夹角越大,说明点Pi越小,如图所示。

    public static void gongneng2(String[] str){
        int numon=0,numdown=0,mjp=0;
        Polygon i1=new Polygon(),i2=new Polygon();
        point[] mjpoint=new point[10];
        point[] p1=new point[5];
        point[] p2=new point[5];
        point[] on=new point[10];
        point[] down=new point[10];
        for(int a=0;a<5;a++){//初始化p1 p2数组
            p1[a]=new point();
            p2[a]=new point();
        }//初始化
        for(int a=0;a<5;a++)
            p1[a].get(str[a]);
        for(int a=0;a<5;a++)
            p2[a].get(str[a+5]);
        i1.number=5;
        i1.qudian(p1,5);//去除冗余点
        i2.number=5;
        i2.qudian(p2,5);//去除冗余点
        i1.biancsh();
        i2.biancsh();
        point u=new point();
        for(int a=0;a<i1.number;a++)
            if(i2.inPolygon(i1.p[a])) mjpoint[mjp++]=i1.p[a];
        for(int a=0;a<i2.number;a++)
            if(i1.inPolygon(i2.p[a])) mjpoint[mjp++]=i2.p[a];
        for(int a=0;a<i1.number;a++){//外循环
            for(int b=0;b<i2.number;b++){//内循环
                if(line.jiaodian2(i1.l[a],i2.l[b],u)&&line.jiaodian2(i2.l[b],i1.l[a],u)) {
                    mjpoint[mjp]=new point();
                    mjpoint[mjp].x=u.x;
                    mjpoint[mjp++].y=u.y;
                }
            }//内循环
        }//外循环
        point zx=new point();//重心
        point zx2=new point();//重心的x加1所在的点 用于做法向量
        int X=0,Y=0;
        for(int a=0;a<mjp;a++){
            X+=mjpoint[a].x;
            Y+=mjpoint[a].y;
        }
        zx.x=X/mjp;
        zx.y=Y/mjp;
        zx2.x=X/mjp+1;
        zx2.y=Y/mjp;
        line l=new line();//法向量所在直线
        l.panduan(zx,zx2);
        for(int a=0;a<mjp;a++){
            if(l.onordown(mjpoint[a])) on[numon++]=mjpoint[a];
            else down[numdown++]=mjpoint[a];
        }
        Polygon.paixu(on,down,numon,numdown,l);
        for(int a=0;a<numon;a++)
            mjpoint[a]=on[a];
        for(int a=0;a<numdown;a++)
            mjpoint[a+numon]=down[a];
        way.shuchu(Polygon.mianji(mjpoint,mjp));
    }
    public static void paixu(point[] p1,point[] p2,int nump1,int nump2,line l){//对点集进行排序
        double[] cos=new double[nump1+nump2];
        int max,min;
        for(int a=0;a<nump1;a++)
            cos[a]=((l.p2.x-l.p1.x)*(p1[a].x-l.p1.x)+(l.p1.y-l.p2.y)*(p1[a].y-l.p1.y))/(Math.sqrt((l.p2.x-l.p1.x)*(l.p2.x-l.p1.x)+(l.p1.y-l.p2.y)*(l.p1.y-l.p2.y))*Math.sqrt((p1[a].x-l.p1.x)*(p1[a].x-l.p1.x)+(p1[a].y-l.p1.y)*(p1[a].y-l.p1.y)));
        for(int a=0;a<nump2;a++){
            cos[a+nump1]=((l.p2.x-l.p1.x)*(p2[a].x-l.p1.x)+(l.p1.y-l.p2.y)*(p2[a].y-l.p1.y))/(Math.sqrt((l.p2.x-l.p1.x)*(l.p2.x-l.p1.x)+(l.p1.y-l.p2.y)*(l.p1.y-l.p2.y))*Math.sqrt((p2[a].x-l.p1.x)*(p2[a].x-l.p1.x)+(p2[a].y-l.p1.y)*(p2[a].y-l.p1.y)));
        }
        point tmpp=new point();
        double tmp;

        for(int a=0;a<nump1;a++){//冒泡排序 降序
            max=a;
            for(int b=a+1;b<nump1;b++)
                if(cos[b]>cos[max]) max=b;
            tmp=cos[a];
            cos[a]=cos[max];
            cos[max]=tmp;
            tmpp=p1[a];
            p1[a]=p1[max];
            p1[max]=tmpp;
        }
        for(int a=0;a<nump2;a++){//冒泡排序 升序
            min=a;
            for(int b=a+1;b<nump2;b++)
                if(cos[b+nump1]<cos[min+nump1]) min=b;
            tmp=cos[a+nump1];
            cos[a+nump1]=cos[min+nump1];
            cos[min+nump1]=tmp;
            tmpp=p2[a];
            p2[a]=p2[min];
            p2[min]=tmpp;
        }
    }//对点集进行排序

功能六:

  只要多边形的每个点均在另一个多边形内,则说明第一个多边形在另一个多边形内

    public static void gongneng3(String[] str){
        String[] st=new String[3];
        st[0]="triangle";
        st[1]="quadrilateral";
        st[2]="pentagon";
        Polygon i1=new Polygon();
        point[] p1=new point[5];
        point pp=new point();
        boolean online=false;
        for(int a=0;a<5;a++){//初始化p1数组
            p1[a]=new point();
        }//初始化
        pp.get(str[0]);
        for(int a=0;a<5;a++)
            p1[a].get(str[a+1]);
        i1.number=5;
        i1.qudian(p1,5);//去除冗余点
        for(int a=0;a<i1.number&&!online;a++)
            if(line.pointon(i1.p[a],pp,i1.p[(a+1)%i1.number])) online=true;
        if(online) System.out.println("on the "+st[i1.number-3]);
        else{
            if(i1.inPolygon(pp)) System.out.println("in the "+st[i1.number-3]);
            else System.out.println("outof the "+st[i1.number-3]);

        }
    }//功能三

心得:

这次大作业重写了之前的类和大部分方法,通过这次实验学到了构造类时有很多种方法,其中的权衡利弊需要考虑清楚再去写,不能想当然的就开写

 

posted @ 2022-10-29 11:55  北顾Jeffrey  阅读(171)  评论(0)    收藏  举报