slope.java

来自「BOOK:Beginning Algorithms Code Example」· Java 代码 · 共 54 行

JAVA
54
字号
package com.wrox.algorithms.geometry;/** * Represents the slope of a {@link Line}. * Objects of this class are immutable. */public class Slope {    private final double _rise;    private final double _travel;    public Slope(double rise, double travel) {        _rise = rise;        _travel = travel;    }    public boolean isVertical() {        return _travel == 0;    }    public int hashCode() {        return (int) (_rise * _travel);    }    public boolean equals(Object object) {        if (this == object) {            return true;        }        if (object == null || object.getClass() != getClass()) {            return false;        }        Slope other = (Slope) object;        if (isVertical() && other.isVertical()) {            return true;        }        if (isVertical() || other.isVertical()) {            return false;        }        return (asDouble()) == (other.asDouble());    }    public double asDouble() {        if (isVertical()) {            throw new IllegalStateException("Vertical slope cannot be represented as double");        }        return _rise / _travel;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?