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
这题已经增加至五边形了,选项一还是老规矩,先判定其是否为五边形,判定的条件和四边形相似,为五点中任意两点不重合,相邻两边不平行,不相邻边线段无交点。选项1相关代码如下:
boolean JudgeIspointSuperposition(){
if(this.p1.JudgeSuperposition(p2)||this.p1.JudgeSuperposition(p3)||this.p1.JudgeSuperposition(p4)
||this.p1.JudgeSuperposition(p5)||this.p2.JudgeSuperposition(p3)||this.p2.JudgeSuperposition(p4)
||this.p2.JudgeSuperposition(p5)||this.p3.JudgeSuperposition(p4)||this.p3.JudgeSuperposition(p5)
||this.p4.JudgeSuperposition(p5))
return true;
return false;
}
boolean JudgeIsPentagon(){
if(JudgeIspointSuperposition())
return false;
if(l12.parallel(l23)||l23.parallel(l34)||l34.parallel(l45)||l45.parallel(l15))
return false;
if(l12.LineSegment(l34)||l23.LineSegment(l45)||l34.LineSegment(l15)||l45.LineSegment(l12))
return false;
return true;
}
选项2是判断该五边形是凹五边形还是凸五边形。如果为凸五边形,则输出五边形周长、面积。如果无法构成五边形,则输出"not a pentagon"。判断凹凸五边形,我使用了向量法,即从一个顶点开始顺时针或者逆时针开始走,相邻两点构成一个向量。如果相邻向量点乘都大于0,则说明该五边形为凸五边形,因为如果是凸五边形,相邻边构成的一定是锐角,所以相邻向量点乘大于0。同理,如果是凹五边形,那么它一定至少存在一个为夹角为钝角的向量角,所以如果存在一个小于0的向量点乘即可证明它是凹五边形。面积使用多边形面积公式即可求的。选项2相关代码如下:
boolean JudgeIsConvex(){
double x1=this.p2.x-this.p1.x;
double y1=this.p2.y-this.p1.y;
double x2=this.p3.x-this.p2.x;
double y2=this.p3.y-this.p2.y;
double x3=this.p4.x-this.p3.x;
double y3=this.p4.y-this.p3.y;
double x4=this.p5.x-this.p4.x;
double y4=this.p5.y-this.p4.y;
double x5=this.p1.x-this.p5.x;
double y5=this.p1.y-this.p5.y;
double t1=x1*x2+y1*y2;
double t2=x2*x3+y2*y3;
double t3=x3*x4+y3*y4;
double t4=x4*x5+y4*y5;
double t5=x5*x1+y5*y1;
if(t1>=0&&t2>=0&&t3>=0&&t4>=0&&t5>=0)
return true;
return false;
}
void Choice2(){
if(!JudgeIsPentagon()){
System.out.println("not a pentagon");
return;
}
if(!JudgeIsConvex()){
System.out.println(false);
return;
}
System.out.print(true+" ");
accuracy(Circumference());
System.out.print(" ");
accuracy(Area());
}
选项三需要判断一条线与后面五个点构成的多边形交点,如果交点为2个则输出面积。首先存在一点,如何判断后面的多边形是五边形还是四边形还是三角形。我使用的方法是定义了一个类似于度的概念,也就是如果存在一个三点共线,则度+1;相邻三点逐一判断,求出多边形的度,那么他所构成的n边形满足,n=点个数-度。这部分代码如下:
int Judgetype(){
int i=0;
if(this.p1.distance(this.p2)+this.p2.distance(this.p3)==this.p1.distance(this.p3))
i++;
if(this.p2.distance(this.p3)+this.p3.distance(this.p4)==this.p2.distance(this.p4))
i++;
if(this.p3.distance(this.p4)+this.p4.distance(this.p5)==this.p3.distance(this.p5))
i++;
if(this.p4.distance(this.p5)+this.p5.distance(this.p1)==this.p4.distance(this.p1))
i++;
if(this.p5.distance(this.p1)+this.p1.distance(this.p2)==this.p5.distance(this.p2))
i++;
return i;
}
但是选项3并不需要输出几边形,只需要分割面积即可。所以选项3我并没有使用该函数,后面才使用。求分割面积思路和四边形一致,依然是使用三个Point数组,一个数组存储交点,一个数组存储在直线上方的点,一个数组存储在直线下方的点。相关代码如下:
void Choice3(line l) {
if (l.equal(this.l12) || l.equal(this.l23) || l.equal(this.l34) || l.equal(this.l45) || l.equal(this.l15)) {
System.out.println("The line is coincide with one of the lines");
return;
}
line[] lines = {l12, l23, l34, l45, l15};
point[] points = {p1, p2, p3, p4, p5};
point[] jdpoint = new point[10];
point[] spoint = new point[10];
point[] xpoint = new point[10];
double s1,s2,s;
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
for (int i = 0; i < 5; i++) {
if (l.JudgeIntersection(l, lines[i]) != null) {
boolean weather = true;
for (int j = 0; j < cnt1; j++) {
if (jdpoint[j].x == l.JudgeIntersection(l, lines[i]).x && jdpoint[j].y == l.JudgeIntersection(l, lines[i]).y)
weather = false;
}
if (weather == true)
jdpoint[cnt1++] = l.JudgeIntersection(l, lines[i]);
}
if (l.JudgePointHighLine(points[i])) {
spoint[cnt2++] = points[i];
} else {
xpoint[cnt3++] = points[i];
}
}
if(cnt1!=2){
System.out.println(cnt1);
}
else {
System.out.print(cnt1+" ");
if(cnt2==1){
s1=Area(spoint[0],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
else if(cnt2==2){
s1=Area(spoint[0],spoint[1],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
else if(cnt2==3){
s1=Area(xpoint[0],xpoint[1],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
else if(cnt2==4){
s1=Area(xpoint[0],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
}
}
选项4判断两个多边形的关系,该选项测试点并未全部通过,可能存在逻辑问题。
大致思路为将另一个五边形的五个点依次判定是否在第一个五边形内部,如果全部在第一个五边形内部,则说明第一个五边形包含第二个五边形。如果第一个五边形的五个点全部在第二个五边形内部,则说明第一个五边形被第二个五边形包含。如果五个点全部在第一个五边形外部,则说明两个五边形相离。如果五个点既有在五边形内部又存在在五边形外部,则说明两个五边形相交,如果五个点中只存在在五边形上和五边形外的点则说明两个五边形连接,如果五个点全部在第一个五边形上,则说明重合。选项4相关代码如下:
void Choice4(Pentagon P){
int i1=Judgetype();
int i2=P.Judgetype();
String[] strings={"pentagon","quadrilateral","triangle"};
int cnt1=0,cnt2=0,cnt3=0;
point[] points={P.p1,P.p2,P.p3,P.p4,P.p5};
for(int i=0;i<5;i++) {
if (PointInside(points[i]) == 0)
cnt1++;//线上
else if (PointInside(points[i]) == 1)
cnt2++;//内
else if (PointInside(points[i]) == 2)
cnt3++;//外
}
if(jiaocuo(P)){
System.out.println("the previous "+strings[i1]+" is interlaced with the following "+strings[i2]);
return;
}
if(cnt1==0&&cnt2==0&&cnt3!=0){
System.out.println("no overlapping area between the previous "+strings[i1]+" and the following "+strings[i2]);
return;
}
if(cnt1!=0&&cnt3!=0&&cnt2==0){
System.out.println("the previous "+strings[i1]+" is connected to the following "+strings[i2]);
return;
}
if(cnt2==0&&cnt3==0&&cnt1!=0){
System.out.println("the previous "+strings[i1]+" coincides with the following "+strings[i2]);
return;
}
if(cnt3==0&&cnt2!=0){
System.out.println("the previous "+strings[i1]+" contains the following "+strings[i2]);
return;
}
if(cnt2==0&&cnt3!=0){
System.out.println("the previous "+strings[i1]+" is inside the following "+strings[i2]);
return;
}
if(cnt2!=0&&cnt3!=0){
System.out.println("the previous "+strings[i1]+" is interlaced with the following "+strings[i2]);
}
}
选项5还是分割问题,之前是使用线来分割,现在使用多边形进行分割。求公共区域的面积,大体思路很简单,以一个五边形为参考系,将另一个五边形在这个五边形内部的点存入一个Point数组中,再将交点去重复的存入该数组,得到的Point数组即为公共面积的多边形所需要的点,这时需要对数组中点进行排序,因为存入数据时,点不一定是按顺序存入,如果使用非顺序的点求面积则会错误。排序可以在多边形内任取一点,利用向量求夹角,使其夹角增加或减少的存入。相关代码如下:
void Choice5(Pentagon P){
int cnt2=0,cnt=0;
line[] lines1={l12,l23,l34,l45,l15};
line[] lines2={P.l12,P.l23,P.l34,P.l45,P.l15};
point[] points={p1,p2,p3,p4,p5};
point[] points1={P.p1,P.p2,P.p3,P.p4,P.p5};
point[] lmpoint=new point[10];
point[] lmpoint1=new point[10];
for(int i=0;i<5;i++){
if(P.pointInpentagon(points[i])){
boolean flag=true;
for(int j=0;j<cnt2;j++){
if(points[i].JudgeSuperposition(lmpoint[j]))
flag=false;
}
if(flag==true)
lmpoint[cnt2++]=points[i];
}
}
for(int i=0;i<5;i++){
if(this.pointInpentagon(points1[i])){
boolean flag=true;
for(int j=0;j<cnt2;j++){
if(points1[i].JudgeSuperposition(lmpoint[j]))
flag=false;
}
if(flag==true)
lmpoint[cnt2++]=points1[i];
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++) {
if(lines1[i].JudgeIntersection(lines1[i],lines2[j])!=null){
point p=lines1[i].JudgeIntersection(lines1[i],lines2[j]);
if(P.pointInpentagon(p)&&this.pointInpentagon(p)){
boolean flag=true;
for(int k=0;k<cnt2;k++){
if(p.JudgeSuperposition(lmpoint[k]))
flag=false;
}
if(flag==true)
lmpoint[cnt2++]=p;
}
}
}
}
xiangliang[] xiangliangs=new xiangliang[5];
xiangliang xx=new xiangliang(new point(1,0),new point(0,0));
for(int i=0;i<cnt2;i++){
point p0=new point(1,0);
xiangliangs[i]=new xiangliang(lmpoint[i],p0);
xiangliangs[i].tag=0;
}
for(int i=0;i<cnt2;i++){
int max=i;
for(int j=i;j<cnt2;j++){
if(lmpoint[i].y>0&&xiangliangs[j].jisuanjiajiao(xx)>xiangliangs[i].jisuanjiajiao(xx)){
max=j;
}
}
if(xiangliangs[max].status==false){
xiangliangs[max].tag=cnt++;
xiangliangs[max].status=true;
}
}
for(int i=0;i<cnt2;i++){
int min=i;
for(int j=i;j<cnt2;j++){
if(lmpoint[i].y<0&&xiangliangs[j].jisuanjiajiao(xx)<xiangliangs[i].jisuanjiajiao(xx)){
min=j;
}
}
if(xiangliangs[min].status==false){
xiangliangs[min].tag=cnt++;
xiangliangs[min].status=true;
}
}
for(int j=0;j<cnt;j++){
if(xiangliangs[j].tag==j)
lmpoint1[j]=xiangliangs[j].p1;
}
if(cnt==3){
accuracy(Area(lmpoint1[0],lmpoint1[1],lmpoint1[2]));
}
if(cnt==4){
accuracy(Area(lmpoint1[0],lmpoint1[1],lmpoint1[2],lmpoint1[3]));
}
if(cnt==5){
Pentagon pen=new Pentagon(lmpoint1[0],lmpoint1[1],lmpoint1[2],lmpoint1[3],lmpoint1[4]);
accuracy(pen.Area());
}
if(cnt==6){
accuracy(Area(lmpoint1[0],lmpoint1[1],lmpoint1[2],lmpoint1[3],lmpoint1[4],lmpoint1[5]));
}
}
选项6判断点是否在多边形内部,使用面积法,妥妥的送分。相关代码如下:
void Choice6(point p){
int type=Judgetype();
if(type==0){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
System.out.println("on the pentagon");
return;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
System.out.println("in the pentagon");
}
else {
System.out.println("outof the pentagon");
}
}
else if(type==1){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
System.out.println("on the quadrilateral");
return;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
System.out.println("in the quadrilateral");
}
else {
System.out.println("outof the quadrilateral");
}
}
else if(type==2){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
System.out.println("on the triangle");
return;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
System.out.println("in the triangle");
}
else {
System.out.println("outof the triangle");
}
}
}
其余相关函数如下:
double Circumference(){
double d1=this.p1.distance(p2);
double d2=this.p2.distance(p3);
double d3=this.p3.distance(p4);
double d4=this.p4.distance(p5);
double d5=this.p5.distance(p1);
return d1+d2+d3+d4+d5;
}
double Area(){
return Math.abs(0.5*(p1.x*p2.y-p2.x*p1.y+p2.x*p3.y-p3.x*p2.y+p3.x*p4.y-p4.x*p3.y+p4.x*p5.y-p5.x*p4.y+p5.x*p1.y-p1.x*p5.y));
}
double Area(point p1,point p2,point p3){
return Math.abs(0.5*((p1.x*p2.y-p2.x*p1.y)+(p2.x*p3.y-p3.x*p2.y)+(p3.x*p1.y-p1.x*p3.y)));
}
double Area(point p1,point p2,point p3,point p4){
double s1,s2,s3,s4;
s1=Area(p1,p2,p4);
s2=Area(p2,p3,p4);
s3=Area(p1,p3,p4);
s4=Area(p1,p2,p3);
if(s1>=s2&&s1>=s3&&s1>=s4) {
if(s2==0||s3==0||s4==0)
return s1;
if (s4 + s3 + s2 == s1) {
return s1 - s2;
} else {
return s1 + s2;
}
}
else if(s2>=s1&&s2>=s3&&s2>=s4) {
if(s1==0||s3==0||s4==0)
return s2;
if (s4 + s3 + s1 == s2) {
return s2-s1;
} else {
return s1 + s2;
}
}
else if(s3>=s1&&s3>=s2&&s3>=s4) {
if(s1==0||s2==0||s4==0)
return s3;
if (s4 +s2+s1== s3) {
return s3-s4;
} else {
return s1 + s2;
}
}
else {
if(s1==0||s2==0||s3==0)
return s4;
if (s1 + s3 + s2 == s4) {
return s4-s3;
} else {
return s1 + s2;
}
}
}
double Area(point p1,point p2,point p3,point p4,point p5,point p6){
return Math.abs(0.5*(p1.x*p2.y-p2.x*p1.y+p2.x*p3.y-p3.x*p2.y+p3.x*p4.y-p4.x*p3.y+p4.x*p5.y-p5.x*p4.y+p5.x*p6.y-p6.x*p5.y+p6.x*p1.y-p1.x*p6.y));
}
void accuracy(double a){
String s=Double.toString(a);
String str=s.substring(s.indexOf(".")+1);
if(str.length()>3)
System.out.printf("%.3f",a);
else
System.out.print(a);
}
boolean jiaocuo(Pentagon p){
if(this.l12.LineSegment(p.l12)||this.l12.LineSegment(p.l23)||this.l12.LineSegment(p.l34)||
this.l12.LineSegment(p.l45)||this.l12.LineSegment(p.l15)){
return true;
}
if(this.l23.LineSegment(p.l12)||this.l23.LineSegment(p.l23)||this.l23.LineSegment(p.l34)||
this.l23.LineSegment(p.l45)||this.l23.LineSegment(p.l15)){
return true;
}
if(this.l34.LineSegment(p.l12)||this.l34.LineSegment(p.l23)||this.l34.LineSegment(p.l34)||
this.l34.LineSegment(p.l45)||this.l34.LineSegment(p.l15)){
return true;
}
if(this.l45.LineSegment(p.l12)||this.l45.LineSegment(p.l23)||this.l45.LineSegment(p.l34)||
this.l12.LineSegment(p.l45)||this.l12.LineSegment(p.l15)){
return true;
}
if(this.l15.LineSegment(p.l12)||this.l15.LineSegment(p.l23)||this.l15.LineSegment(p.l34)||
this.l15.LineSegment(p.l45)||this.l15.LineSegment(p.l15)){
return true;
}
return false;
}
boolean pointInpentagon(point p){
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1))
return true;
return false;
}
double jisuan(point p1){
return (p1.x*1)/Math.sqrt(Math.pow(p1.x,2)+Math.pow(p1.y,2));
}
}
点类
class point{
double x,y;
point(double a,double b){
x=a;
y=b;
}
boolean JudgeInLine(line l){
if(this.x>=Math.min(l.p1.x,l.p2.x)&&this.x<=Math.max(l.p1.x,l.p2.x)&&this.y>=Math.min(l.p1.y,l.p2.y)&&this.y<=Math.max(l.p1.y,l.p2.y)){
return true;
}
else
return false;
}
boolean JudgeSuperposition(point p){
if(p.x==this.x&&p.y==this.y)
return true;
return false;
}
double distance(point p2){
return Math.sqrt(Math.pow((this.x-p2.x),2)+Math.pow((this.y-p2.y),2));
}
}
class line{
double a,b,c;
point p1,p2;
line(point p1,point p2){
a=p2.y-p1.y;
b=p1.x-p2.x;
c=p2.x*p1.y-p1.x*p2.y;
this.p1=p1;
this.p2=p2;
}
boolean JudgePointHighLine(point p){
if(a*p.x+b*p.y+c>0)
return true;
else
return false;
}
boolean JudgePointInLine(point p){
if(a*p.x+b*p.y+c==0&&(p.x<=Math.max(this.p1.x,this.p2.x)&&p.x>=Math.min(this.p1.x,this.p2.x)&&p.y<=Math.max(this.p1.y,this.p2.y)&&p.y>=Math.min(this.p1.y,this.p2.y)))
return true;
else
return false;
}
point JudgeIntersection(line l1,line l2){
if(l1.parallel(l2))
return null;
double x=(l2.c*l1.b-l1.c*l2.b)/(l1.a*l2.b-l2.a*l1.b);
double y=(l1.c*l2.a-l2.c*l1.a)/(l1.a*l2.b-l2.a*l1.b);
if(x>=Math.min(l2.p1.x,l2.p2.x)&&x<=Math.max(l2.p1.x,l2.p2.x)&&y>=Math.min(l2.p1.y,l2.p2.y)&&y<=Math.max(l2.p1.y,l2.p2.y)){
point p=new point(x,y);
return p;
}
else
return null;
}
boolean parallel(line l){
if(this.a==0){
if(l.a==0)
return true;
else
return false;
}
else if(this.b==0){
if(l.b==0)
return true;
else
return false;
}
else{
if(l.a/l.b==this.a/this.b)
return true;
else
return false;
}
}
boolean Superposition(line l){
if((l.p1.x!=l.p2.x&&l.p1.y!=l.p2.y)&&(a*l.p1.x+b*l.p1.y+c==0&&a*l.p2.x+b*l.p2.y+c==0))
return true;
return false;
}
boolean LineSegment(line l){
if(l.parallel(this))
return false;
if(Math.max(this.p1.x,this.p2.x)<=Math.min(l.p1.x,l.p2.x)||Math.max(l.p1.x,l.p2.x)<=Math.min(this.p1.x,this.p2.x)||Math.max(this.p1.y,this.p2.y)<=Math.min(l.p1.y,l.p2.y)||Math.max(l.p1.y,l.p2.y)<=Math.min(this.p1.y,this.p2.y))
return false;
else
return true;
}
boolean equal(line l){
if(parallel(l)&&this.c==l.c)
return true;
return false;
}
}
五边形类
class Pentagon{
point p1,p2,p3,p4,p5;
line l12,l13,l14,l15,l23,l24,l25,l34,l35,l45;
Pentagon(point p1,point p2,point p3,point p4,point p5){
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
this.p5=p5;
this.l12=new line(this.p1,this.p2);
this.l13=new line(this.p1,this.p3);
this.l14=new line(this.p1,this.p4);
this.l15=new line(this.p1,this.p5);
this.l23=new line(this.p2,this.p3);
this.l24=new line(this.p2,this.p4);
this.l25=new line(this.p2,this.p5);
this.l34=new line(this.p3,this.p4);
this.l35=new line(this.p3,this.p5);
this.l45=new line(this.p4,this.p5);
}
boolean JudgeIspointSuperposition(){
if(this.p1.JudgeSuperposition(p2)||this.p1.JudgeSuperposition(p3)||this.p1.JudgeSuperposition(p4)
||this.p1.JudgeSuperposition(p5)||this.p2.JudgeSuperposition(p3)||this.p2.JudgeSuperposition(p4)
||this.p2.JudgeSuperposition(p5)||this.p3.JudgeSuperposition(p4)||this.p3.JudgeSuperposition(p5)
||this.p4.JudgeSuperposition(p5))
return true;
return false;
}
boolean JudgeIsPentagon(){
if(JudgeIspointSuperposition())
return false;
if(l12.parallel(l23)||l23.parallel(l34)||l34.parallel(l45)||l45.parallel(l15))
return false;
if(l12.LineSegment(l34)||l23.LineSegment(l45)||l34.LineSegment(l15)||l45.LineSegment(l12))
return false;
return true;
}
int Judgetype(){
int i=0;
if(this.p1.distance(this.p2)+this.p2.distance(this.p3)==this.p1.distance(this.p3))
i++;
if(this.p2.distance(this.p3)+this.p3.distance(this.p4)==this.p2.distance(this.p4))
i++;
if(this.p3.distance(this.p4)+this.p4.distance(this.p5)==this.p3.distance(this.p5))
i++;
if(this.p4.distance(this.p5)+this.p5.distance(this.p1)==this.p4.distance(this.p1))
i++;
if(this.p5.distance(this.p1)+this.p1.distance(this.p2)==this.p5.distance(this.p2))
i++;
return i;
}
int PointInside(point p){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
return 0;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
return 1;
}
else {
return 2;
}
}
void Choice6(point p){
int type=Judgetype();
if(type==0){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
System.out.println("on the pentagon");
return;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
System.out.println("in the pentagon");
}
else {
System.out.println("outof the pentagon");
}
}
else if(type==1){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
System.out.println("on the quadrilateral");
return;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
System.out.println("in the quadrilateral");
}
else {
System.out.println("outof the quadrilateral");
}
}
else if(type==2){
if(l12.JudgePointInLine(p)||l23.JudgePointInLine(p)||l34.JudgePointInLine(p)||l45.JudgePointInLine(p)||l15.JudgePointInLine(p)){
System.out.println("on the triangle");
return;
}
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1)){
System.out.println("in the triangle");
}
else {
System.out.println("outof the triangle");
}
}
}
boolean JudgeIsConvex(){
double x1=this.p2.x-this.p1.x;
double y1=this.p2.y-this.p1.y;
double x2=this.p3.x-this.p2.x;
double y2=this.p3.y-this.p2.y;
double x3=this.p4.x-this.p3.x;
double y3=this.p4.y-this.p3.y;
double x4=this.p5.x-this.p4.x;
double y4=this.p5.y-this.p4.y;
double x5=this.p1.x-this.p5.x;
double y5=this.p1.y-this.p5.y;
double t1=x1*x2+y1*y2;
double t2=x2*x3+y2*y3;
double t3=x3*x4+y3*y4;
double t4=x4*x5+y4*y5;
double t5=x5*x1+y5*y1;
if(t1>=0&&t2>=0&&t3>=0&&t4>=0&&t5>=0)
return true;
return false;
}
void Choice2(){
if(!JudgeIsPentagon()){
System.out.println("not a pentagon");
return;
}
if(!JudgeIsConvex()){
System.out.println(false);
return;
}
System.out.print(true+" ");
accuracy(Circumference());
System.out.print(" ");
accuracy(Area());
}
void Choice3(line l) {
if (l.equal(this.l12) || l.equal(this.l23) || l.equal(this.l34) || l.equal(this.l45) || l.equal(this.l15)) {
System.out.println("The line is coincide with one of the lines");
return;
}
line[] lines = {l12, l23, l34, l45, l15};
point[] points = {p1, p2, p3, p4, p5};
point[] jdpoint = new point[10];
point[] spoint = new point[10];
point[] xpoint = new point[10];
double s1,s2,s;
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
for (int i = 0; i < 5; i++) {
if (l.JudgeIntersection(l, lines[i]) != null) {
boolean weather = true;
for (int j = 0; j < cnt1; j++) {
if (jdpoint[j].x == l.JudgeIntersection(l, lines[i]).x && jdpoint[j].y == l.JudgeIntersection(l, lines[i]).y)
weather = false;
}
if (weather == true)
jdpoint[cnt1++] = l.JudgeIntersection(l, lines[i]);
}
if (l.JudgePointHighLine(points[i])) {
spoint[cnt2++] = points[i];
} else {
xpoint[cnt3++] = points[i];
}
}
if(cnt1!=2){
System.out.println(cnt1);
}
else {
System.out.print(cnt1+" ");
if(cnt2==1){
s1=Area(spoint[0],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
else if(cnt2==2){
s1=Area(spoint[0],spoint[1],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
else if(cnt2==3){
s1=Area(xpoint[0],xpoint[1],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
else if(cnt2==4){
s1=Area(xpoint[0],jdpoint[0],jdpoint[1]);
s=Area();
s2=s-s1;
accuracy(Math.min(s1,s2));
System.out.print(" ");
accuracy(Math.max(s1,s2));
}
}
}
void Choice4(Pentagon P){
int i1=Judgetype();
int i2=P.Judgetype();
String[] strings={"pentagon","quadrilateral","triangle"};
int cnt1=0,cnt2=0,cnt3=0;
point[] points={P.p1,P.p2,P.p3,P.p4,P.p5};
for(int i=0;i<5;i++) {
if (PointInside(points[i]) == 0)
cnt1++;//线上
else if (PointInside(points[i]) == 1)
cnt2++;//内
else if (PointInside(points[i]) == 2)
cnt3++;//外
}
if(jiaocuo(P)){
System.out.println("the previous "+strings[i1]+" is interlaced with the following "+strings[i2]);
return;
}
if(cnt1==0&&cnt2==0&&cnt3!=0){
System.out.println("no overlapping area between the previous "+strings[i1]+" and the following "+strings[i2]);
return;
}
if(cnt1!=0&&cnt3!=0&&cnt2==0){
System.out.println("the previous "+strings[i1]+" is connected to the following "+strings[i2]);
return;
}
if(cnt2==0&&cnt3==0&&cnt1!=0){
System.out.println("the previous "+strings[i1]+" coincides with the following "+strings[i2]);
return;
}
if(cnt3==0&&cnt2!=0){
System.out.println("the previous "+strings[i1]+" contains the following "+strings[i2]);
return;
}
if(cnt2==0&&cnt3!=0){
System.out.println("the previous "+strings[i1]+" is inside the following "+strings[i2]);
return;
}
if(cnt2!=0&&cnt3!=0){
System.out.println("the previous "+strings[i1]+" is interlaced with the following "+strings[i2]);
}
}
void Choice5(Pentagon P){
int cnt2=0,cnt=0;
line[] lines1={l12,l23,l34,l45,l15};
line[] lines2={P.l12,P.l23,P.l34,P.l45,P.l15};
point[] points={p1,p2,p3,p4,p5};
point[] points1={P.p1,P.p2,P.p3,P.p4,P.p5};
point[] lmpoint=new point[10];
point[] lmpoint1=new point[10];
for(int i=0;i<5;i++){
if(P.pointInpentagon(points[i])){
boolean flag=true;
for(int j=0;j<cnt2;j++){
if(points[i].JudgeSuperposition(lmpoint[j]))
flag=false;
}
if(flag==true)
lmpoint[cnt2++]=points[i];
}
}
for(int i=0;i<5;i++){
if(this.pointInpentagon(points1[i])){
boolean flag=true;
for(int j=0;j<cnt2;j++){
if(points1[i].JudgeSuperposition(lmpoint[j]))
flag=false;
}
if(flag==true)
lmpoint[cnt2++]=points1[i];
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++) {
if(lines1[i].JudgeIntersection(lines1[i],lines2[j])!=null){
point p=lines1[i].JudgeIntersection(lines1[i],lines2[j]);
if(P.pointInpentagon(p)&&this.pointInpentagon(p)){
boolean flag=true;
for(int k=0;k<cnt2;k++){
if(p.JudgeSuperposition(lmpoint[k]))
flag=false;
}
if(flag==true)
lmpoint[cnt2++]=p;
}
}
}
}
xiangliang[] xiangliangs=new xiangliang[6];
xiangliang xx=new xiangliang(new point(1,0),new point(0,0));
for(int i=0;i<cnt2;i++){
point p0=new point(1,0);
xiangliangs[i]=new xiangliang(lmpoint[i],p0);
xiangliangs[i].tag=0;
}
for(int i=0;i<cnt2;i++){
int max=i;
for(int j=i+1;j<cnt2;j++){
if(lmpoint[i].y>0&&xiangliangs[j].jisuanjiajiao(xx)>xiangliangs[i].jisuanjiajiao(xx)){
max=j;
}
}
if(xiangliangs[max].status==false){
xiangliangs[max].tag=cnt++;
xiangliangs[max].status=true;
}
}
for(int i=0;i<cnt2;i++){
int min=i;
for(int j=i+1;j<cnt2;j++){
if(lmpoint[i].y<0&&xiangliangs[j].jisuanjiajiao(xx)<xiangliangs[i].jisuanjiajiao(xx)){
min=j;
}
}
if(xiangliangs[min].status==false){
xiangliangs[min].tag=cnt++;
xiangliangs[min].status=true;
}
}
System.out.println(cnt);
//for(int i=0;i<cnt;i++){
for(int j=0;j<cnt;j++){
if(xiangliangs[j].tag==j)
lmpoint1[j]=xiangliangs[j].p1;
}
//}
System.out.println(xiangliangs[cnt-1].p1.x+" "+xiangliangs[cnt-1].p1.y);
for(int i=0;i<cnt;i++){
System.out.println(lmpoint1[i].x+" "+lmpoint1[i].y);
}
if(cnt==3){
accuracy(Area(lmpoint1[0],lmpoint1[1],lmpoint1[2]));
}
if(cnt==4){
accuracy(Area(lmpoint1[0],lmpoint1[1],lmpoint1[2],lmpoint1[3]));
}
if(cnt==5){
Pentagon pen=new Pentagon(lmpoint1[0],lmpoint1[1],lmpoint1[2],lmpoint1[3],lmpoint1[4]);
accuracy(pen.Area());
}
if(cnt==6){
accuracy(Area(lmpoint1[0],lmpoint1[1],lmpoint1[2],lmpoint1[3],lmpoint1[4],lmpoint1[5]));
}
}
double Circumference(){
double d1=this.p1.distance(p2);
double d2=this.p2.distance(p3);
double d3=this.p3.distance(p4);
double d4=this.p4.distance(p5);
double d5=this.p5.distance(p1);
return d1+d2+d3+d4+d5;
}
double Area(){
return Math.abs(0.5*(p1.x*p2.y-p2.x*p1.y+p2.x*p3.y-p3.x*p2.y+p3.x*p4.y-p4.x*p3.y+p4.x*p5.y-p5.x*p4.y+p5.x*p1.y-p1.x*p5.y));
}
double Area(point p1,point p2,point p3){
return Math.abs(0.5*((p1.x*p2.y-p2.x*p1.y)+(p2.x*p3.y-p3.x*p2.y)+(p3.x*p1.y-p1.x*p3.y)));
}
double Area(point p1,point p2,point p3,point p4){
double s1,s2,s3,s4;
s1=Area(p1,p2,p4);
s2=Area(p2,p3,p4);
s3=Area(p1,p3,p4);
s4=Area(p1,p2,p3);
if(s1>=s2&&s1>=s3&&s1>=s4) {
if(s2==0||s3==0||s4==0)
return s1;
if (s4 + s3 + s2 == s1) {
return s1 - s2;
} else {
return s1 + s2;
}
}
else if(s2>=s1&&s2>=s3&&s2>=s4) {
if(s1==0||s3==0||s4==0)
return s2;
if (s4 + s3 + s1 == s2) {
return s2-s1;
} else {
return s1 + s2;
}
}
else if(s3>=s1&&s3>=s2&&s3>=s4) {
if(s1==0||s2==0||s4==0)
return s3;
if (s4 +s2+s1== s3) {
return s3-s4;
} else {
return s1 + s2;
}
}
else {
if(s1==0||s2==0||s3==0)
return s4;
if (s1 + s3 + s2 == s4) {
return s4-s3;
} else {
return s1 + s2;
}
}
}
double Area(point p1,point p2,point p3,point p4,point p5,point p6){
return Math.abs(0.5*(p1.x*p2.y-p2.x*p1.y+p2.x*p3.y-p3.x*p2.y+p3.x*p4.y-p4.x*p3.y+p4.x*p5.y-p5.x*p4.y+p5.x*p6.y-p6.x*p5.y+p6.x*p1.y-p1.x*p6.y));
}
void accuracy(double a){
String s=Double.toString(a);
String str=s.substring(s.indexOf(".")+1);
if(str.length()>3)
System.out.printf("%.3f",a);
else
System.out.print(a);
}
boolean jiaocuo(Pentagon p){
if(this.l12.LineSegment(p.l12)||this.l12.LineSegment(p.l23)||this.l12.LineSegment(p.l34)||
this.l12.LineSegment(p.l45)||this.l12.LineSegment(p.l15)){
return true;
}
if(this.l23.LineSegment(p.l12)||this.l23.LineSegment(p.l23)||this.l23.LineSegment(p.l34)||
this.l23.LineSegment(p.l45)||this.l23.LineSegment(p.l15)){
return true;
}
if(this.l34.LineSegment(p.l12)||this.l34.LineSegment(p.l23)||this.l34.LineSegment(p.l34)||
this.l34.LineSegment(p.l45)||this.l34.LineSegment(p.l15)){
return true;
}
if(this.l45.LineSegment(p.l12)||this.l45.LineSegment(p.l23)||this.l45.LineSegment(p.l34)||
this.l12.LineSegment(p.l45)||this.l12.LineSegment(p.l15)){
return true;
}
if(this.l15.LineSegment(p.l12)||this.l15.LineSegment(p.l23)||this.l15.LineSegment(p.l34)||
this.l15.LineSegment(p.l45)||this.l15.LineSegment(p.l15)){
return true;
}
return false;
}
boolean pointInpentagon(point p){
if(Area()==Area(p,p1,p2)+Area(p,p2,p3)+Area(p,p3,p4)+Area(p,p4,p5)+Area(p,p5,p1))
return true;
return false;
}
double jisuan(point p1){
return (p1.x*1)/Math.sqrt(Math.pow(p1.x,2)+Math.pow(p1.y,2));
}
}
标签:p2,p3,p1,return,p4,Area,nchu,oop,2022 From: https://www.cnblogs.com/pillowtalk/p/16836220.html