Android绘制锁屏功能所涉及的一些重要代码

本文介绍了一种计算二维平面上两点间角度的方法,并提供了计算两点间直线距离的公式。通过对不同情况的讨论,实现了全面的角度计算逻辑,适用于游戏开发、图形学等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   /**
     * 计算两点之间的角度
     * @param a
     * @param b
     * @return
     */
    public float getDegrees(Point a, Point b) {
        float ax = a.x;// a.index % 3;
        float ay = a.y;// a.index / 3;
        float bx = b.x;// b.index % 3;
        float by = b.y;// b.index / 3;
        float degrees = 0;
        if (bx == ax) // y轴相等 90度或270
        {
            if (by > ay) // 在y轴的下边 90
            {
                degrees = 90;
            } else if (by < ay) // 在y轴的上边 270
            {
                degrees = 270;
            }
        } 
        else if (by == ay) // y轴相等 0度或180
        {
            if (bx > ax) // 在y轴的下边 90
            {
                degrees = 0;
            } else if (bx < ax) // 在y轴的上边 270
            {
                degrees = 180;
            }
        } 
        else {
            if (bx > ax) // 在y轴的右边 270~90
            {
                if (by > ay) // 在y轴的下边 0 - 90
                {
                    degrees = 0;
                    degrees = degrees
                            + switchDegrees(Math.abs(by - ay),
                                    Math.abs(bx - ax));
                } else if (by < ay) // 在y轴的上边 270~0
                {
                    degrees = 360;
                    degrees = degrees
                            - switchDegrees(Math.abs(by - ay),
                                    Math.abs(bx - ax));
                }
            } else if (bx < ax) // 在y轴的左边 90~270
            {
                if (by > ay) // 在y轴的下边 180 ~ 270
                {
                    degrees = 90;
                    degrees = degrees
                            + switchDegrees(Math.abs(bx - ax),
                                    Math.abs(by - ay));
                } else if (by < ay) // 在y轴的上边 90 ~ 180
                {
                    degrees = 270;
                    degrees = degrees
                            - switchDegrees(Math.abs(bx - ax),
                                    Math.abs(by - ay));
                }

            }
        }
        return degrees;
    }
   /**
     * 1=30度 2=45度 4=60度
     * 
     * @param tan
     * @return
     */
    private float switchDegrees(float x, float y) {
            return (float) MathUtil.pointTotoDegrees(x, y);
    }
   /**
     * 两点间的距离
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return
     */
  public static double distance(double x1, double y1, double x2, double y2) {
        return Math.sqrt(Math.abs(x1 - x2) * Math.abs(x1 - x2)
                + Math.abs(y1 - y2) * Math.abs(y1 - y2));
    }
/**
 * 计算点a(x,y)的角度
 * 
 * @param x
 * @param y
 * @return
 */
public static double pointTotoDegrees(double x, double y) {
    return Math.toDegrees(Math.atan2(x, y));
}
       /**
         * 鼠标的坐标是否和九宫格中的点重合
         * @param pointX 点的x坐标
         * @param pointY 点的y坐标
         * @param r 九宫格中的点的半径
         * @param movingX 鼠标的x坐标
         * @param movingY 鼠标的y坐标
         */
        public static boolean with(float pointX,float pointY,float r,float movingX,float movingY) {
            // 开方
            return Math.sqrt((pointX - movingX) * (pointX - movingX) + (pointY - movingY) * (pointY - movingY)) < r;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值