美文网首页
一个关于自定义View的Math工具类

一个关于自定义View的Math工具类

作者: galaxy_zheng | 来源:发表于2019-08-12 16:56 被阅读0次

layout: post

title: '一个关于自定义View的Math工具类'

subtitle: '转载请注明出处'

date: 2019-08-12

categories: Utils

cover: 'http://bpic.588ku.com/back_pic/05/61/11/465b46e23671e61.jpg'

tags: Utils



import android.graphics.PointF;

public class MathUtils {
    private MathUtils() {
    }

    /**
     * Get the distance between two points.
     * 获得两点之间距离
     *
     * @param A Point A
     * @param B Point B
     * @return the distance between point A and point B.
     */
    public static double getDistance(PointF A, PointF B) {
//        return (int) Math.sqrt(Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2));
        return Math.sqrt(Math.pow(A.x - B.x, 2) + Math.pow(A.y - B.y, 2));
    }


    /**
     * Get the distance between two points.
     * 获得两点之间距离
     */
    public static double getDistance(float x1, float y1, float x2, float y2) {
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }

    /**
     * 获得两点之间角度
     *
     * @param A centerPoint
     * @param B secondPoint
     * @return
     */
    public static double getAngleFromPoint(PointF A, PointF B) {
        //centerPoint中心点  secondPoint移动点
        double width = B.x - A.x;
        double height = B.y - A.y;
        double rads = Math.atan2(height, width);
        double angle = Math.toDegrees(rads);

        return angle;
    }

    /**
     * Get the coordinates of a point on the line by cut length.
     * 获取某一条直线上距离起点一定距离的某点的位置
     *
     * @param A         Point A
     * @param B         Point B
     * @param cutLength cut length
     * @return the point.
     */
    public static PointF getPointByCutLength(PointF A, PointF B, int cutLength) {
        float radian = getRadian(A, B);
        return new PointF(A.x + (int) (cutLength * Math.cos(radian)), A.y + (int) (cutLength * Math.sin(radian)));
    }

    /**
     * Get the radian between current line(determined by point A and B) and horizontal line.
     * 获得线段与水平线之间夹角的弧度值
     *
     * @param A point A
     * @param B point B
     * @return the radian
     */
    public static float getRadian(PointF A, PointF B) {
        float lenA = B.x - A.x;
        float lenB = B.y - A.y;
        float lenC = (float) Math.sqrt(lenA * lenA + lenB * lenB);
        float radian = (float) Math.acos(lenA / lenC);
        radian = radian * (B.y < A.y ? -1 : 1);
        return radian;
    }

    /**
     * Get the degrees between current line(determined by point A and B) and horizontal line.
     * 获得线段与水平线之间夹角的角度值
     *
     * @param A point A
     * @param B point B
     * @return the degrees
     */
    public static double getDegrees(PointF A, PointF B) {
        return Math.toDegrees(getRadian(A, B));
    }

    /**
     * angle to radian
     * 角度值转换为弧度值
     *
     * @param angle angle
     * @return radian
     */
    public static double angle2Radian(double angle) {
        return angle / 180 * Math.PI;
    }

    /**
     * radian to angle
     * 弧度值转换为角度值
     *
     * @param radian radian
     * @return angle
     */
    public static double radian2Angle(double radian) {
        return radian / Math.PI * 180;
    }


//    private void CircleCenter(double x1, double y1, double x2, double y2, double R, Canvas canvas) {
//        double c1 = (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1) / (2 * (x2 - x1));
//        double c2 = (y2 - y1) / (x2 - x1);  //斜率
//        double A = (c2 * c2 + 1);
//        double B = (2 * x1 * c2 - 2 * c1 * c2 - 2 * y1);
//        double C = x1 * x1 - 2 * x1 * c1 + c1 * c1 + y1 * y1 - R * R;
//        float y = (float) ((-B + Math.sqrt(B * B - 4 * A * C)) / (2 * A));
//        float x = (float) (c1 - c2 * y);
//        System.out.println("圆心x:"+x+"圆心y:"+y+"-----------------------不太准确有极端情况需要判断");
//    }
}

相关文章

网友评论

      本文标题:一个关于自定义View的Math工具类

      本文链接:https://www.haomeiwen.com/subject/itnujctx.html