第四次大作业、第五次大作业和期中考试总结
目录一、前言
1.第四次大作业
点线形系列4-凸四边形的计算,该题有五个选项,选项一输入四个点,判断能否构成四边形和平行四边形;选项二输入四个点坐标,判断能否构成菱形、矩形和正方形;选项三输入四个点坐标,判断能否构成凹凸四边形,并输出四边形的面积、周长;选项四输入六个点坐标,前两个点构成一条直线,后面四个点构成一个多边形,并输出被直线分割的面积;选项五输入五个点坐标,判断第一个点是否在后四个点构成多边形的内部。以上五个选项均需要判断输入的格式是否准确。
2.第五次大作业
(1)点线形系列5-凸五边形的计算-1,该题有三个选项,选项一输入五个点坐标,判断能否构成五边形;选项二输入五个点坐标,判断能否构成凹凸五边形,并输出五边形的周长、面积;选项三输入七个点坐标,前两个点构成一条直线,后面五个点构成一个多边形,求直线与多边形的交点个数,如果交点个数为2,输出被切割的面积。以上三个选项均需要判断输入的格式是否准确。
(2)点线形系列5-凸五边形的计算-2,该题有三个选项,在上一题的基础上增加的选项,选项四输入十个点坐标,前五个点和后五个点构成一个多边形,判断这两个多边形之间存在哪种关系(分离、连接、完全重合、被包含、交错、包含);选项五输入十个点坐标,前五个点和后五个点构成多边形,输出这两多边形公共区域的面积;选项六输入六个点坐标,判断第一个点与后五个点的关系(内部、边上、外部)。以上三个选项均需要判断输入格式是否准确。
3.期中考试
(1)点与线(类设计),根据题目给出的类图,设计点类和线类,主函数调用点类和线类的方法对输入进行处理。点类中包含下x,y两个属性,有没有参数的构造方法和带参数的构造方法,以及输出display方法;线类中包含两个点属性和一个color属性,有没有参数的构造方法和带参数的构造方法,以及输出display方法。
(2)点线面问题重构(继承与多态),在第一题的基础上,将点类和线类进一步抽象,定义一个共同父类Element(抽象类),在抽象类中声明display方法,点类和线类是抽象类的子类;再定义一个子类面类,该类只有属性颜色,display方法输出面的颜色。
(3)点线面问题再重构(容器类),在前两题的基础上,增加容器类保存点、线、面对象,对该容器有五个选项(输入结束、增加点、增加线、增加面、删除某个位置的对象),结束后依次遍历容器内的对象,依次调用每一个对象的display方法输出。
二、设计与分析
1.第四次大作业
点线形系列4-凸四边形的计算
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是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
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。
代码如下(点击展开)
package no4;
import java.util.Scanner;
public class Main2{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String s = input.nextLine();
if(s.length()<=2){
System.out.println("Wrong Format");
return;
}
int x = (int)s.charAt(0)-'0';
if(s.charAt(1)!=':'){
System.out.println("Wrong Format");
return;
}
String[] c = s.substring(2).split(" ");
for(int i=0;i<c.length;i++){
String[] m = c[i].split(",");
if(m.length!=2){
System.out.println("Wrong Format");
return;
}
for(int j=0;j<m.length;j++){
if(!m[j].matches("^[+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?)")){
System.out.println("Wrong Format");
return;
}
}
}
if(!(x>=1&&x<=5)){
System.out.println("Wrong Format");
return;
}
switch (x){
case 1:{
if(c.length!=4){
System.out.println("wrong number of points");
return;
}
Point v1 = new Point(), v2 = new Point(), v3 = new Point(), v4 = new Point();
String[] v = c[0].split(",");
v1.x = Double.parseDouble(v[0]);
v1.y = Double.parseDouble(v[1]);
String[] u = c[1].split(",");
v2.x = Double.parseDouble(u[0]);
v2.y = Double.parseDouble(u[1]);
String[] w = c[2].split(",");
v3.x = Double.parseDouble(w[0]);
v3.y = Double.parseDouble(w[1]);
String[] p = c[3].split(",");
v4.x = Double.parseDouble(p[0]);
v4.y = Double.parseDouble(p[1]);
if(v1.x==v2.x&&v1.y==v2.y||v2.x==v3.x&&v2.y==v3.y||v3.x==v4.x&&v3.y==v4.y||v4.x==v1.x&&v4.y==v1.y||v2.x==v4.x&&v2.y==v4.y||v1.x==v3.x&&v1.y==v3.y){
System.out.println("points coincide");
return;
}
Quadrangle q = new Quadrangle();
q.v1=v1;q.v2=v2;q.v3=v3;q.v4=v4;
if(q.if_quad()==0){
System.out.println("false false");
}
else if(q.if_quad()==1){
System.out.println("true false");
}
else{
System.out.println("true true");
}
return;
}
case 2:{
if(c.length!=4){
System.out.println("wrong number of points");
return;
}
Point v1 = new Point(), v2 = new Point(), v3 = new Point(), v4 = new Point();
String[] v = c[0].split(",");
v1.x = Double.parseDouble(v[0]);
v1.y = Double.parseDouble(v[1]);
String[] u = c[1].split(",");
v2.x = Double.parseDouble(u[0]);
v2.y = Double.parseDouble(u[1]);
String[] w = c[2].split(",");
v3.x = Double.parseDouble(w[0]);
v3.y = Double.parseDouble(w[1]);
String[] p = c[3].split(",");
v4.x = Double.parseDouble(p[0]);
v4.y = Double.parseDouble(p[1]);
Quadrangle q = new Quadrangle();
q.v1=v1;q.v2=v2;q.v3=v3;q.v4=v4;
if(q.if_quad()==0){
System.out.println("not a quadrilateral");
}
else if(q.if_quad()==1){
System.out.println("false false false");
}
else{
int f = q.lin_chang_zheng();
if(f==0){
System.out.println("false false false");
}
else if(f==1){
System.out.println("true false false");
}
else if(f==2){
System.out.println("false true false");
}
else{
System.out.println("true true true");
}
}
return;
}
case 3:{
if(c.length!=4){
System.out.println("wrong number of points");
return;
}
Point v1 = new Point(), v2 = new Point(), v3 = new Point(), v4 = new Point();
String[] v = c[0].split(",");
v1.x = Double.parseDouble(v[0]);
v1.y = Double.parseDouble(v[1]);
String[] u = c[1].split(",");
v2.x = Double.parseDouble(u[0]);
v2.y = Double.parseDouble(u[1]);
String[] w = c[2].split(",");
v3.x = Double.parseDouble(w[0]);
v3.y = Double.parseDouble(w[1]);
String[] p = c[3].split(",");
v4.x = Double.parseDouble(p[0]);
v4.y = Double.parseDouble(p[1]);
Quadrangle q = new Quadrangle();
q.v1=v1;q.v2=v2;q.v3=v3;q.v4=v4;
if(q.if_quad()==0){
System.out.println("not a quadrilateral");
return;
}
double s0,s1,s2;
s1 = Point.mianji(q.v1,q.v2,q.v3)+Point.mianji(q.v1,q.v3,q.v4);
s2 = Point.mianji(q.v1,q.v2,q.v4)+Point.mianji(q.v2,q.v3,q.v4);
if(s1 == s2){
System.out.print("true ");
}
else {
System.out.print("false ");
}
Line l1 = new Line(), l2 = new Line(), l3 = new Line(), l4 = new Line();
l1.a1 = v1;l1.a2 = v2;l2.a1 = v2;l2.a2 = v3;
l3.a1 = v3;l3.a2 = v4;l4.a1 = v4;l4.a2 = v1;
l1.length();l2.length();l3.length();l4.length();
double l = l1.l+l2.l+l3.l+l4.l;
if(s1<s2){
s0=s1;
}
else{
s0=s2;
}
String str;
str = String.format("%.3f",l);
l = Double.parseDouble(str);
str = String.format("%.3f",s0);
s0 = Double.parseDouble(str);
System.out.println(l+" "+s0);
return;
}
case 4:{
if(c.length!=6){
System.out.println("wrong number of points");
return;
}
Point v1 = new Point(), v2 = new Point(), v3 = new Point(), v4 = new Point(), v5 = new Point(), v6 = new Point();
String[] v = c[0].split(",");
v1.x = Double.parseDouble(v[0]);
v1.y = Double.parseDouble(v[1]);
String[] u = c[1].split(",");
v2.x = Double.parseDouble(u[0]);
v2.y = Double.parseDouble(u[1]);
String[] w = c[2].split(",");
v3.x = Double.parseDouble(w[0]);
v3.y = Double.parseDouble(w[1]);
String[] p = c[3].split(",");
v4.x = Double.parseDouble(p[0]);
v4.y = Double.parseDouble(p[1]);
w = c[4].split(",");
v5.x = Double.parseDouble(w[0]);
v5.y = Double.parseDouble(w[1]);
p = c[5].split(",");
v6.x = Double.parseDouble(p[0]);
v6.y = Double.parseDouble(p[1]);
Quadrangle q = new Quadrangle();
q.v1=v3;q.v2=v4;q.v3=v5;q.v4=v6;
if(v1.x==v2.x&&v1.y==v2.y){
System.out.println("points coincide");
return;
}
Line l0 = new Line();
l0.a1 = v1;l0.a2 = v2;
Line l1 = new Line(), l2 = new Line(), l3 = new Line(), l4 = new Line(), l5 = new Line(), l6 = new Line();
l1.a1 = v3;l1.a2 = v4;l2.a1 = v4;l2.a2 = v5;
l3.a1 = v5;l3.a2 = v6;l4.a1 = v6;l4.a2 = v3;
l5.a1 = v3;l5.a2 = v5;l6.a1 = v4;l6.a2 = v6;
l0.slope();l1.slope();l2.slope();l3.slope();l4.slope();l5.slope();l6.slope();
l0.length();l1.length();l2.length();l3.length();l4.length();l5.length();l6.length();
// if(Line.chonghe(l1,l0)||Line.chonghe(l2,l0)||Line.chonghe(l3,l0)||Line.chonghe(l4,l0)){
// System.out.println("The line is coincide with one of the lines");
// return;
// }
double[] s0=new double[3];
if(q.if_quad()==0){
double[] s3=new double[7];
s3=q.if_triangle();
if(s3[0]==0){
System.out.println("not a quadrilateral or triangle");
return;
}
else {
if(Line.chonghe(l1,l0)||Line.chonghe(l2,l0)||Line.chonghe(l3,l0)||Line.chonghe(l4,l0)){
System.out.println("The line is coincide with one of the lines");
return;
}
Point[] p3 = new Point[3];
p3[0] = new Point();p3[1]=new Point();p3[2]=new Point();
p3[0].x=s3[1];p3[0].y=s3[2];
p3[1].x=s3[3];p3[1].y=s3[4];
p3[2].x=s3[5];p3[2].y=s3[6];
Line l3_1=new Line(),l3_2=new Line(),l3_3=new Line();
l3_1.a1=p3[0];l3_1.a2=p3[1];
l3_2.a1=p3[1];l3_2.a2=p3[2];
l3_3.a1=p3[2];l3_3.a2=p3[0];
l3_1.slope();l3_2.slope();l3_3.slope();
s0=Quadrangle.jiequ_mianji(l3_1,l3_2,l3_3,l0);
if(s0[0]==0){
System.out.println("0");
}
else if(s0[0]==1){
System.out.println("1");
}
else if(s0[0]==2){
s0[1] = sishewuru_3(s0[1]);
s0[2] = sishewuru_3(s0[2]);
System.out.println("2 "+s0[1]+" "+s0[2]);
}
return;
}
}
else {
if(Line.chonghe(l1,l0)||Line.chonghe(l2,l0)||Line.chonghe(l3,l0)||Line.chonghe(l4,l0)){
System.out.println("The line is coincide with one of the lines");
return;
}
double s1=0,s2=0;
Point p10=new Point(),p20=new Point(),p30=new Point(),p40=new Point();
p10 = Line.jiaodian(l1,l0);
p20 = Line.jiaodian(l2,l0);
p30 = Line.jiaodian(l3,l0);
p40 = Line.jiaodian(l4,l0);
boolean f1,f2,f3,f4,f0=false;
f1=Line.xianduanshang(v3,v4,p10);
f2=Line.xianduanshang(v4,v5,p20);
f3=Line.xianduanshang(v5,v6,p30);
f4=Line.xianduanshang(v6,v3,p40);
if(p10.equals(v4)&&p30.equals(v6)){
s1 = Point.mianji(v3,v4,v6);
s2 = Point.mianji(v5,v4,v6);
f0=true;
}
else if(p10.equals(v3)&&p20.equals(v5)){
s1 = Point.mianji(v4,v3,v5);
s2 = Point.mianji(v6,v3,v5);
f0=true;
}
else if(p10.equals(v3)&&f2){
s1=Point.mianji(v3,v4,p20);
s2=Point.mianji(p10,p20,v5)+Point.mianji(p10,v5,v6);
f0=true;
}
else if(p10.equals(v3)&&f3){
s1=Point.mianji(p10,p30,v6);
s2=Point.mianji(p10,p30,v5)+Point.mianji(p10,v5,v4);
f0=true;
}
else if(p10.equals(v4)&&f3){
s1=Point.mianji(p10,v5,p30);
s2=Point.mianji(p10,p30,v6)+Point.mianji(p10,v6,v3);
f0=true;
}
else if(p10.equals(v4)&&f4){
s1=Point.mianji(p10,v3,p40);
s2=Point.mianji(p10,p40,v6)+Point.mianji(p10,v5,v6);
f0=true;
}
else if(p20.equals(v5)&&f4){
s1=Point.mianji(p20,v6,p40);
s2=Point.mianji(p20,p40,v3)+Point.mianji(p20,v3,v4);
f0=true;
}
else if(p20.equals(v5)&&f1){
s1=Point.mianji(p20,v4,p10);
s2=Point.mianji(p20,v6,v3)+Point.mianji(p20,v3,p10);
f0=true;
}
else if(p30.equals(v6)&&f1){
s1=Point.mianji(p30,v3,p10);
s2=Point.mianji(p30,p10,v4)+Point.mianji(p30,v4,v5);
f0=true;
}
else if(p30.equals(v6)&&f2){
s1=Point.mianji(p30,p20,v5);
s2=Point.mianji(p30,p20,v4)+Point.mianji(p30,v4,v3);
f0=true;
}
else if(f1&&f2){
s1=Point.mianji(v4,p10,p20);
s2=Point.mianji(p10,p20,v5)+Point.mianji(p10,v5,v6)+Point.mianji(p10,v6,v3);
f0=true;
}
else if(f1&&f3){
s1=Point.mianji(p10,v4,v5)+Point.mianji(p10,v5,p30);
s2=Point.mianji(p10,p30,v6)+Point.mianji(p10,v6,v3);
f0=true;
}
else if(f1&&f4){
s1=Point.mianji(v3,p10,p40);
s2=Point.mianji(p10,v4,v5)+Point.mianji(p10,v5,v6)+Point.mianji(p10,p40,v6);
f0=true;
}
else if(f2&&f3){
s1=Point.mianji(p20,p30,v5);
s2=Point.mianji(p20,v4,v3)+Point.mianji(p20,v3,v6)+Point.mianji(p20,p30,v6);
f0=true;
}
else if(f2&&f4){
s1=Point.mianji(p20,v4,v3)+Point.mianji(p20,v3,p40);
s2=Point.mianji(p20,v5,v6)+Point.mianji(p20,v6,p40);
f0=true;
}
else if(f3&&f4){
s1=Point.mianji(p30,p40,v6);
s2=Point.mianji(p30,v5,v4)+Point.mianji(p30,v4,v3)+Point.mianji(p30,v3,p40);
f0=true;
}
else if(p10.equals(v3)||p10.equals(v4)||p20.equals(v5)||p30.equals(v6)){
System.out.println("1");
f0=false;
}
else {
System.out.println("0");
f0=false;
}
if(f0){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s2<s1){
double ans=s2;
s2=s1;
s1=ans;
}
System.out.println("2 "+s1+" "+s2);
}
}
return;
}
case 5:{
if(c.length!=5){
System.out.println("wrong number of points");
return;
}
Point v1 = new Point(), v2 = new Point(), v3 = new Point(), v4 = new Point(), v5 = new Point();
String[] v = c[0].split(",");
v1.x = Double.parseDouble(v[0]);
v1.y = Double.parseDouble(v[1]);
String[] u = c[1].split(",");
v2.x = Double.parseDouble(u[0]);
v2.y = Double.parseDouble(u[1]);
String[] w = c[2].split(",");
v3.x = Double.parseDouble(w[0]);
v3.y = Double.parseDouble(w[1]);
String[] p = c[3].split(",");
v4.x = Double.parseDouble(p[0]);
v4.y = Double.parseDouble(p[1]);
w = c[4].split(",");
v5.x = Double.parseDouble(w[0]);
v5.y = Double.parseDouble(w[1]);
Quadrangle q = new Quadrangle();
q.v1=v2;q.v2=v3;q.v3=v4;q.v4=v5;
double s1=0,s2=0;
Line l1 = new Line(), l2 = new Line(), l3 = new Line(), l4 = new Line(), l5 = new Line(), l6 = new Line();
l1.a1 = v2;l1.a2 = v3;l2.a1 = v3;l2.a2 = v4;
l3.a1 = v4;l3.a2 = v5;l4.a1 = v5;l4.a2 = v2;
l5.a1 = v2;l5.a2 = v4;l6.a1 = v3;l6.a2 = v5;
l1.slope();l2.slope();l3.slope();l4.slope();l5.slope();l6.slope();
double[] s0=new double[3];
if(q.if_quad()==0){
double[] s3=new double[7];
s3=q.if_triangle();
if(s3[0]==0){
System.out.println("not a quadrilateral or triangle");
return;
}
else {
Point[] p3 = new Point[3];
p3[0] = new Point();p3[1]=new Point();p3[2]=new Point();
p3[0].x=s3[1];p3[0].y=s3[2];
p3[1].x=s3[3];p3[1].y=s3[4];
p3[2].x=s3[5];p3[2].y=s3[6];
Line l3_1=new Line(),l3_2=new Line(),l3_3=new Line();
l3_1.a1=p3[0];l3_1.a2=p3[1];
l3_2.a1=p3[1];l3_2.a2=p3[2];
l3_3.a1=p3[2];l3_3.a2=p3[0];
l3_1.slope();l3_2.slope();l3_3.slope();
s1=Point.mianji(p3[0],p3[1],p3[2]);
s2=Point.mianji(p3[0],p3[1],v1)+Point.mianji(p3[0],p3[2],v1)+Point.mianji(p3[1],p3[2],v1);
if(Math.abs(s1-s2)<0.001){
if(l3_1.gongxian(v1)==0&&Line.xianduanshang(p3[0],p3[1],v1)||l3_3.gongxian(v1)==0&&Line.xianduanshang(p3[0],p3[2],v1)||l3_2.gongxian(v1)==0&&Line.xianduanshang(p3[1],p3[2],v1)){
System.out.println("on the triangle");
}
else if(v1.equals(p3[0])||v1.equals(p3[1])||v1.equals(p3[2])){
System.out.println("on the triangle");
}
else{
System.out.println("in the triangle");
}
return;
}
else {
System.out.println("outof the triangle");
return;
}
}
}
else {
s1 = Point.mianji(v2,v3,v4)+Point.mianji(v2,v4,v5);
s2 = Point.mianji(v1,v2,v3)+Point.mianji(v1,v3,v4)+Point.mianji(v1,v4,v5)+Point.mianji(v1,v5,v2);
if(Math.abs(s1-s2)<0.001){
if(l1.gongxian(v1)==0&&Line.xianduanshang(v2,v3,v1)||l2.gongxian(v1)==0&&Line.xianduanshang(v3,v4,v1)||l3.gongxian(v1)==0&&Line.xianduanshang(v4,v5,v1)||l4.gongxian(v1)==0&&Line.xianduanshang(v5,v2,v1)){
System.out.println("on the quadrilateral");
}
else if(v1.equals(v2)||v1.equals(v3)||v1.equals(v4)||v1.equals(v5)){
System.out.println("on the quadrilateral");
}
else{
System.out.println("in the quadrilateral");
}
return;
}
else {
System.out.println("outof the quadrilateral");
return;
}
}
}
}
}
public static double sishewuru_3(double x){
String str = String.format("%.3f",x);
Double s = Double.parseDouble(str);
return s;
}
}
class Point{
double x,y;
public boolean equals(Point a){
if(x==a.x&&y==a.y){
return true;
}
return false;
}
public static double mianji(Point p1,Point p2,Point p3){
Line l1 = new Line(),l2 = new Line(),l3 = new Line();
l1.a1 = p1;l1.a2 = p2;
l2.a1 = p2;l2.a2 = p3;
l3.a1 = p3;l3.a2 = p1;
l1.length();l2.length();l3.length();
double p = (l1.l+l2.l+l3.l)/2;
return Math.sqrt(p*(p-l1.l)*(p-l2.l)*(p-l3.l));
}
}
class Line{
Point a1 = new Point(),a2 = new Point();
double A,B,C;
double l;
public void slope(){
A = a2.y - a1.y;
B = a1.x - a2.x;
C = a2.x * a1.y - a2.y * a1.x;
}
public static Point xiangl(Point p1,Point p2){
Point p=new Point();
p.x=p1.x-p2.x;
p.y=p1.y-p2.y;
return p;
}
public static boolean chonghe(Line l1,Line l2){
if(l1.a1.equals(l1.a2)){
return false;
}
if(l1.gongxian(l2.a1)==0&&l1.gongxian(l2.a2)==0){
return true;
}
return false;
}
public void length(){
l = Math.sqrt((a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y));
}
public int gongxian(Point x){
if(A*x.x+B*x.y+C==0){
return 0;
}
else if(A*x.x+B*x.y+C<0){
return -1;
}
else{
return 1;
}
}
public double length_p_x(Point x){
return Math.abs((A*x.x+B*x.y+C)/(Math.sqrt(A*A+B*B)));
}
public static Point jiaodian(Line l1,Line l2){
double flag = l1.A*l2.B - l1.B*l2.A;
Point p0 = new Point();
if(flag == 0){
p0.x = 99999.8;
p0.y = 788998.8;
}
else{
p0.x = (l1.B*l2.C-l1.C*l2.B)/flag+0;
p0.y = (l2.A*l1.C-l1.A*l2.C)/flag+0;
}
return p0;
}
public static boolean xianduanshang(Point p1,Point p2,Point p0){
if(p1.equals(p2))
return false;
if((p0.x-p1.x)*(p0.x-p2.x)<0||(p0.y-p1.y)*(p0.y-p2.y)<0){
return true;
}
return false;
}
}
class Quadrangle{
Point v1 = new Point(), v2 = new Point(), v3 = new Point(), v4 = new Point();
public double[] if_triangle(){
double[] s0 = new double[7];
if(v1.equals(v3)){
Line l = new Line();
l.a1 = v1;l.a2 = v2;
l.slope();
if(l.gongxian(v4)==0){
s0[0]=0;
}
else{
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v2.x;s0[4]=v2.y;s0[5]=v4.x;s0[6]=v4.y;
}
}
else if(v2.equals(v4)){
Line l = new Line();
l.a1 = v3;l.a2 = v2;
l.slope();
if(l.gongxian(v1)==0){
s0[0]=0;
}
else{
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v2.x;s0[4]=v2.y;s0[5]=v3.x;s0[6]=v3.y;
}
}
else if(v1.x==v2.x&&v1.y==v2.y){
Line l = new Line();
l.a1 = v1;l.a2 = v3;
l.slope();
if(l.gongxian(v4)==0){
s0[0]=0;
}
else {
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v3.x;s0[4]=v3.y;s0[5]=v4.x;s0[6]=v4.y;
}
}
else if(v2.x==v3.x&&v2.y==v3.y){
Line l = new Line();
l.a1 = v2;l.a2 = v4;
l.slope();
if(l.gongxian(v1)==0){
s0[0]=0;
}
else {
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v3.x;s0[4]=v3.y;s0[5]=v4.x;s0[6]=v4.y;
}
}
else if(v3.x==v4.x&&v3.y==v4.y){
Line l = new Line();
l.a1 = v1;l.a2 = v4;
l.slope();
if(l.gongxian(v2)==0){
s0[0]=0;
}
else {
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v2.x;s0[4]=v2.y;s0[5]=v4.x;s0[6]=v4.y;
}
}
else if(v4.x==v1.x&&v4.y==v1.y){
Line l = new Line();
l.a1 = v2;l.a2 = v4;
l.slope();
if(l.gongxian(v3)==0){
s0[0]=0;
}
else {
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v3.x;s0[4]=v3.y;s0[5]=v2.x;s0[6]=v2.y;
}
}
else {
Line l1 = new Line(), l2 = new Line(), l3 = new Line(), l4 = new Line();
l1.a1 = v1;l1.a2 = v2;l2.a1 = v2;l2.a2 = v3;
l3.a1 = v3;l3.a2 = v4;l4.a1 = v4;l4.a2 = v1;
l1.slope();l2.slope();l3.slope();l4.slope();
if(l1.gongxian(v3)==0&&Line.xianduanshang(v1,v3,v2)&&l1.gongxian(v4)!=0){
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v3.x;s0[4]=v3.y;s0[5]=v4.x;s0[6]=v4.y;
}
else if(l2.gongxian(v4)==0&&Line.xianduanshang(v2,v4,v3)&&l2.gongxian(v1)!=0){
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v4.x;s0[4]=v4.y;s0[5]=v2.x;s0[6]=v2.y;
}
else if(l3.gongxian(v1)==0&&Line.xianduanshang(v3,v1,v4)&&l3.gongxian(v2)!=0){
s0[0]=1;
s0[1]=v1.x;s0[2]=v1.y;s0[3]=v3.x;s0[4]=v3.y;s0[5]=v2.x;s0[6]=v2.y;
}
else if(l4.gongxian(v2)==0&&Line.xianduanshang(v4,v2,v1)&&l4.gongxian(v3)!=0){
s0[0]=1;
s0[1]=v4.x;s0[2]=v4.y;s0[3]=v3.x;s0[4]=v3.y;s0[5]=v2.x;s0[6]=v2.y;
}
else{
s0[0]=0;
}
}
return s0;
}
//0表示不能构成四边形,1表示不能构成平行四边形,2表示能构成平行四边形
public int if_quad(){
Line l1 = new Line(), l2 = new Line(), l3 = new Line(), l4 = new Line();
l1.a1 = v1;l1.a2 = v2;l2.a1 = v2;l2.a2 = v3;
l3.a1 = v3;l3.a2 = v4;l4.a1 = v4;l4.a2 = v1;
l1.slope();l2.slope();l3.slope();l4.slope();
if(l1.gongxian(v3)==0||l1.gongxian(v4)==0||l3.gongxian(v1)==0||l2.gongxian(v4)==0){
return 0;
}
Point p0 = new Point(),p1 = new Point();
double flag1 = l1.A*l3.B - l1.B*l3.A, flag2 = l2.A*l4.B-l2.B*l4.A;
if(flag1==0){
if(flag2==0){
return 2;
}
else{
p1 = Line.jiaodian(l2,l4);
if(Line.xianduanshang(v2,v3,p1)&&Line.xianduanshang(v1,v4,p1)){
return 0;
}
else{
return 1;
}
}
}
else{
p0 = Line.jiaodian(l1,l3);
if(flag2==0){
if(Line.xianduanshang(v1,v2,p0)&&Line.xianduanshang(v3,v4,p0)){
return 0;
}
else{
return 1;
}
}
else{
p1 = Line.jiaodian(l2,l4);
if(Line.xianduanshang(v1,v2,p0)&&Line.xianduanshang(v3,v4,p0)){
return 0;
}
else{
if(Line.xianduanshang(v2,v3,p1)&&Line.xianduanshang(v1,v4,p1)){
return 0;
}
else{
return 1;
}
}
}
}
}
//0代表啥也不是,1代表菱形,2代表长方形,3代表正方形
public int lin_chang_zheng(){
Line l1 = new Line(), l2 = new Line(), l3 = new Line(), l4 = new Line();
l1.a1 = v1;l1.a2 = v2;l2.a1 = v2;l2.a2 = v3;
l3.a1 = v3;l3.a2 = v4;l4.a1 = v4;l4.a2 = v1;
l1.slope();l2.slope();l3.slope();l4.slope();
l1.length();l2.length();l3.length();l4.length();
double h = l1.length_p_x(v3);
if(l1.l==l2.l&&l2.l==l3.l&&l3.l==l4.l){
if(h==l2.l){
return 3;
}
else {
return 1;
}
}
if(l1.l==l3.l&&l2.l==l4.l){
if(h==l2.l){
return 2;
}
}
return 0;
}
public static double[] jiequ_mianji(Line l1,Line l2,Line l3,Line l0){
double[] s0 = new double[3];
Point p1=new Point(),p2=new Point(),p3=new Point();
p1=Line.jiaodian(l1,l0);
p2=Line.jiaodian(l2,l0);
p3=Line.jiaodian(l3,l0);
double S = Point.mianji(l1.a1,l1.a2,l2.a2);
double s1=0,s2=0;
if(p1.x==p2.x&&p1.y==p2.y&&p1.x!=99999.8){
s1 = Point.mianji(p1,p3,l1.a1);
s0[0]=2;
}
else if(p2.x==p3.x&&p2.y==p3.y&&p2.x!=99999.8){
s1 = Point.mianji(p1,p2,l1.a1);
s0[0]=2;
}
else if(p3.x==p1.x&&p3.y==p1.y&&p1.x!=99999.8){
s1 = Point.mianji(p1,p2,l1.a2);
s0[0]=2;
}
else {
if(Line.xianduanshang(l1.a1,l1.a2,p1)){
if(Line.xianduanshang(l2.a1,l2.a2,p2)){
s1=Point.mianji(p1,p2,l1.a2);
s0[0]=2;
}
else if(Line.xianduanshang(l3.a1,l3.a2,p3)){
s1=Point.mianji(p1,p3,l1.a1);
s0[0]=2;
}
else{
s0[1]=1;
}
}
else{
if(Line.xianduanshang(l3.a1,l3.a2,p3)){
if(Line.xianduanshang(l2.a1,l2.a2,p2)){
s1=Point.mianji(p2,p3,l3.a1);
s0[0]=2;
}
else{
s0[0]=1;
}
}
else {
s0[0]=0;
}
}
}
s2 = S-s1;
if(s0[0]==2){
if(s2<=0||s1<=0){
s0[0]=1;
}
}
String str;
str = String.format("%.3f",s1);
s1 = Double.parseDouble(str);
str = String.format("%.3f",s2);
s2 = Double.parseDouble(str);
if(s1<s2){
s0[1]=s1;
s0[2]=s2;
}
else{
s0[1]=s2;
s0[2]=s1;
}
return s0;
}
}
分析:判断能否构成四边形,我先判断是否存在三点共线,再判断不相邻的对边的交点是否再两条线段上,有且仅当这两个条件满足时,才可以构成四边形;计算四边形的面积时将四边形分成三角形计算,取四个面积的最小值当作改四边形的面积;判断不能构成四边形的四个点能否构成三角形,先将冗余点去除,如果得到三个点在判断三点是否共线,如果不共线即可构成三角形;切割四边形时,先计算直线与四边形每条边的交点,再判断交点在四边形的哪一条边,将切割的区域分割成三角形计算面积;判断点与四边形的关系,以该点依次与四边形的每一条边构成三角形计算面积,如果面积之和大于四边形的面积,即可说明点在外面,如果相等,还需判断点是否在线段上,如果不在线段上,即可说明点在内部。
类图如下:
SourceMonitor报表图如下:
2.第五次大作业
(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位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
代码如下(点击展开)
package no5;
import java.util.Scanner;
import java.util.ArrayList;
//点类
class Point{
double x,y;
//判断点是否重合
public static boolean equals_point(Point p1,Point p2){
if(p1.x==p2.x&&p1.y==p2.y){
return true;
}
return false;
}
}
//线类
class Line{
Point p1=new Point(),p2=new Point();
double a,b,c;
public Line(Point a1,Point a2){
this.p1=a1;
this.p2=a2;
this.a=p2.y-p1.y;
this.b=p1.x-p2.x;
this.c=p2.x*p1.y-p2.y*p1.x;
}
//判断点是否在线段上(不包含端点)
public boolean on_line1(Point p){
if(p.x==99999.8&&p.y==788998.8){
return false;
}
if(Math.abs(a*p.x+b*p.y+c)<0.001){
if((p.x-p1.x)*(p.x-p2.x)<0||(p.y-p1.y)*(p.y-p2.y)<0){
return true;
}
}
return false;
}
//判断点在线段上(包含端点)
public boolean on_line(Point p){
if(p.x==99999.8&&p.y==788998.8){
return false;
}
if(Math.abs(a*p.x+b*p.y+c)<0.001){
if((p.x-p1.x)*(p.x-p2.x)<=0&&(p.y-p1.y)*(p.y-p2.y)<=0){
return true;
}
}
return false;
}
//求两直线的交点坐标
public static Point jiaodian(Line l1,Line l2){
double flag = l1.a*l2.b - l1.b*l2.a;
Point p0 = new Point();
if(flag == 0){
p0.x = 99999.8;
p0.y = 788998.8;
}
else{
p0.x = (l1.b*l2.c-l1.c*l2.b)/flag+0;
p0.y = (l2.a*l1.c-l1.a*l2.c)/flag+0;
}
return p0;
}
//判断两直线是否重合
public boolean chonghe(Line l){
if(l.p1.x*a+l.p1.y*b+c==0&&l.p2.x*a+l.p2.y*b+c==0){
return true;
}
return false;
}
//判断点是否与直线共线
public boolean gongxian(Point p){
if(Math.abs(p.x*a+p.y*b+c)<0.001){
return true;
}
return false;
}
}
class polygon{
ArrayList<Point> list=new ArrayList<>();
double ares,round;
public polygon(){
ares=0;
round=0;
}
//去除冗余点
public ArrayList<Point> get_point(){
ArrayList<Point> list_re=new ArrayList<>();
list_re.add(list.get(0));
for(int i=1;i<list.size();i++){
boolean f=true;
for(int j=0;j<list_re.size();j++){//判断是否重合
if(list.get(i).equals(list_re.get(j))){
f=false;
break;
}
}
Line l=new Line(list.get((i-1)%list.size()),list.get((i+1)%list.size()));
//判断是否在线段上
if(!l.on_line(list.get(i))&&f){
list_re.add(list.get(i));
}
}
Line l=new Line(list_re.get(list_re.size()-1),list_re.get(1));
//特判第一个点是否在线段52上
if(l.on_line(list_re.get(0))){
list_re.remove(0);
}
return list_re;
}
// public ArrayList<Point> get_point(){
// ArrayList<Point> list1=new ArrayList<>();
// ArrayList<Point> list2=new ArrayList<>();
// list1.add(list.get(0));
// for(int i=1;i<list.size();i++){
// boolean f=true;
// for(int j=0;j<list1.size();j++){
// if(Point.equals_point(list1.get(j),list.get(i))){
// f=false;
// }
// }
// if(f) list1.add(list.get(i));
// }
// for(int i=0;i<list1.size();i++){
// Line l=new Line(list1.get(i-1>=0?i-1:list1.size()-1),list1.get((i+1)%list1.size()));
// if(!l.on_line(list1.get(i))){
// list2.add(list1.get(i));
// }
// }
// return list2;
// }
//凹多边形返回false,凸多边形返回true
public boolean IS_ao_tu(){
if(list.size()<4){
return false;
}
int f=0;
Point p1=new Point(),p2=new Point(),p3=new Point();
p1=list.get(0);
p2=list.get(1);
p3=list.get(2);
f=chaji(p1,p2,p3);
for(int i=1;i<list.size()-2;i++){
p1=list.get(i);
p2=list.get(i+1);
p3=list.get(i+2);
if(chaji(p1,p2,p3)!=f){
return false;
}
}
return true;
}
//求出多边形的面积
public void get_square(){
if(list.size()<3){
return;
}
Point p0=new Point();
Point p1=new Point(),p2=new Point();
p0=list.get(0);
for(int i=1;i<list.size()-1;i++){
p1=list.get(i);
p2=list.get(i+1);
ares+=get_mianji(p0,p1,p2);
}
}
//求出多边形的周长
public void get_round(){
for(int i=0;i<list.size();i++){
round+=get_juli(list.get(i),list.get((i+1)%list.size()));
}
}
//求三角形的面积
public static double get_mianji(Point p0,Point p1,Point p2){
double l1,l2,l3;
l1=get_juli(p0,p1);
l2=get_juli(p1,p2);
l3=get_juli(p2,p0);
double l=l1+l2+l3;
l=l/2;
return Math.sqrt(l*(l-l1)*(l-l2)*(l-l3));
}
//求两点之间的距离
public static double get_juli(Point p1,Point p2){
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
//求三点的叉积
public static int chaji(Point p1,Point p2,Point p3){
double a1,b1,a2,b2;
a1=p2.x-p1.x;
b1=p2.y-p1.y;
a2=p3.x-p2.x;
b2=p3.y-p2.y;
if(Math.abs(a1*b2-b1*a2)<0.0001){
return 0;
}
else if(a1*b2-b1*a2<0){
return -1;
}
else{
return 1;
}
}
//格式化四舍五入保留三位
public static double sishewuru_3(double x){
String str = String.format("%.3f",x);
Double s = Double.parseDouble(str);
return s;
}
}
//五边形类继承多边形
class Pentagon extends polygon{
public Pentagon(ArrayList<Point> list){
this.list=list;
}
//判断能否构成五边形
public boolean is_pentagon(){
ArrayList<Line> list_l=new ArrayList<>();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).gongxian(list.get((i+2)%list.size()))){
return false;
}
}
for(int i=0;i<list.size();i++){
Line l1=new Line(list_l.get(i).p1,list_l.get(i).p2);
Line l2=new Line(list_l.get((i+2)%list_l.size()).p1,list_l.get((i+2)%list_l.size()).p2);
Point jiaod=Line.jiaodian(l1,l2);
if(l1.on_line1(jiaod)&&l2.on_line1(jiaod)){
return false;
}
}
return true;
}
//计算直线与五边形的截取面积
public void jie_ares(Line l){
ArrayList<Line> list_l=new ArrayList<>();
ArrayList<Point> list_p=new ArrayList<>();
this.get_square();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).chonghe(l)){
System.out.println("The line is coincide with one of the lines");
return;
}
}
for(int i=0;i<list_l.size();i++){
list_p.add(Line.jiaodian(list_l.get(i),l));
}
double s1,s2;
boolean f=false;
s1=0;s2=0;
if(!f){
//直线过相邻两条边
for(int i=0;i<list_p.size();i++){
if(!Point.equals_point(list_p.get(i),list_p.get((i+1)%list_p.size()))){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+1)%list_l.size()).on_line(list_p.get((i+1)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list_p.get((i+1)%list_p.size()),list.get((i+1)%list.size()));
s2=this.ares-s1;
break;
}
}
}
}
if(!f){
//直线过对边
for(int i=0;i<list_p.size();i++){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+2)%list_l.size()).on_line(list_p.get((i+2)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list.get((i+1)%list.size()))+polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list_p.get((i+2)%list_p.size()));
s2=this.ares-s1;
break;
}
}
}
if(!f){
//当直线与五边形没有两个交点时,判断是否有一个交点
for(int i=0;i<list_p.size();i++){
if(Point.equals_point(list_p.get(i),list.get(i))){
System.out.println("1");
return;
}
}
System.out.println(("0"));
return;
}
//输出面积
if(f){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s1>s2){
double temp=s2;
s2=s1;
s1=temp;
}
System.out.println("2 "+s1+" "+s2);
}
return;
}
}
//四边形类继承多边形
class Quadrangle extends polygon{
public Quadrangle(ArrayList<Point> list){
this.list=list;
}
//判断是否构成四边形
public boolean is_quadrangle(){
for(int i=0;i<list.size();i++){
Line l1=new Line(list.get(i),list.get((i+1)%list.size()));
Line l2=new Line(list.get((i+2)%list.size()),list.get((i+3)%list.size()));
Point jiaod=Line.jiaodian(l1,l2);
if(l1.on_line(jiaod)&&l2.on_line(jiaod)){
return false;
}
}
return true;
}
//直线截取四边形的面积
public void jie_ares(Line l){
ArrayList<Line> list_l=new ArrayList<>();
ArrayList<Point> list_p=new ArrayList<>();
this.get_square();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).chonghe(l)){
System.out.println("The line is coincide with one of the lines");
return;
}
}
for(int i=0;i<list_l.size();i++){
list_p.add(Line.jiaodian(list_l.get(i),l));
}
double s1,s2;
boolean f=false;
s1=0;s2=0;
if(!f){
//直线过相邻两条边
for(int i=0;i<list_p.size();i++){
if(!Point.equals_point(list_p.get(i),list_p.get((i+1)%list_p.size()))){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+1)%list_l.size()).on_line(list_p.get((i+1)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list_p.get((i+1)%list_p.size()),list.get((i+1)%list.size()));
s2=this.ares-s1;
break;
}
}
}
}
if(!f){
//直线过对边
for(int i=0;i<list_p.size();i++){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+2)%list_l.size()).on_line(list_p.get((i+2)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list.get((i+1)%list.size()))+polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list_p.get((i+2)%list_p.size()));
s2=this.ares-s1;
break;
}
}
}
if(!f){
//直线与四边形没有两个交点,判断是否有一个交点
for(int i=0;i<list_p.size();i++){
if(Point.equals_point(list_p.get(i),list.get(i))){
System.out.println("1");
return;
}
}
System.out.println(("0"));
return;
}
//输出面积
if(f){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s1>s2){
double temp=s2;
s2=s1;
s1=temp;
}
System.out.println("2 "+s1+" "+s2);
}
return;
}
}
//三角形类继承多边形
class triangle extends polygon{
public triangle(ArrayList<Point> list){
this.list=list;
}
//判断是否构成三角形
public boolean if_triangle(){
Line l=new Line(list.get(0),list.get(1));
Point p=list.get(2);
if(p.x*l.a+p.y*l.b+l.c==0){
return false;
}
return true;
}
//直线截取三角形的面积
public void jie_ares(Line l){
ArrayList<Line> list_l=new ArrayList<>();
ArrayList<Point> list_p=new ArrayList<>();
this.get_square();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).chonghe(l)){
System.out.println("The line is coincide with one of the lines");
return;
}
}
for(int i=0;i<list_l.size();i++){
list_p.add(Line.jiaodian(list_l.get(i),l));
}
double s1,s2;
boolean f=false;
s1=0;s2=0;
if(!f){
//直线过相邻两条边
for(int i=0;i<list_p.size();i++){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+1)%list_l.size()).on_line(list_p.get((i+1)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list_p.get((i+1)%list_p.size()),list.get((i+1)%list.size()));
s2=this.ares-s1;
break;
}
}
}
if(!f){
//直线与三角形没有两个交点,判断是否有一个交点
for(int i=0;i<list_p.size();i++){
if(Point.equals_point(list_p.get(i),list.get(i))){
System.out.println("1");
return;
}
}
System.out.println(("0"));
return;
}
//输出面积
if(f){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s1>s2){
double temp=s2;
s2=s1;
s1=temp;
}
System.out.println("2 "+s1+" "+s2);
}
return;
}
}
public class Main01 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
if(s.length()<=2){
System.out.println("Wrong Format");
return;
}
int x = (int)s.charAt(0)-'0';
if(s.charAt(1)!=':'){
System.out.println("Wrong Format");
return;
}
String[] c = s.substring(2).split(" ");
for(int i=0;i<c.length;i++){
String[] m = c[i].split(",");
if(m.length!=2){
System.out.println("Wrong Format");
return;
}
for(int j=0;j<m.length;j++){
if(!m[j].matches("^[+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?)")){
System.out.println("Wrong Format");
return;
}
}
}
if(!(x>=1&&x<=3)){
System.out.println("Wrong Format");
return;
}
switch(x){
case 1:{
if(c.length!=5){
System.out.println("wrong number of points");
return;
}
polygon poly=new polygon();
for(int i=0;i<c.length;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly.list.add(p);
}
ArrayList<Point> list=new ArrayList<>();
list=poly.get_point();
if(list.size()==5){
Pentagon pen=new Pentagon(list);
if(pen.is_pentagon()){
System.out.println("true");
return;
}
}
System.out.println("false");
return;
}
case 2:{
if(c.length!=5){
System.out.println("wrong number of points");
return;
}
polygon poly=new polygon();
for(int i=0;i<c.length;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly.list.add(p);
}
ArrayList<Point> list=new ArrayList<>();
list=poly.get_point();
if(list.size()==5){
Pentagon pen=new Pentagon(list);
if(pen.is_pentagon()){
if(pen.IS_ao_tu()){
pen.get_square();
pen.get_round();
System.out.println("true "+sishewuru_3(pen.round)+" "+sishewuru_3(pen.ares));
return;
}
System.out.println("false");
return;
}
}
System.out.println("not a pentagon");
return;
}
case 3:{
if(c.length!=7){
System.out.println("wrong number of points");
return;
}
Point p1=new Point(),p2=new Point();
String[] u = c[0].split(",");
p1.x = Double.parseDouble(u[0]);
p1.y = Double.parseDouble(u[1]);
u = c[1].split(",");
p2.x = Double.parseDouble(u[0]);
p2.y = Double.parseDouble(u[1]);
Line l=new Line(p1,p2);
polygon poly=new polygon();
for(int i=2;i<c.length;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly.list.add(p);
}
ArrayList<Point> list=new ArrayList<>();
list=poly.get_point();
//五边形
if(list.size()==5){
Pentagon pen=new Pentagon(list);
if(pen.is_pentagon()){
pen.jie_ares(l);
}
else {
System.out.println("not a polygon");
}
return;
}
//四边形
else if(list.size()==4){
Quadrangle qua=new Quadrangle(list);
if(qua.is_quadrangle()){
qua.jie_ares(l);
}
else {
System.out.println("not a polygon");
}
return;
}
//三角形
else if(list.size()==3){
triangle tri=new triangle(list);
if(tri.if_triangle()){
tri.jie_ares(l);
}
else {
System.out.println("not a polygon");
}
return;
}
else{
System.out.println("not a polygon");
return;
}
}
}
}
//输出点的坐标
public static void out_put(polygon poly){
for(int i=0;i<poly.list.size();i++){
System.out.println((poly.list.get(i).x+","+poly.list.get(i).y));
}
}
//格式化数据
public static double sishewuru_3(double x){
String str = String.format("%.3f",x);
Double s = Double.parseDouble(str);
return s;
}
}
分析:判断输入的五点能否构成五边形,先将冗余点去除,如果剩余的点的个数为5,再用循环依次判断不相邻边的交点是否在线段上,如果都不在线段上即可构成五边形;判断五边形是凹五边形还是凸五边形,使用叉积,如果相邻两点的叉积都同号,即可说明为凸五边形,否则为凹五边形;计算直线与五边形的交点个数,先计算直线与每一条边的交点,再使用循环依次判断交点在相邻边还得对边,如果没有两个交点再判断与五边形是否存在交点,根据交点个数输出结果。
类图如下:
SourceMonitor报表图如下:
(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
代码如下(点击展开)
package no52;
import java.util.Scanner;
import java.util.ArrayList;
//点类
class Point{
double x,y;
//判断点是否重合
public static boolean equals_point(Point p1,Point p2){
if(p1.x==p2.x&&p1.y==p2.y){
return true;
}
return false;
}
}
//线类
class Line{
Point p1=new Point(),p2=new Point();
double a,b,c;
public Line(Point a1,Point a2){
this.p1=a1;
this.p2=a2;
this.a=p2.y-p1.y;
this.b=p1.x-p2.x;
this.c=p2.x*p1.y-p2.y*p1.x;
}
//判断点是否在线段上(不包含端点)
public boolean on_line1(Point p){
if(p.x==99999.8&&p.y==788998.8){
return false;
}
if(Point.equals_point(p1,p)||Point.equals_point(p2,p)){
return true;
}
if(Math.abs(a*p.x+b*p.y+c)<0.001){
if((p.x-p1.x)*(p.x-p2.x)<0||(p.y-p1.y)*(p.y-p2.y)<0){
return true;
}
}
return false;
}
//判断点在线段上(包含端点)
public boolean on_line(Point p){
if(p.x==99999.8&&p.y==788998.8){
return false;
}
if(Math.abs(a*p.x+b*p.y+c)<0.001){
if((p.x-p1.x)*(p.x-p2.x)<=0&&(p.y-p1.y)*(p.y-p2.y)<=0){
return true;
}
}
return false;
}
//求两直线的交点坐标
public static Point jiaodian(Line l1,Line l2){
double flag = l1.a*l2.b - l1.b*l2.a;
Point p0 = new Point();
if(flag == 0){
p0.x = 99999.8;
p0.y = 788998.8;
}
else{
p0.x = (l1.b*l2.c-l1.c*l2.b)/flag+0;
p0.y = (l2.a*l1.c-l1.a*l2.c)/flag+0;
}
return p0;
}
//判断两直线是否重合
public boolean chonghe(Line l){
if(l.p1.x*a+l.p1.y*b+c==0&&l.p2.x*a+l.p2.y*b+c==0){
return true;
}
return false;
}
//判断点是否与直线共线
public boolean gongxian(Point p){
if(Math.abs(p.x*a+p.y*b+c)<0.001){
return true;
}
return false;
}
}
class polygon{
ArrayList<Point> list=new ArrayList<>();
double ares,round;
public polygon(){
ares=0;
round=0;
}
//去除冗余点
public ArrayList<Point> get_point(){
ArrayList<Point> list1=new ArrayList<>();
ArrayList<Point> list2=new ArrayList<>();
list1.add(list.get(0));
for(int i=1;i<list.size();i++){
boolean f=true;
for(int j=0;j<list1.size();j++){
if(Point.equals_point(list1.get(j),list.get(i))){
f=false;
}
}
if(f) list1.add(list.get(i));
}
for(int i=0;i<list1.size();i++){
Line l=new Line(list1.get(i-1>=0?i-1:list1.size()-1),list1.get((i+1)%list1.size()));
if(!l.on_line(list1.get(i))){
list2.add(list1.get(i));
}
}
return list2;
}
//凹多边形返回false,凸多边形返回true
public boolean IS_ao_tu(){
if(list.size()<4){
return false;
}
int f=0;
Point p1=new Point(),p2=new Point(),p3=new Point();
p1=list.get(0);
p2=list.get(1);
p3=list.get(2);
f=chaji(p1,p2,p3);
for(int i=1;i<list.size()-2;i++){
p1=list.get(i);
p2=list.get(i+1);
p3=list.get(i+2);
if(chaji(p1,p2,p3)!=f){
return false;
}
}
return true;
}
//求某一多边形在另一多边形内部的点
public ArrayList<Point> poly_point(polygon ploy){
ArrayList<Point> list_point=new ArrayList<>();
for(int i=0;i<ploy.list.size();i++){
if(where_point(ploy.list.get(i))){
list_point.add(ploy.list.get(i));
}
}
return list_point;
}
//判断某一点是否在多边形内部
public boolean where_point(Point p){
get_square();
double s=0;
for(int i=0;i<list.size();i++){
s+=get_mianji(p,list.get(i),list.get((i+1)%list.size()));
}
if(Math.abs(s-ares)<=0.001){
return true;
}
else{
return false;
}
}
//判断点是否在下线段上
public boolean where_point1(Point p){
ArrayList<Line> list_l=new ArrayList<>();
for(int i=0;i<list.size();i++){
Line l=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).on_line1(p)){
return true;
}
}
return false;
}
//求出两个多边形的交点坐标集
public ArrayList<Point> crossover_point(polygon poly){
ArrayList<Point> list_point=new ArrayList<>();
ArrayList<Line> list_line1=new ArrayList<>();
ArrayList<Line> list_line2=new ArrayList<>();
for(int i=0;i<this.list.size();i++){
Line l=new Line(this.list.get(i),this.list.get((i+1)%this.list.size()));
list_line1.add(l);
}
for(int i=0;i<poly.list.size();i++){
Line l=new Line(poly.list.get(i),poly.list.get((i+1)%poly.list.size()));
list_line2.add(l);
}
for(int i=0;i<list_line1.size();i++){
Line l1=new Line(list_line1.get(i).p1,list_line1.get(i).p2);
for(int j=0;j<list_line2.size();j++){
Line l2=new Line(list_line2.get(j).p1,list_line2.get(j).p2);
Point jiaod=Line.jiaodian(l1,l2);
if(l1.on_line1(jiaod)&&l2.on_line1(jiaod)){
boolean f=true;
for(int k=0;k<list_point.size();k++){
if(Point.equals_point(list_point.get(k),jiaod)){
f=false;
break;
}
}
if(f){
list_point.add(jiaod);
}
}
}
}
return list_point;
}
//求出多边形的面积
public void get_square(){
if(list.size()<3){
return;
}
ares=0;
Point p0=new Point();
Point p1=new Point(),p2=new Point();
p0=list.get(0);
for(int i=1;i<list.size()-1;i++){
p1=list.get(i);
p2=list.get(i+1);
ares+=get_mianji(p0,p1,p2);
}
}
//求出多边形的周长
public void get_round(){
round=0;
for(int i=0;i<list.size();i++){
round+=get_juli(list.get(i),list.get((i+1)%list.size()));
}
}
//对多边形的点进行排序
public static ArrayList<Point> sort(ArrayList<Point> list_point){
if(list_point.size()>0){
Point cen=new Point();
double cx,cy;
cx=0;cy=0;
for(int i=0;i<list_point.size();i++){
cx+=list_point.get(i).x;
cy+=list_point.get(i).y;
}
cen.x=(cx/list_point.size());
cen.y=(cy/list_point.size());
for(int i=1;i<list_point.size();i++){
for(int j=0;j<list_point.size()-i;j++){
if(chaji(list_point.get(j),list_point.get(j+1),cen)>0){
Point p=new Point();
p.x=list_point.get(j).x;
p.y=list_point.get(j).y;
list_point.get(j).x=list_point.get(j+1).x;
list_point.get(j).y=list_point.get(j+1).y;
list_point.get(j+1).x=p.x;
list_point.get(j+1).y=p.y;
}
}
}
}
return list_point;
}
//求两个多边形的重合点
public static double[] union_poly(polygon poly1,polygon poly2){
ArrayList<Point> list_point1=new ArrayList<>();
ArrayList<Point> list_point2=new ArrayList<>();
ArrayList<Point> list_point3=new ArrayList<>();
ArrayList<Point> list_point4=new ArrayList<>();
ArrayList<Point> list_point=new ArrayList<>();
list_point1=poly1.poly_point(poly2);
list_point2=poly2.poly_point(poly1);
list_point3=poly1.crossover_point(poly2);
list_point4=polygon.union_point(list_point1,list_point2);
list_point=polygon.union_point(list_point4,list_point3);
double[] res=new double[3];
res[0]=list_point1.size();
res[1]=list_point2.size();
res[2]=list_point.size();
return res;
}
//两个点集的叠加
public static ArrayList<Point> union_point(ArrayList<Point> list1,ArrayList<Point> list2){
ArrayList<Point> list_point=new ArrayList<>();
for(int i=0;i<list1.size();i++){
boolean f=true;
for(int j=0;j<list_point.size();j++){
if(Point.equals_point(list_point.get(j),list1.get(i))){
f=false;
break;
}
}
if(f)
list_point.add(list1.get(i));
}
for(int i=0;i<list2.size();i++){
boolean f=false;
for(int j=0;j<list_point.size();j++){
if(Point.equals_point(list2.get(i),list_point.get(j))){
f=true;
break;
}
}
if(!f){
list_point.add(list2.get(i));
}
}
return list_point;
}
//求三角形的面积
public static double get_mianji(Point p0,Point p1,Point p2){
double l1,l2,l3;
l1=get_juli(p0,p1);
l2=get_juli(p1,p2);
l3=get_juli(p2,p0);
double l=l1+l2+l3;
l=l/2;
return Math.sqrt(l*(l-l1)*(l-l2)*(l-l3));
}
//求两点之间的距离
public static double get_juli(Point p1,Point p2){
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
//求三点的叉积
public static int chaji(Point p1,Point p2,Point p3){
double a1,b1,a2,b2;
a1=p2.x-p1.x;
b1=p2.y-p1.y;
a2=p3.x-p2.x;
b2=p3.y-p2.y;
if(Math.abs(a1*b2-b1*a2)<0.0001){
return 0;
}
else if(a1*b2-b1*a2<0){
return -1;
}
else{
return 1;
}
}
//格式化四舍五入保留三位
public static double sishewuru_3(double x){
String str = String.format("%.3f",x);
Double s = Double.parseDouble(str);
return s;
}
}
//五边形类继承多边形
class Pentagon extends polygon{
public Pentagon(ArrayList<Point> list){
this.list=list;
}
//判断能否构成五边形
public boolean is_pentagon(){
ArrayList<Line> list_l=new ArrayList<>();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).gongxian(list.get((i+2)%list.size()))){
return false;
}
}
for(int i=0;i<list.size();i++){
Line l1=new Line(list_l.get(i).p1,list_l.get(i).p2);
Line l2=new Line(list_l.get((i+2)%list_l.size()).p1,list_l.get((i+2)%list_l.size()).p2);
Point jiaod=Line.jiaodian(l1,l2);
if(l1.on_line1(jiaod)&&l2.on_line1(jiaod)){
return false;
}
}
return true;
}
//计算直线与五边形的截取面积
public void jie_ares(Line l){
ArrayList<Line> list_l=new ArrayList<>();
ArrayList<Point> list_p=new ArrayList<>();
this.get_square();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).chonghe(l)){
System.out.println("The line is coincide with one of the lines");
return;
}
}
for(int i=0;i<list_l.size();i++){
list_p.add(Line.jiaodian(list_l.get(i),l));
}
double s1,s2;
boolean f=false;
s1=0;s2=0;
if(!f){
//直线过相邻两条边
for(int i=0;i<list_p.size();i++){
if(!Point.equals_point(list_p.get(i),list_p.get((i+1)%list_p.size()))){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+1)%list_l.size()).on_line(list_p.get((i+1)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list_p.get((i+1)%list_p.size()),list.get((i+1)%list.size()));
s2=this.ares-s1;
break;
}
}
}
}
if(!f){
//直线过对边
for(int i=0;i<list_p.size();i++){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+2)%list_l.size()).on_line(list_p.get((i+2)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list.get((i+1)%list.size()))+polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list_p.get((i+2)%list_p.size()));
s2=this.ares-s1;
break;
}
}
}
if(!f){
//当直线与五边形没有两个交点时,判断是否有一个交点
for(int i=0;i<list_p.size();i++){
if(Point.equals_point(list_p.get(i),list.get(i))){
System.out.println("1");
return;
}
}
System.out.println(("0"));
return;
}
//输出面积
if(f){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s1>s2){
double temp=s2;
s2=s1;
s1=temp;
}
System.out.println("2 "+s1+" "+s2);
}
return;
}
}
//四边形类继承多边形
class Quadrangle extends polygon{
public Quadrangle(ArrayList<Point> list){
this.list=list;
}
//判断是否构成四边形
public boolean is_quadrangle(){
for(int i=0;i<list.size();i++){
Line l1=new Line(list.get(i),list.get((i+1)%list.size()));
Line l2=new Line(list.get((i+2)%list.size()),list.get((i+3)%list.size()));
Point jiaod=Line.jiaodian(l1,l2);
if(l1.on_line(jiaod)&&l2.on_line(jiaod)){
return false;
}
}
return true;
}
//直线截取四边形的面积
public void jie_ares(Line l){
ArrayList<Line> list_l=new ArrayList<>();
ArrayList<Point> list_p=new ArrayList<>();
this.get_square();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).chonghe(l)){
System.out.println("The line is coincide with one of the lines");
return;
}
}
for(int i=0;i<list_l.size();i++){
list_p.add(Line.jiaodian(list_l.get(i),l));
}
double s1,s2;
boolean f=false;
s1=0;s2=0;
if(!f){
//直线过相邻两条边
for(int i=0;i<list_p.size();i++){
if(!Point.equals_point(list_p.get(i),list_p.get((i+1)%list_p.size()))){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+1)%list_l.size()).on_line(list_p.get((i+1)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list_p.get((i+1)%list_p.size()),list.get((i+1)%list.size()));
s2=this.ares-s1;
break;
}
}
}
}
if(!f){
//直线过对边
for(int i=0;i<list_p.size();i++){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+2)%list_l.size()).on_line(list_p.get((i+2)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list.get((i+1)%list.size()))+polygon.get_mianji(list_p.get(i),list.get((i+2)%list.size()),list_p.get((i+2)%list_p.size()));
s2=this.ares-s1;
break;
}
}
}
if(!f){
//直线与四边形没有两个交点,判断是否有一个交点
for(int i=0;i<list_p.size();i++){
if(Point.equals_point(list_p.get(i),list.get(i))){
System.out.println("1");
return;
}
}
System.out.println(("0"));
return;
}
//输出面积
if(f){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s1>s2){
double temp=s2;
s2=s1;
s1=temp;
}
System.out.println("2 "+s1+" "+s2);
}
return;
}
}
//三角形类继承多边形
class triangle extends polygon{
public triangle(ArrayList<Point> list){
this.list=list;
}
//判断是否构成三角形
public boolean if_triangle(){
Line l=new Line(list.get(0),list.get(1));
Point p=list.get(2);
if(p.x*l.a+p.y*l.b+l.c==0){
return false;
}
return true;
}
//直线截取三角形的面积
public void jie_ares(Line l){
ArrayList<Line> list_l=new ArrayList<>();
ArrayList<Point> list_p=new ArrayList<>();
this.get_square();
for(int i=0;i<list.size();i++){
Line l_new=new Line(list.get(i),list.get((i+1)%list.size()));
list_l.add(l_new);
}
for(int i=0;i<list_l.size();i++){
if(list_l.get(i).chonghe(l)){
System.out.println("The line is coincide with one of the lines");
return;
}
}
for(int i=0;i<list_l.size();i++){
list_p.add(Line.jiaodian(list_l.get(i),l));
}
double s1,s2;
boolean f=false;
s1=0;s2=0;
if(!f){
//直线过相邻两条边
for(int i=0;i<list_p.size();i++){
if(list_l.get(i).on_line(list_p.get(i))&&list_l.get((i+1)%list_l.size()).on_line(list_p.get((i+1)%list_p.size()))){
f=true;
s1=polygon.get_mianji(list_p.get(i),list_p.get((i+1)%list_p.size()),list.get((i+1)%list.size()));
s2=this.ares-s1;
break;
}
}
}
if(!f){
//直线与三角形没有两个交点,判断是否有一个交点
for(int i=0;i<list_p.size();i++){
if(Point.equals_point(list_p.get(i),list.get(i))){
System.out.println("1");
return;
}
}
System.out.println(("0"));
return;
}
//输出面积
if(f){
s1=sishewuru_3(s1);
s2=sishewuru_3(s2);
if(s1>s2){
double temp=s2;
s2=s1;
s1=temp;
}
System.out.println("2 "+s1+" "+s2);
}
return;
}
}
class solve{
public void solve_matter(int x,String[] c){
switch(x){
case 4:{
if(c.length!=10){
System.out.println("wrong number of points");
return;
}
polygon poly1=new polygon();
polygon poly2=new polygon();
for(int i=0;i<c.length/2;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly1.list.add(p);
}
for(int i=c.length/2;i<c.length;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly2.list.add(p);
}
ArrayList<Point> list1=new ArrayList<>();
ArrayList<Point> list2=new ArrayList<>();
list1=poly1.get_point();
list2=poly2.get_point();
if(list1.size()==5){
Pentagon polygon1=new Pentagon(list1);
if(polygon1.is_pentagon()){
if(list2.size()==5){
Pentagon polygon2=new Pentagon(list2);
if(polygon2.is_pentagon()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(2,2,1);
}
else if(res[2]==1||res[2]==2){
out_put(2,2,2);
}
else if(res[0]==5&&res[1]==5){
out_put(2,2,3);
}
else if(res[0]==5&&res[1]!=0){
out_put(2,2,6);
}
else if(res[0]!=0&&res[1]==5){
out_put(2,2,4);
}
else {
out_put(2,2,5);
}
}
}
else if(list2.size()==4){
Quadrangle polygon2=new Quadrangle(list2);
if(polygon2.is_quadrangle()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(2,1,1);
}
else if(res[2]==1||res[2]==2){
out_put(2,1,2);
}
else if(res[0]==5&&res[1]==4){
out_put(2,1,3);
}
else if(res[0]==5&&res[1]!=0){
out_put(2,1,6);
}
else if(res[0]!=0&&res[1]==4){
out_put(2,1,4);
}
else {
out_put(2,1,5);
}
}
}
else if(list2.size()==3){
triangle polygon2=new triangle(list2);
if(polygon2.if_triangle()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(2,0,1);
}
else if(res[2]==1||res[2]==2){
out_put(2,0,2);
}
else if(res[0]==5&&res[1]==3){
out_put(2,0,3);
}
else if(res[0]==5&&res[1]!=0){
out_put(2,0,6);
}
else if(res[0]!=0&&res[1]==3){
out_put(2,0,4);
}
else {
out_put(2,0,5);
}
}
}
}
}
else if(list1.size()==4){
Quadrangle polygon1=new Quadrangle(list1);
if(polygon1.is_quadrangle()){
if(list2.size()==5){
Pentagon polygon2=new Pentagon(list2);
if(polygon2.is_pentagon()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(1,2,1);
}
else if(res[2]==1||res[2]==2){
out_put(1,2,2);
}
else if(res[0]==5&&res[1]==4){
out_put(1,2,3);
}
else if(res[0]==5&&res[1]!=0){
out_put(1,2,6);
}
else if(res[0]!=0&&res[1]==4){
out_put(1,2,4);
}
else {
out_put(1,2,5);
}
}
}
else if(list2.size()==4){
Quadrangle polygon2=new Quadrangle(list2);
if(polygon2.is_quadrangle()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(1,1,1);
}
else if(res[2]==1||res[2]==2){
out_put(1,1,2);
}
else if(res[0]==4&&res[1]==4){
out_put(1,1,3);
}
else if(res[0]==4&&res[1]!=0){
out_put(1,1,6);
}
else if(res[0]!=0&&res[1]==4){
out_put(1,1,4);
}
else {
out_put(1,1,5);
}
}
}
else if(list2.size()==3){
triangle polygon2=new triangle(list2);
if(polygon2.if_triangle()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(1,0,1);
}
else if(res[2]==1||res[2]==2){
out_put(1,0,2);
}
else if(res[0]==3&&res[1]==4){
out_put(1,0,3);
}
else if(res[0]==3&&res[1]!=0){
out_put(1,0,6);
}
else if(res[0]!=0&&res[1]==4){
out_put(1,0,4);
}
else {
out_put(1,0,5);
}
}
}
}
}
else if(list1.size()==3){
triangle polygon1=new triangle(list1);
if(polygon1.if_triangle()){
if(list2.size()==5){
Pentagon polygon2=new Pentagon(list2);
if(polygon2.is_pentagon()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(0,2,1);
}
else if(res[2]==1||res[2]==2){
out_put(0,2,2);
}
else if(res[0]==5&&res[1]==3){
out_put(0,2,3);
}
else if(res[0]==5&&res[1]!=0){
out_put(0,2,6);
}
else if(res[0]!=0&&res[1]==3){
out_put(0,2,4);
}
else {
out_put(0,2,5);
}
}
}
else if(list2.size()==4){
Quadrangle polygon2=new Quadrangle(list2);
if(polygon2.is_quadrangle()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(0,1,1);
}
else if(res[2]==1||res[2]==2){
out_put(0,1,2);
}
else if(res[0]==4&&res[1]==3){
out_put(0,1,3);
}
else if(res[0]==4&&res[1]!=0){
out_put(0,1,6);
}
else if(res[0]!=0&&res[1]==3){
out_put(0,1,4);
}
else {
out_put(0,1,5);
}
}
}
else if(list2.size()==3){
triangle polygon2=new triangle(list2);
if(polygon2.if_triangle()){
double[] res=new double[3];
res=polygon.union_poly(polygon1,polygon2);
if(res[2]==0){
out_put(0,0,1);
}
else if(res[2]==1||res[2]==2){
out_put(0,0,2);
}
else if(res[0]==3&&res[1]==3){
out_put(0,0,3);
}
else if(res[0]==3&&res[1]!=0){
out_put(0,0,6);
}
else if(res[0]!=0&&res[1]==3){
out_put(0,0,4);
}
else {
out_put(0,0,5);
}
}
}
}
}
return;
}
case 5:{
if(c.length!=10){
System.out.println("wrong number of points");
return;
}
polygon poly1=new polygon();
polygon poly2=new polygon();
for(int i=0;i<c.length/2;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly1.list.add(p);
}
for(int i=c.length/2;i<c.length;i++) {
String[] v = c[i].split(",");
Point p=new Point();
p.x = Double.parseDouble(v[0]);
p.y = Double.parseDouble(v[1]);
poly2.list.add(p);
}
ArrayList<Point> list1=new ArrayList<>();
ArrayList<Point> list2=new ArrayList<>();
list1=poly1.get_point();
list2=poly2.get_point();
ArrayList<Point> list_point1=new ArrayList<>();
ArrayList<Point> list_point2=new ArrayList<>();
ArrayList<Point> list_point3=new ArrayList<>();
ArrayList<Point> list_point4=new ArrayList<>();
ArrayList<Point> list_point=new ArrayList<>();
polygon polygon1=new polygon();
polygon polygon2=new polygon();
polygon1.list=list1;
polygon2.list=list2;
list_point1=polygon1.poly_point(polygon2);
list_point2=polygon2.poly_point(polygon1);
list_point3=polygon1.crossover_point(polygon2);
list_point4=polygon.union_point(list_point1,list_point2);
list_point=polygon.union_point(list_point4,list_point3);
list_point=polygon.sort(list_point);
list_point=polygon.sort(list_point);
polygon Ploy=new Pentagon(list_point);
Ploy.get_square();
System.out.println(sishewuru_3(Ploy.ares));
return;
}
case 6:{
if(c.length!=6){
System.out.println("wrong number of points");
return;
}
Point p=new Point();
String[] u=c[0].split(",");
p.x=Double.parseDouble(u[0]);
p.y=Double.parseDouble(u[1]);
polygon poly1=new polygon();
for(int i=1;i<c.length;i++) {
String[] v = c[i].split(",");
Point q=new Point();
q.x = Double.parseDouble(v[0]);
q.y = Double.parseDouble(v[1]);
poly1.list.add(q);
}
ArrayList<Point> list1=new ArrayList<>();
list1=poly1.get_point();
if(list1.size()==5){
Pentagon polygon1=new Pentagon(list1);
if(polygon1.is_pentagon()){
if(polygon1.where_point(p)){
if(polygon1.where_point1(p)){
System.out.println("on the pentagon");
}
else{
System.out.println("in the pentagon");
}
}
else{
System.out.println("outof the pentagon");
}
}
}
else if(list1.size()==4){
Quadrangle polygon1=new Quadrangle(list1);
if(polygon1.is_quadrangle()){
if(polygon1.where_point(p)){
if(polygon1.where_point1(p)){
System.out.println("on the quadrilateral");
}
else{
System.out.println("in the quadrilateral");
}
}
else{
System.out.println("outof the quadrilateral");
}
}
}
else if(list1.size()==3){
triangle polygon1=new triangle(list1);
if(polygon1.if_triangle()){
if(polygon1.where_point(p)){
if(polygon1.where_point1(p)){
System.out.println("on the triangle");
}
else{
System.out.println("in the triangle");
}
}
else{
System.out.println("outof the triangle");
}
}
}
return;
}
}
}
//格式化数据
public static double sishewuru_3(double x){
String str = String.format("%.3f",x);
Double s = Double.parseDouble(str);
return s;
}
//输出结果
public static void out_put(int a,int b,int x){
String[] s={"triangle","quadrilateral","pentagon"};
String s1=s[a];
String s2=s[b];
switch (x){
case 1://分离
System.out.println("no overlapping area between the previous "+s1+" and the following "+s1);break;
case 2://连接
System.out.println("the previous "+s1+" is connected to the following "+s2);break;
case 3://重合
System.out.println("the previous "+s1+" coincides with the following "+s2);break;
case 4://被包含
System.out.println("the previous "+s1+" is inside the following "+s2);break;
case 5://交错
System.out.println("the previous "+s1+" is interlaced with the following "+s2);break;
case 6://包含
System.out.println("the previous "+s1+" contains the following "+s2);break;
default:
return;
}
}
//输出点的坐标
public static void out_put(polygon poly){
for(int i=0;i<poly.list.size();i++){
System.out.println((poly.list.get(i).x+","+poly.list.get(i).y));
}
}
}
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String s=in.nextLine();
if(s.length()<=2){
System.out.println("Wrong Format");
return;
}
int x = (int)s.charAt(0)-'0';
if(s.charAt(1)!=':'){
System.out.println("Wrong Format");
return;
}
String[] c = s.substring(2).split(" ");
for(int i=0;i<c.length;i++){
String[] m = c[i].split(",");
if(m.length!=2){
System.out.println("Wrong Format");
return;
}
for(int j=0;j<m.length;j++){
if(!m[j].matches("^[+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?)")){
System.out.println("Wrong Format");
return;
}
}
}
if(!(x>=4&&x<=6)){
System.out.println("Wrong Format");
return;
}
solve problem=new solve();
problem.solve_matter(x,c);
return;
}
}
分析:调用上一题的类与方法,使用循环依次判断第一个多边形的点是否在第二个多边形内部,再判断第二个多边形的点是否在第一个多边形内部,并求出两个多边形的交点,将这个点的集合去重存储在一个数组当中。如果去重的数组点的个数为0,则两多边形分离;如果数组点的个数小于3,则多边形连接;如果第一个多边形的点都在第二个多边形内部,并且第二个多边形的点都在第一个多边形的内部,则两个多边形重合;如果第一个多边形的点都在第二个多边形内部,但是第二个多边形的点不全在第一个多边形内部,则第一个多边形包含于第二个多边形;同理,第一个多边形包含第二个多边形;其他情况就是交错。计算两个多边形重合区域的面积,需要将得到的点的数组进行排序,排序得到的点能够组成一个凸多边形,再调用多边形计算面积和周长的方法得到周长和面积,并输出。判断点是否在多边形内部,计算该点与多边形所有边构成一个三角形计算面积之和如果大于多边形,则点在外部,如果相等,需要判断点是否在线段上,如果不在线段上,即可说明点在内部,否则点在多边形上。
类图如下:
SourceMonitor报表图如下:
3.期中考试
(1)点与线(类设计)
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format
设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。
代码如下(点击展开):
package Middle;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args){
Input input=new Input();
Point p1,p2;
p1=input.input_p();
p2=input.input_p();
if(!input.point_true(p1.getX(),p1.getY())||!input.point_true(p2.getX(),p2.getY())){
System.out.println("Wrong Format");
return;
}
String color=input.input_color();
Line line=new Line(p1,p2,color);
line.display();
return;
}
}
class Input{
Scanner input=new Scanner(System.in);
public Point input_p(){
double x,y;
x=input.nextDouble();
y=input.nextDouble();
Point p=new Point(x,y);
return p;
}
public String input_color(){
String color=input.next();
return color;
}
public boolean point_true(double x,double y){
if(x>0&x<=200&&y>0&&y<=200){
return true;
}
return false;
}
}
class Point{
private double x,y;
Point(){}
Point(double x,double y){
this.x=x;
this.y=y;
}
public void display(){
String strx=String.format("%.2f",x);
String stry=String.format("%.2f",y);
System.out.println("("+strx+","+stry+")");
return;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
}
class Line{
private Point p1=new Point();
private Point p2=new Point();
private String color;
Line(Point p1, Point p2, String color){
this.p1=p1;
this.p2=p2;
this.color=color;
}
public double getDistance(){
double distance=Math.sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
return distance;
}
public void display(){
System.out.println("The line's color is:"+getColor());
System.out.println("The line's begin point's Coordinate is:");
p1.display();
System.out.println("The line's end point's Coordinate is:");
p2.display();
System.out.print("The line's length is:");
System.out.printf("%.2f",getDistance());
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
public String getColor() {
return color;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public void setColor(String color) {
this.color = color;
}
}
分析:根据题目要求设计相关类及其属性和方法,我增加了一个输入类,输入点的坐标、字符和整数,主函数调用相关的方法进行处理。
类图如下:
SourceMonitor报表图如下:
(2)点线面问题重构(继承与多态)
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
element = p1;//起点Point
element.display();
element = p2;//终点Point
element.display();
element = line;//线段
element.display();
element = plane;//面
element.display();
代码如下(点击展开):
package Middle;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args){
Input input=new Input();
Point p1,p2;
p1=input.input_p();
p2=input.input_p();
if(!input.point_true(p1.getX(),p1.getY())||!input.point_true(p2.getX(),p2.getY())){
System.out.println("Wrong Format");
return;
}
String color=input.input_color();
Line line=new Line(p1,p2,color);
Plane plane=new Plane(color);
p1.display();
p2.display();
line.display();
plane.display();
return;
}
}
class Input{
Scanner input=new Scanner(System.in);
public Point input_p(){
double x,y;
x=input.nextDouble();
y=input.nextDouble();
Point p=new Point(x,y);
return p;
}
public String input_color(){
String color=input.next();
return color;
}
public boolean point_true(double x,double y){
if(x>0&x<=200&&y>0&&y<=200){
return true;
}
return false;
}
}
abstract class element{
abstract public void display();
}
class Point extends element{
private double x,y;
Point(){}
Point(double x,double y){
this.x=x;
this.y=y;
}
public void display(){
String strx=String.format("%.2f",x);
String stry=String.format("%.2f",y);
System.out.println("("+strx+","+stry+")");
return;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
}
class Line extends element{
private Point p1=new Point();
private Point p2=new Point();
private String color;
Line(Point p1, Point p2, String color){
this.p1=p1;
this.p2=p2;
this.color=color;
}
public double getDistance(){
double distance=Math.sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
return distance;
}
public void display(){
System.out.println("The line's color is:"+getColor());
System.out.println("The line's begin point's Coordinate is:");
p1.display();
System.out.println("The line's end point's Coordinate is:");
p2.display();
System.out.print("The line's length is:");
System.out.printf("%.2f\n",getDistance());
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
public String getColor() {
return color;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public void setColor(String color) {
this.color = color;
}
}
class Plane extends element{
private String color;
Plane(String color){
this.color=color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+getColor());
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
}
分析:在第一题的基础上设计一个抽象类,电和线都继承该抽象类,并增加面类也继承该抽象类,在抽象类中定义display方法。
类图如下:
SourceMonitor报表图如下:
(3)点线面问题再重构(容器类)
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
1:向容器中增加Point对象
2:向容器中增加Line对象
3:向容器中增加Plane对象
4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
0:输入结束
示例代码如下:
choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1://insert Point object into list
...
break;
case 2://insert Line object into list
...
break;
case 3://insert Plane object into list
...
break;
case 4://delete index - 1 object from list
int index = input.nextInt();
...
}
choice = input.nextInt();
}
输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
代码如下(点击展开):
package Middle;
import java.util.ArrayList;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args){
Input input=new Input();
int choice = input.input_int();
GeometryObject list=new GeometryObject();
while (choice!=0){
switch (choice){
case 1:{
Point p=input.input_p();
if(!input.point_true(p.getX(),p.getY())){
break;
}
list.add(p);
break;
}
case 2:{
Point p1=input.input_p(),p2=input.input_p();
if(!input.point_true(p1.getX(),p1.getY())||!input.point_true(p2.getX(),p2.getY())){
System.out.println("Wrong Format");
break;
}
String color=input.input_color();
Line line=new Line(p1,p2,color);
list.add(line);
break;
}
case 3:{
String color=input.input_color();
Plane plane=new Plane(color);
list.add(plane);
break;
}
case 4:{
int x=input.input_int();
list.remove(x);
break;
}
}
choice=input.input_int();
}
for(element p:list.list){
p.display();
}
return;
}
}
class Input{
Scanner input=new Scanner(System.in);
public Point input_p(){
double x,y;
x=input.nextDouble();
y=input.nextDouble();
Point p=new Point(x,y);
return p;
}
public String input_color(){
String color=input.next();
return color;
}
public int input_int(){
int num=input.nextInt();
return num;
}
public boolean point_true(double x,double y){
if(x>0&x<=200&&y>0&&y<=200){
return true;
}
return false;
}
}
abstract class element{
abstract public void display();
}
class Point extends element{
private double x,y;
Point(){}
Point(double x,double y){
this.x=x;
this.y=y;
}
public void display(){
String strx=String.format("%.2f",x);
String stry=String.format("%.2f",y);
System.out.println("("+strx+","+stry+")");
return;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
}
class Line extends element{
private Point p1=new Point();
private Point p2=new Point();
private String color;
Line(Point p1, Point p2, String color){
this.p1=p1;
this.p2=p2;
this.color=color;
}
public double getDistance(){
double distance=Math.sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
return distance;
}
public void display(){
System.out.println("The line's color is:"+getColor());
System.out.println("The line's begin point's Coordinate is:");
p1.display();
System.out.println("The line's end point's Coordinate is:");
p2.display();
System.out.print("The line's length is:");
System.out.printf("%.2f\n",getDistance());
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
public String getColor() {
return color;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public void setColor(String color) {
this.color = color;
}
}
class Plane extends element{
private String color;
Plane(String color){
this.color=color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+getColor());
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
}
class GeometryObject{
ArrayList<element> list=new ArrayList<>();
public void add(element el){
list.add(el);
}
public void remove(int x){
if(list.size()<x){
return;
}
list.remove(x-1);
}
public ArrayList<element> getList(){
return this.list;
}
}
分析:在第二题的基础上,增加一个容器类存放抽象类对象,有五个选项,选项0结束,选项1容器中添加点,选项2容器中添加线,选项3容器中添加面,选项4删除某个位置的对象。结束后依次调用容器内对象的display方法输出。
类图如下:
SourceMonitor报表图如下: