Android之2D圖形(圓、直線、點)工具類

jopen 10年前發布 | 15K 次閱讀 Android Android開發 移動開發
    public class Circle {  
        private PointF centerPoint;  
        private float radius;  
        public PointF getCenterPoint() {  
            return centerPoint;  
        }  
        public void setCenterPoint(PointF centerPoint) {  
            this.centerPoint = centerPoint;  
        }  
        public float getRadius() {  
            return radius;  
        }  
        public void setRadius(float radius) {  
            this.radius = radius;  
        }  


    }  
    public class CircleUtils {  

        /** 
         * 根據圓上的三個點求圓心坐標、半徑 
         * @param pA 
         * @param pB 
         * @param pC 
         * @return 
         */  
        public static Circle getCircle(PointF pA,PointF pB,PointF pC)  
        {  
             float mat1,mat2,mat3 ;    
             mat1 = ((pB.x*pB.x +pB.y*pB.y)-(pA.x*pA.x +pA.y*pA.y))*(2*(pC.y-pA.y))-    
                     ((pC.x*pC.x +pC.y*pC.y)-(pA.x*pA.x +pA.y*pA.y))*(2*(pB.y-pA.y));    
             mat2 = (2*(pB.x-pA.x))*((pC.x*pC.x+pC.y*pC.y)-(pA.x*pA.x +pA.y*pA.y))-    
                     (2*(pC.x-pA.x))*((pB.x*pB.x+pB.y*pB.y)-(pA.x*pA.x +pA.y*pA.y));    
             mat3 = 4*((pB.x-pA.x)*(pC.y-pA.y) - (pC.x-pA.x)*(pB.y-pA.y));    

             Circle circle=new Circle();  
             PointF centerPoint=new PointF();  
             float radius;  
             centerPoint.x = mat1/mat3;    
             centerPoint.y = mat2/mat3;   
             radius=(float) Math.sqrt(((pA.x-centerPoint.x)*(pA.x-centerPoint.x) + (pA.y-centerPoint.y)*(pA.y-centerPoint.y)));    

             circle.setCenterPoint(centerPoint);  
             circle.setRadius(radius);  

             return circle;  
        }  

        /** 
         * 求一段圓弧兩端另一點的坐標 
         * @param circle 
         * @param startP 圓弧一端的點 
         * @param angle 圓弧對應的角度 
         * @return 
         */  
        public PointF getEndPointOfArc(Circle circle,PointF startP,float angle) {  
            PointF centerP=circle.getCenterPoint();  
            PointF endPointF=new PointF();  
            endPointF.x=(float) (centerP.x+(startP.x-centerP.x)*Math.cos(angle*Math.PI/180)-(startP.y-centerP.y)*Math.sin(angle*Math.PI/180));  
            endPointF.y=(float) (centerP.y+(startP.x-centerP.x)*Math.sin(angle*Math.PI/180)+(startP.y-centerP.y)*Math.cos(angle*Math.PI/180));  
            return endPointF;  

        }  
    }  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!