CalculateModel.java 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.leiguoqiang.handwriting.model;

import android.util.Log;

import com.leiguoqiang.handwriting.entity.PointBean;

import java.math.BigDecimal;

/**
 * @author leiguoqiang
 * contact: 274764936
 * 算法模型:曲线拟合点算法,距离算法,笔锋辅助点算法
 */
public class CalculateModel {

    public static float add(float a, float b) {
qq_35526484's avatar
qq_35526484 已提交
17
        float result = new BigDecimal(a).add(new BigDecimal(b)).floatValue();
18 19
        return Float.isNaN(result) ? 0.0f : result;

qq_35526484's avatar
qq_35526484 已提交
20
//        float result = KeyImpl.add(a, b);
21
//        return Float.isNaN(result) ? 0.0f : result;
qq_35526484's avatar
qq_35526484 已提交
22
    }
23

qq_35526484's avatar
qq_35526484 已提交
24 25
    public static float subtraction(float a, float b) {
        float result = new BigDecimal(a).subtract(new BigDecimal(b)).floatValue();
26
        return Float.isNaN(result) ? 0.0f : result;
qq_35526484's avatar
qq_35526484 已提交
27 28 29

//        float result = KeyImpl.subtraction(a, b);
//        return Float.isNaN(result) ? 0.0f : result;
30 31 32
    }

    public static float subtraction(long a, long b) {
qq_35526484's avatar
qq_35526484 已提交
33
        float result = new BigDecimal(a).subtract(new BigDecimal(b)).longValue();
34
        return Float.isNaN(result) ? 0.0f : result;
qq_35526484's avatar
qq_35526484 已提交
35 36 37
//
//        float result = KeyImpl.subtraction(a, b);
//        return Float.isNaN(result) ? 0.0f : result;
38 39 40
    }

    public static float multiple(float a, float b) {
qq_35526484's avatar
qq_35526484 已提交
41
        float result = new BigDecimal(a).multiply(new BigDecimal(b)).floatValue();
42
        return Float.isNaN(result) ? 0.0f : result;
qq_35526484's avatar
qq_35526484 已提交
43 44 45
//
//        float result = KeyImpl.multiple(a, b);
//        return Float.isNaN(result) ? 0.0f : result;
46 47 48
    }

    public static float divider(float a, float b) {
qq_35526484's avatar
qq_35526484 已提交
49 50 51 52 53 54
        float result = 0.0f;
        try {
            result = new BigDecimal(a).divide(new BigDecimal(b), 20, BigDecimal.ROUND_HALF_UP).floatValue();
        } catch (Exception e) {
            Log.i("handwriting_module:", "除法错误");
        }
55
        return Float.isNaN(result) ? 0.0f : result;
qq_35526484's avatar
qq_35526484 已提交
56 57 58

//        float result = KeyImpl.divider(a, b);
//        return Float.isNaN(result) ? 0.0f : result;
59 60 61
    }

    public static float divider(long a, long b) {
qq_35526484's avatar
qq_35526484 已提交
62 63 64 65 66 67
        float result = 0;
        try {
            result = new BigDecimal(a).divide(new BigDecimal(b), 20, BigDecimal.ROUND_HALF_UP).longValue();
        } catch (Exception e) {
            Log.i("handwriting_module:", "除法错误");
        }
68
        return Float.isNaN(result) ? 0.0f : result;
qq_35526484's avatar
qq_35526484 已提交
69 70 71

//        float result = KeyImpl.divider(a, b);
//        return Float.isNaN(result) ? 0.0f : result;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }

    /**
     * 两点间距离
     *
     * @param pointA
     * @param pointB
     * @return
     */
    public static float distance(PointBean pointA, PointBean pointB) {
        float result = 0.0f;
        if (pointA.equals(pointB)) {
            return result;
        }
        try {
            float distanceX = Math.abs(subtraction(pointA.x, pointB.x));
            float distanceY = Math.abs(subtraction(pointA.y, pointB.y));
            double temp = Math.sqrt(add(multiple(distanceX, distanceX),
                    multiple(distanceY, distanceY)));
            result = (float) temp;
        } catch (Exception e) {
            Log.i("handwriting_module:", "两点距离算法错误");
        }
        return Float.isNaN(result) ? 0.0f : result;
    }

    /**
     * 两点滑动速度
     *
     * @param pointA
     * @param pointB
     * @return
     */
    public static float velocity(PointBean pointA, PointBean pointB) {
        float result = 0.0f;
        try {
            float temp1 = (long) distance(pointA, pointB);
            float temp2 = subtraction(pointB.time, pointA.time);
            result = divider(temp1, temp2);
        } catch (Exception e) {
        }
        return result;
    }

}