在二維平面上,有兩個正方形,請找出一條直線,可以將這兩個正方形對半分

/**
 * 功能:在二維平面上,有兩個正方形,請找出一條直線,可以將這兩個正方形對半分。
 * 假定正方形的上下兩條邊與x軸平行。java

 */app

 

[java] view plain copythis

 

  1. /** 
  2.  * 考慮: 
  3.  * 線的準確含義,可能性有: 
  4.  *      1)由斜率和y軸截距肯定; 
  5.  *      2)由這條邊上的任意兩點肯定; 
  6.  *      3)線段,以正方形的邊做爲起點和終點。 
  7.  *  
  8.  * 假設:這條線的端點應該落在正方形的邊上。 
  9.  * 思路:要將兩個正方形對半分,這條線必須鏈接兩個正方形的中心點。 
  10.  */  
  11.   
  12. public class Square {  
  13.   
  14.     //正方形的四條邊  
  15.     int left;  
  16.     int right;  
  17.     int top;  
  18.     int bottem;  
  19.     int size;  
  20.       
  21.     public static void main(String[] args) {  
  22.         // TODO Auto-generated method stub  
  23.   
  24.     }  
  25.   
  26.     //獲得正方形的中心點的位置  
  27.     public Point getMiddle(){  
  28.         return new Point((this.left+this.right)/2.0,(this.top+this.bottem)/2);  
  29.     }  
  30.       
  31.     //返回線段mid1和mid2的線段與square2的邊相交的點,即從mid1到mid2畫一條線,一直延伸置碰到square2的靠外的那條邊。  
  32.     public Point extend(Point mid1,Point mid2,int size){  
  33.         //肯定線段mid1->mid2的方向  
  34.         int xdir=mid1.x<mid2.x?1:-1;  
  35.         int ydir=mid1.y<mid2.y?1:-1;  
  36.           
  37.         //若是mid1和mid2的x座標相同,計算斜率時,會拋出零異常,作特別處理  
  38.         if(mid1.x==mid2.y)  
  39.             return new Point(mid2.x,mid2.y+ydir*size/2.0);  
  40.           
  41.         //計算線段的斜率  
  42.         double slope=(mid2.y-mid1.y)/(mid2.x-mid1.x);  
  43.         double x1=0;  
  44.         double y1=0;  
  45.           
  46.         //計算相交點,注意斜率的取值  
  47.         if(Math.abs(slope)==1){  
  48.             x1=mid2.x+xdir*size/2.0;  
  49.             y1=mid2.y+ydir*size/2.0;  
  50.         }else if(Math.abs(slope)<1){  
  51.             x1=mid2.x+xdir*size/2.0;  
  52.             y1=mid2.y+slope*ydir*(size/2.0);//注意方向  
  53.         }else if(Math.abs(slope)>1){  
  54.             x1=mid2.x+slope*xdir*(size/2.0);  
  55.             y1=mid2.y+ydir*size/2.0;  
  56.         }  
  57.         return new Point(x1,y1);  
  58.     }  
  59.       
  60.     public MyLine cut(Square other){  
  61.         //計算兩個中心點之間的線段與正方形的邊相交的位置  
  62.         Point point1=extend(this.getMiddle(),other.getMiddle(),other.size);  
  63.         Point point2=extend(this.getMiddle(),other.getMiddle(),-other.size);  
  64.         Point point3=extend(other.getMiddle(),this.getMiddle(),this.size);  
  65.         Point point4=extend(other.getMiddle(),this.getMiddle(),-this.size);  
  66.           
  67.         //找出線段的起點和終點  
  68.         Point start=point1;  
  69.         Point end=point1;  
  70.         Point[] points={point1,point2,point3};  
  71.         for(int i=0;i<points.length;i++){  
  72.             if(points[i].x<start.x||points[i].x==start.x&&points[i].y<start.y)  
  73.                 start=points[i];  
  74.             else if(points[i].x>end.x||points[i].x==end.x&&points[i].y>end.y)  
  75.                 end=points[i];  
  76.         }  
  77.           
  78.         return new MyLine(start,end);  
  79.     }  
  80.       
  81. }  
  82.   
  83. class MyLine{  
  84.     Point start;  
  85.     Point end;  
  86.     public MyLine(Point start,Point end){  
  87.         this.start=start;  
  88.         this.end=end;  
  89.     }  
  90. }  
  91.   
  92. class Point{  
  93.     double x;  
  94.     double y;  
  95.     public Point(double x,double y){  
  96.         this.x=x;  
  97.         this.y=y;  
  98.     }  
  99. }

或者:spa

public static double[] getBipartition(Point[] A, Point[] B) {                                                     .net

    // 此題因爲正方形的上下兩條邊與x軸平行, 因此就不用直線相交去求中心點了                            code

    // 求出第一個點不一樣的座標(由於點的順序多是混亂的)                                      blog

    double p1x1 = A[0].x;                                             ip

    double p1x2 = A[1].x == A[0].x ? A[2].x:A[1].x;                   ci

    double p1y1 = A[0].y;                                             get

    double p1y2 = A[1].y == A[0].y ? A[2].y:A[1].y;                  

                                                                       

    // 求出第二個點不一樣的座標(由於點的順序多是混亂的)                                     

    double p2x1 = B[0].x;                                            

    double p2x2 = B[1].x == B[0].x ? B[2].x:B[1].x;                  

    double p2y1 = B[0].y;                                            

    double p2y2 = B[1].y == B[0].y ? B[2].y:B[1].y;                  

                                                                       

    // 平分線的兩個點                                                       

    double x1 = (double)(p1x2 + p1x1) / 2;                           

    double y1 = (double)(p1y2 + p1y1) / 2;                           

    double x2 = (double)(p2x1 + p2x2) / 2;                           

    double y2 = (double)(p2y1 + p2y2) / 2;                           

                                                                       

    double k = (double)(y2 - y1) / (x2 - x1);                        

    double b = y1 - k*x1;                                            

                                                                       

    return new double[] {k,b};                                       

}  

 

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class Bipartition {

    public double[] getBipartition(Point[] A, Point[] B) {

        // write code here

        //平分直線一定通過正方形的中心點

        int x1 = (A[0].x+A[2].x)/2;

        int y1 = (A[0].y+A[2].y)/2;

         

        int x2 = (B[0].x+B[2].x)/2;

        int y2 = (B[0].y+B[2].y)/2;

         

        double[] a = new double[2];

        a[0]=(double)(y2-y1)/(double)(x2-x1);

        a[1]=y2-a[0]*x2;

        return a;

    }

}

 

public class Bipartition {

    public double[] getBipartition(Point[] A, Point[] B) {

        // write code here

        double Acenterx=0;//正方形A的中心x座標

        double Acentery=0;//正方形A的中心y座標

        

        for(int i=0;i<4;i++){

            Acenterx+=A[i].x;

            Acentery+=A[i].y;

        }

        Acenterx/=4.0;

        Acentery/=4.0;

        

        double Bcenterx=0;//正方形B的中心x座標

        double Bcentery=0;//正方形B的中心y座標

        for(int i=0;i<4;i++){

            Bcenterx+=B[i].x;

            Bcentery+=B[i].y;

        }

        Bcenterx/=4.0;

        Bcentery/=4.0;

        

        double[] result=new double[2];

        result[0]=(Bcentery-Acentery)/(Bcenterx-Acenterx);//求斜率

        result[1]=Acentery-result[0]*Acenterx;//求截距

        

        return result;

    }

}

相關文章
相關標籤/搜索