一个体育生的编程之路(一)补充

20107月做的笔试题:(下边是我自己当时做出来的答案,当时只会JAVA,如果有更好的方法,请不吝赐教!)

第一个题目

在一个坐标系中已知两个矩形的左下顶点和右上顶点的坐标,如果两个矩形有重叠区域,求出重叠区域矩形的左下顶点坐标和右上顶点坐标。

public class Mytest{

       public static void main(String[] args){

      

              float x1=1.0f,y1=1.0f;      //第一个矩形左下顶点

              float x2=3.0f,y2=3.0f;      //第一个矩形右上顶点

              float x3=2.0f,y3=2.0f;      //第二个矩形左下顶点

              float x4=4.0f,y4=4.0f;      //第二个矩形右上顶点

 

              float x5,y5;   //重合区域矩形左下顶点

              float x6,y6;   //重合区域矩形右上顶点

 

              x5 = x1<=x3?x3:x1;

              y5 = y1<=y3?y3:y1;

              x6 = x2<=x4?x2:x4;

              y6 = y2<=y4?y2:y4;

              if(x5<x6&&y5<y6){

                     System.out.println("两个矩形有重叠区域。\n 重叠区域的矩形坐标为:");

                     System.out.println("x5= " + x5 + ", y5=" + y5);

                     System.out.println("x6= " + x6 + ", y6=" + y6);

              }

       }

}

 

 

第二个题目

第二个题目是已经三角形ABC三个顶点的坐标,A(x1,y1),   B(x2,y2),  C(x3,y3) ,   和另外一点P(x,y) 的坐标,怎么判断在三角形内还是外

 

虽然做出来了,但是不高效,现在我也不知道高效的方法是什么?哪位大侠告诉小弟。

方法一:求面积之和

public class Mytest02 {

       public static void main(String[] args){

              double x1=1.0, y1=1.0 ;           //A

              double x2=3.0, y2=1.0 ;           //B

              double x3=2.0, y3=3.0 ;   //C

 

      

              double x=2.0, y=2.0 ;  //P

 

              double pa = Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));

              double pb = Math.sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));

              double pc = Math.sqrt((x-x3)*(x-x3)+(y-y3)*(y-y3));

      

              double ab = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

              double ac = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));

              double bc = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));

      

              double abc = (ab+bc+ac)/2 ;

      

              double pab = (pa+pb+ab)/2;

              double pbc = (pb+pc+bc)/2;

              double pac = (pa+pc+ac)/2;

System.out.println(abc);

System.out.println(pab);

System.out.println(pbc);

System.out.println(pac);

              double sabc = Math.sqrt(abc*(abc-ab)*(abc-bc)*(abc-ac));

      

              double spab = Math.sqrt(pab*(pab-pa)*(pab-pb)*(pab-ab));

              double spbc = Math.sqrt(pbc*(pbc-pb)*(pbc-pc)*(pbc-bc));

              double spac = Math.sqrt(pac*(pac-pa)*(pac-pc)*(pac-ac));

System.out.println(sabc);

System.out.println(spab);

System.out.println(spbc);

System.out.println(spac);

              //判断

              if(sabc-(spab+spbc+spac)<0.0000001){

                     System.out.println("P点在这个三角形内部或者边上");

              }else {

                     System.out.println("P点不在这个三角形外部");

              }

       }

 

   

}

 

 

方法二:求角度之和

public class Mytest03 {

       public static void main(String[] args){

              double x1=1.0, y1=1.0 ;           //A

              double x2=3.0, y2=1.0 ;           //B

              double x3=2.0, y3=3.0 ;   //C

 

      

              double x=2.0, y=2.0 ;  //P

 

              double pa = Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));

              double pb = Math.sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));

              double pc = Math.sqrt((x-x3)*(x-x3)+(y-y3)*(y-y3));

      

              double ab = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

              double ac = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));

              double bc = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));

      

double apb = Math.acos((pa*pa+pb*pb-ab*ab)/(2*pa*pb)) ;

double apc = Math.acos((pa*pa+pc*pc-ac*ac)/(2*pa*pc)) ;

double bpc = Math.acos((pb*pb+pc*pc-bc*bc)/(2*pb*pc)) ;

System.out.println(apb) ;

System.out.println(apc) ;

System.out.println(bpc) ;

System.out.println(Math.PI*2) ;

              //判断

              if(apb+apc+bpc-Math.PI*2<0.0000001){

                     System.out.println("P点在这个三角形内部或者边上");

              }else {

                     System.out.println("P点在这个三角形外部");

              }

       }

 

}

你可能感兴趣的:(一个体育生的编程之路(一)补充)