天天看點

一個體育生的程式設計之路(一)補充

2010年7月做的筆試題:(下邊是我自己當時做出來的答案,當時隻會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點在這個三角形外部");

              }

       }

}

繼續閱讀