Java工具集-数学(一次函数)
- 2019 年 10 月 26 日
- 筆記
简单工具类
写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦 网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是 发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些 甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用 每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能 做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独 使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用. 抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!
介绍
遵从两大原则
- 1.绝不依赖JDK以外的源码
- 2.牺牲代码复用性,每个类都必须是单独的组件,绝不互相引用,做到完全解耦
package *; import java.math.BigDecimal; import java.math.RoundingMode; /** * @program: simple_tools * @description: 数学-一次函数 * @author: ChenWenLong * @create: 2019-10-24 10:48 **/ public class LinearFunction { // 默认对结果都进行四舍五入操作 // 自变量: independent 因变量: dependent // 斜率 private BigDecimal slope = BigDecimal.ONE; //与Y轴的交点 private BigDecimal point = BigDecimal.ZERO; public static LinearFunction instance; // 公式 : y=kx+b //私有化构造器 private LinearFunction(){} private LinearFunction(BigDecimal slope,BigDecimal point){ this.point = point; this.slope = slope; } //默认创建一个正比例函数 static { if(instance == null){ synchronized (LinearFunction.class){ if(instance == null){ instance = new LinearFunction(); } } } } /** * 功能描述: * 〈创建一次函数〉 * * @params : [slope, point] * @return : com.simple.util.math.LinearFunction * @author : cwl * @date : 2019/10/24 11:02 */ public static void init(double slope,double point){ if(slope == 0){ throw new RuntimeException("slope is not be zero"); } instance.setPoint(new BigDecimal(point)); instance.setSlope(new BigDecimal(slope)); } /** * 功能描述: * 〈判断点是否在线上〉 * * @params : [x, y] * @return : boolean * @author : cwl * @date : 2019/10/24 11:03 */ public static boolean isOnline(double x ,double y){ return new BigDecimal(y).setScale(2,RoundingMode.HALF_UP). equals(instance.getSlope().multiply(new BigDecimal(x)).add(new BigDecimal(y)).setScale(2,RoundingMode.HALF_UP)); } /** * 功能描述: * 〈通过y求x的值〉 * * @params : [y] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:35 */ public static BigDecimal getX(double y){ BigDecimal dependent = new BigDecimal(y); return (dependent.subtract(instance.getPoint())).divide(instance.getSlope(), 2, RoundingMode.HALF_UP); } /** * 功能描述: * 〈根据x求y〉 * * @params : [x] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:44 */ public static BigDecimal getY(double x){ BigDecimal independent = new BigDecimal(x); return independent.multiply(instance.slope).add(instance.getPoint()).setScale(2,RoundingMode.HALF_UP); } /** * 功能描述: * 〈获得斜率〉 * * @params : [x, y, b] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:57 */ public static BigDecimal getSlope(double x,double y,double b){ BigDecimal dependent = new BigDecimal(y); return (dependent.subtract(new BigDecimal(b))).divide(new BigDecimal(x), 2, RoundingMode.HALF_UP); } /** * 功能描述: * 〈正比例函数获得斜率〉 * * @params : [x, y] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:58 */ public static BigDecimal getSlope(double x,double y){ BigDecimal dependent = new BigDecimal(y); return dependent.divide(new BigDecimal(x), 2, RoundingMode.HALF_UP); } @Override public String toString() { return "LinearFunction{" + "slope=" + slope + ", point=" + point + '}'; } private BigDecimal getSlope() { return slope; } private void setSlope(BigDecimal slope) { this.slope = slope; } private BigDecimal getPoint() { return point; } private void setPoint(BigDecimal point) { this.point = point; } }