From 50e586a002e67d10fc5551ef8d58e82f3ba1759f Mon Sep 17 00:00:00 2001 From: Rainbow Yang Date: Thu, 15 Nov 2018 15:30:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0graphics=EF=BC=8C=E5=8C=85?= =?UTF-8?q?=E6=8B=ACpoint=E5=92=8Cline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../moe/rainbowyang/math/graphics/Graphics.kt | 8 +++ .../math/{ => graphics}/line/Line.kt | 21 ++++---- .../rainbowyang/math/graphics/point/Point.kt | 38 ++++++++++++++ .../math/graphics/point/Point2D.kt | 23 +++++++++ .../math/graphics/point/Point2DPolar.kt | 31 ++++++++++++ .../math/graphics/point/Point3D.kt | 30 ++++++++++++ .../{ => graphics}/point/Point3DSpherical.kt | 20 ++++---- .../math/{ => graphics}/point/PointAxes.kt | 44 +++++++++-------- .../{ => graphics}/point/PointSpherical.kt | 45 +++++++++-------- .../point/_extensions.kt} | 19 ++++--- .../moe/rainbowyang/math/point/Point.kt | 42 ---------------- .../moe/rainbowyang/math/point/Point2D.kt | 21 -------- .../math/point/Point2DBiangular.kt | 15 ------ .../rainbowyang/math/point/Point2DBipolar.kt | 20 -------- .../math/point/Point2DBipolarCenter.kt | 18 ------- .../rainbowyang/math/point/Point2DElliptic.kt | 18 ------- .../math/point/Point2DHyperbolic.kt | 40 --------------- .../math/point/Point2DParabolic.kt | 49 ------------------- .../rainbowyang/math/point/Point2DPolar.kt | 37 -------------- .../moe/rainbowyang/math/point/Point3D.kt | 27 ---------- 21 files changed, 208 insertions(+), 360 deletions(-) create mode 100644 src/main/kotlin/moe/rainbowyang/math/graphics/Graphics.kt rename src/main/kotlin/moe/rainbowyang/math/{ => graphics}/line/Line.kt (68%) create mode 100644 src/main/kotlin/moe/rainbowyang/math/graphics/point/Point.kt create mode 100644 src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2D.kt create mode 100644 src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2DPolar.kt create mode 100644 src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3D.kt rename src/main/kotlin/moe/rainbowyang/math/{ => graphics}/point/Point3DSpherical.kt (52%) rename src/main/kotlin/moe/rainbowyang/math/{ => graphics}/point/PointAxes.kt (65%) rename src/main/kotlin/moe/rainbowyang/math/{ => graphics}/point/PointSpherical.kt (57%) rename src/main/kotlin/moe/rainbowyang/math/{point/PointChange.kt => graphics/point/_extensions.kt} (68%) delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2D.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DBiangular.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolar.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolarCenter.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DElliptic.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DHyperbolic.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DParabolic.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point2DPolar.kt delete mode 100644 src/main/kotlin/moe/rainbowyang/math/point/Point3D.kt diff --git a/build.gradle b/build.gradle index 0f4ba16..73107cb 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'moe.rainbowyang' -version '0.1.3' +version '0.2.0' repositories { mavenCentral() diff --git a/src/main/kotlin/moe/rainbowyang/math/graphics/Graphics.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/Graphics.kt new file mode 100644 index 0000000..90f3827 --- /dev/null +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/Graphics.kt @@ -0,0 +1,8 @@ +package moe.rainbowyang.math.graphics + +/** + * 抽象图形 + * @author: Rainbow Yang + * @create: 2018-11-14 18:52 + **/ +interface Graphics \ No newline at end of file diff --git a/src/main/kotlin/moe/rainbowyang/math/line/Line.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/line/Line.kt similarity index 68% rename from src/main/kotlin/moe/rainbowyang/math/line/Line.kt rename to src/main/kotlin/moe/rainbowyang/math/graphics/line/Line.kt index 11d582a..03ba1bc 100644 --- a/src/main/kotlin/moe/rainbowyang/math/line/Line.kt +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/line/Line.kt @@ -1,24 +1,25 @@ -package moe.rainbowyang.math.line +package moe.rainbowyang.math.graphics.line import moe.rainbowyang.math.almostEquals -import moe.rainbowyang.math.point.Point2D +import moe.rainbowyang.math.graphics.point.Point2D +import moe.rainbowyang.math.number.Real +import moe.rainbowyang.math.number.toReal import kotlin.math.atan /** * 数学意义上的线 * 提供求交点等功能 * 表达式为ax+by+c=0 - * * @author Rainbow Yang */ -class Line(val a: Double, val b: Double, val c: Double) { +class Line(val a: Real, val b: Real, val c: Real) { /** 斜率 */ val slope = -a / b /** 倾斜角 */ - val angle = atan(slope) + val angle = atan(slope.value).toReal() - constructor(a: Number, b: Number, c: Number) : this(a.toDouble(), b.toDouble(), c.toDouble()) + constructor(a: Number, b: Number, c: Number) : this(a.toReal(), b.toReal(), c.toReal()) companion object { /** @@ -36,14 +37,15 @@ class Line(val a: Double, val b: Double, val c: Double) { */ operator fun invoke(point: Point2D, angle: Double) = if ((angle - Math.PI / 2) % Math.PI almostEquals 0.0) { - Line(1, 0, -point.x) + Line(1, 0, -point.x.value) } else { val a = Math.tan(angle) - Line(a, -1, point.y * (1 - a)) + Line(a, -1, point.y.value * (1 - a)) } val X_AXIS = Line(0.0, 1.0, 0.0) val Y_AXIS = Line(1.0, 0.0, 0.0) + operator fun invoke(point: Point2D, angle: Real) = invoke(point, angle.value) } /** @@ -52,7 +54,8 @@ class Line(val a: Double, val b: Double, val c: Double) { */ infix fun crossTo(other: Line): Point2D { //平行 - if (a * other.b - b * other.a == 0.0) throw NoCrossException("$this has no cross with $other") + if (a * other.b - b * other.a == Real.ZERO) + throw NoCrossException("$this has no cross with $other") return Point2D(-(c * other.b - b * other.c) / (a * other.b - b * other.a), -(a * other.c - c * other.a) / (a * other.b - b * other.a)) diff --git a/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point.kt new file mode 100644 index 0000000..50ad7b0 --- /dev/null +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point.kt @@ -0,0 +1,38 @@ +package moe.rainbowyang.math.graphics.point + +import moe.rainbowyang.math.graphics.Graphics +import moe.rainbowyang.math.number.Real +import moe.rainbowyang.math.operation.Addition + +/** + * 抽象点 + * 所有子类的所有操作均不应修改其本身,而是返回一个新的类 + * 所有点之间都应能够进行互相转换,可以用[PointAxes]作为中介 + * @author Rainbow Yang + */ +abstract class Point : Graphics, Addition { + + /** + * 转换为[PointAxes] + */ + abstract val asAxes: PointAxes + + /** + * 检测该点中是否没有[Double.NaN]之类无效的值 + */ + open val available: Boolean get() = asAxes.available + + /** + * 默认通过[PointAxes]进行计算,值为其模 + */ + open val length: Real get() = asAxes.length + + override operator fun plus(other: Point): Point = asAxes.plus(other.asAxes) + override operator fun unaryMinus(): Point = this * -Real.ONE + + open operator fun times(times: Real): Point = asAxes * times + open operator fun div(div: Real): Point = this * div.reciprocal() + + override fun equals(other: Any?): Boolean = asAxes == other + override fun hashCode(): Int = asAxes.hashCode() +} diff --git a/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2D.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2D.kt new file mode 100644 index 0000000..974ad5a --- /dev/null +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2D.kt @@ -0,0 +1,23 @@ +package moe.rainbowyang.math.graphics.point + +import moe.rainbowyang.math.number.Real +import moe.rainbowyang.math.number.atan2 + +/** + * 二维笛卡尔坐标系点 + * @author Rainbow Yang + */ +data class Point2D(val x: Real, val y: Real) : Point() { + + constructor(point: PointAxes) : this(point[0], point[1]) + + override val asAxes = PointAxes(x, y) + + val angle = atan2(y, x) + + /** + * 逆时针旋转[angle]【弧度】 + */ + fun spin(angle: Real) = asPoint2DPolar.spin(angle).asPoint2D + +} diff --git a/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2DPolar.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2DPolar.kt new file mode 100644 index 0000000..0218a39 --- /dev/null +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point2DPolar.kt @@ -0,0 +1,31 @@ +package moe.rainbowyang.math.graphics.point + +import moe.rainbowyang.math.number.Real +import moe.rainbowyang.math.number.toReal +import moe.rainbowyang.math.operation.cos +import moe.rainbowyang.math.operation.sin + +/** + * 二维极坐标点 + * @author Rainbow Yang + */ +data class Point2DPolar(val radius: Real, val angle: Real) : Point() { + + constructor(radius: Number, angle: Number) : this(radius.toReal(), angle.toReal()) + + companion object { + operator fun invoke(form: Point): Point2DPolar { + val pd = form.asPoint2D + return Point2DPolar(pd.length, pd.angle) + } + } + + override val asAxes by lazy { PointAxes(radius * cos(angle), radius * sin(angle)) } + + /** + * 逆时针旋转[angle]【弧度】 + */ + fun spin(angle: Real) = Point2DPolar(radius, this.angle + angle) + +} + diff --git a/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3D.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3D.kt new file mode 100644 index 0000000..7838315 --- /dev/null +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3D.kt @@ -0,0 +1,30 @@ +package moe.rainbowyang.math.graphics.point + +import moe.rainbowyang.math.number.Real +import moe.rainbowyang.math.number.toReal + +/** + * 三维轴坐标点 + * @author Rainbow Yang + */ +data class Point3D(val x: Real, val y: Real, val z: Real) : Point() { + + constructor(x: Number = 0, y: Number = 0, z: Number = 0) : this(x.toReal(), y.toReal(), z.toReal()) + + companion object { + operator fun invoke(form: Point): Point3D { + val (x, y, z) = form.asAxes + return Point3D(x, y, z) + } + } + + override val asAxes = PointAxes(x, y, z) + + fun spinAtXY(angle: Real) = asAxes.spinAtAndNew(0, 1, angle) + fun spinAtXZ(angle: Real) = asAxes.spinAtAndNew(0, 2, angle) + fun spinAtYX(angle: Real) = asAxes.spinAtAndNew(1, 0, angle) + fun spinAtYZ(angle: Real) = asAxes.spinAtAndNew(1, 2, angle) + fun spinAtZX(angle: Real) = asAxes.spinAtAndNew(2, 0, angle) + fun spinAtZY(angle: Real) = asAxes.spinAtAndNew(2, 1, angle) + +} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point3DSpherical.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3DSpherical.kt similarity index 52% rename from src/main/kotlin/moe/rainbowyang/math/point/Point3DSpherical.kt rename to src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3DSpherical.kt index e9e7d97..5badd95 100644 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point3DSpherical.kt +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/Point3DSpherical.kt @@ -1,12 +1,16 @@ -package moe.rainbowyang.math.point +package moe.rainbowyang.math.graphics.point + +import moe.rainbowyang.math.number.* +import moe.rainbowyang.math.operation.cos +import moe.rainbowyang.math.operation.sin /** * 三维球坐标系点 * @author Rainbow Yang */ -class Point3DSpherical(val r: Double, val theta: Double, val phi: Double) : Point { +class Point3DSpherical(val r: Real, val theta: Real, val phi: Real) : Point() { - constructor(r: Number = 0, θ: Number = 0, φ: Number = 0) : this(r.toDouble(), θ.toDouble(), φ.toDouble()) + constructor(r: Number = 0, θ: Number = 0, φ: Number = 0) : this(r.toReal(), θ.toReal(), φ.toReal()) operator fun component1() = r operator fun component2() = theta @@ -17,19 +21,17 @@ class Point3DSpherical(val r: Double, val theta: Double, val phi: Double) : Poin val (x, y, z) = form.asAxes val r = form.length - val theta = Math.acos(z / r) + val theta = Math.acos((z / r).value).toReal() val phi = Point2D(x, y).angle return Point3DSpherical(r, theta, phi) } } - fun spinAtTheta(angle: Double) = Point3DSpherical(r, theta + angle, phi) - fun spinAtPhi(angle: Double) = Point3DSpherical(r, theta, phi + angle) + fun spinAtTheta(angle: Real) = Point3DSpherical(r, theta + angle, phi) + fun spinAtPhi(angle: Real) = Point3DSpherical(r, theta, phi + angle) - override val asAxes by lazy { - PointAxes(r * Math.sin(theta) * Math.cos(phi), r * Math.sin(theta) * Math.sin(phi), r * Math.cos(theta)) - } + override val asAxes = PointAxes(r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)) override fun toString(): String { return "Point3DSpherical(r=$r, theta=$theta, phi=$phi)" diff --git a/src/main/kotlin/moe/rainbowyang/math/point/PointAxes.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/PointAxes.kt similarity index 65% rename from src/main/kotlin/moe/rainbowyang/math/point/PointAxes.kt rename to src/main/kotlin/moe/rainbowyang/math/graphics/point/PointAxes.kt index e8e24f5..ac05371 100644 --- a/src/main/kotlin/moe/rainbowyang/math/point/PointAxes.kt +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/PointAxes.kt @@ -1,22 +1,25 @@ -package moe.rainbowyang.math.point +package moe.rainbowyang.math.graphics.point -import moe.rainbowyang.math.checkValues -import moe.rainbowyang.math.lengthOf +import moe.rainbowyang.math.number.* import moe.rainbowyang.math.until import java.util.* import kotlin.math.max + /** * 任意纬度的坐标轴点 - * * @author Rainbow Yang */ -class PointAxes(val values: List) : Point { +class PointAxes(val values: List) : Point() { + + constructor(vararg values: Real) : this(List(values.size) { values[it] }) - constructor(vararg values: Number) : this(List(values.size) { values[it].toDouble() }) + /** + * 生成size维的值均为value的点 + */ + constructor(value: Real, size: Int) : this(List(size) { value }) - //生成size维的值均为value的点 - constructor(value: Double, size: Int) : this(List(size) { value }) + constructor(value: Number, size: Int) : this(List(size) { value.toReal() }) /** * 维度数 @@ -27,33 +30,32 @@ class PointAxes(val values: List) : Point { operator fun component2() = values[1] operator fun component3() = values[2] operator fun component4() = values[3] - operator fun component5() = values[4] - operator fun get(index: Int) = values.getOrElse(index) { 0.0 } //维度不够时补0 + operator fun get(index: Int) = values.getOrElse(index) { Real.ZERO } //维度不够时补0 - override val asAxes get() = this + override val asAxes = this - override val available get() = values.checkValues() - override val length get() = values.lengthOf() + override val available = values.checkValues() + override val length = values.lengthOf() override fun plus(other: Point): Point { val paOther = other.asAxes return PointAxes(List(max(size, paOther.size)) { this[it] + paOther[it] }) } - override operator fun times(times: Double) = PointAxes(List(size) { get(it) * times }) + override operator fun times(times: Real) = PointAxes(List(size) { get(it) * times }) - fun plusAt(index: Int, plus: Number): PointAxes = - PointAxes(createNewListWithOldData(index).apply { this[index] += plus.toDouble() }) + fun plusAt(index: Int, plus: Real): PointAxes = + PointAxes(createNewListWithOldData(index).apply { this[index] += plus }) fun setAtAndNew(index: Int, value: Number): PointAxes = - PointAxes(createNewListWithOldData(index).apply { this[index] = value.toDouble() }) + PointAxes(createNewListWithOldData(index).apply { this[index] = value.toReal() }) fun timesAtAndNew(index: Int, times: Number): PointAxes = - PointAxes(createNewListWithOldData(index).apply { this[index] *= times.toDouble() }) + PointAxes(createNewListWithOldData(index).apply { this[index] *= times.toReal() }) - fun spinAtAndNew(firstIndex: Int, secondIndex: Int, angle: Number): PointAxes { + fun spinAtAndNew(firstIndex: Int, secondIndex: Int, angle: Real): PointAxes { val newValues = createNewListWithOldData(firstIndex, secondIndex) val (x, y) = Point2D(this[firstIndex], this[secondIndex]).asPoint2DPolar.spin(angle).asAxes newValues[firstIndex] = x @@ -67,7 +69,7 @@ class PointAxes(val values: List) : Point { private fun createNewListWithOldData(index: Int) = MutableList(max(index + 1, size)) { get(it) } - override fun toString() = "PointForAxes(${Arrays.toString(values.toDoubleArray())})" + override fun toString() = "PointForAxes(${Arrays.toString(values.toTypedArray())})" override fun equals(other: Any?): Boolean { @@ -85,7 +87,7 @@ class PointAxes(val values: List) : Point { } override fun hashCode(): Int { - return Arrays.hashCode(values.toDoubleArray()) + return Arrays.hashCode(values.toTypedArray()) } } diff --git a/src/main/kotlin/moe/rainbowyang/math/point/PointSpherical.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/PointSpherical.kt similarity index 57% rename from src/main/kotlin/moe/rainbowyang/math/point/PointSpherical.kt rename to src/main/kotlin/moe/rainbowyang/math/graphics/point/PointSpherical.kt index 353309b..5cf480a 100644 --- a/src/main/kotlin/moe/rainbowyang/math/point/PointSpherical.kt +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/PointSpherical.kt @@ -1,5 +1,8 @@ -package moe.rainbowyang.math.point +package moe.rainbowyang.math.graphics.point +import moe.rainbowyang.math.number.Real +import moe.rainbowyang.math.operation.cos +import moe.rainbowyang.math.operation.sin import java.util.* @@ -9,47 +12,43 @@ import java.util.* * * @author Rainbow Yang */ -data class PointSpherical(val radius: Double, val angles: List) : Point { - +data class PointSpherical(val radius: Real, val angles: List) : Point() { + companion object { operator fun invoke(cp: Point): PointSpherical { val values = cp.asAxes.values - + var radius = values[0] - val angles = mutableListOf() - + val angles = mutableListOf() + (1 until values.size).forEach { val height = values[it] val (r, angle) = Point2D(radius, height).asPoint2DPolar - + radius = r angles.add(angle) } - + return PointSpherical(radius, angles.toList()) - } } - - override val asAxes by lazy { - val values = mutableListOf() - + + override val asAxes = toAxes() + + fun toAxes(): PointAxes { + val values = mutableListOf() var rest = radius - angles.reversed().forEach { - values.add(rest * Math.sin(it)) - rest *= Math.cos(it) + values.add(rest * sin(it)) + rest *= cos(it) } - values.add(rest) - values.reverse() - - PointAxes(values) + return PointAxes(values) } - + override fun toString(): String { - return "PointSpherical(radius=$radius, angles=${Arrays.toString(angles.toDoubleArray())})" + return "PointSpherical(radius=$radius, angles=${Arrays.toString(angles.toTypedArray())})" } - + } diff --git a/src/main/kotlin/moe/rainbowyang/math/point/PointChange.kt b/src/main/kotlin/moe/rainbowyang/math/graphics/point/_extensions.kt similarity index 68% rename from src/main/kotlin/moe/rainbowyang/math/point/PointChange.kt rename to src/main/kotlin/moe/rainbowyang/math/graphics/point/_extensions.kt index b553c5f..07c214f 100644 --- a/src/main/kotlin/moe/rainbowyang/math/point/PointChange.kt +++ b/src/main/kotlin/moe/rainbowyang/math/graphics/point/_extensions.kt @@ -1,21 +1,20 @@ -package moe.rainbowyang.math.point +package moe.rainbowyang.math.graphics.point /* * 这里放置所有的点转换为其他点的方法,但其逻辑应写在其对应的类中 */ +val Point.asPoint2D get() = asAxes.asPoint2D val PointAxes.asPoint2D get() = Point2D(this) -val PointAxes.asPoint2DPolar get() = Point2DPolar(this) -val PointAxes.asPoint2DHyperbolic get() = Point2DHyperbolic(this) -val PointAxes.asPoint2DParabolic get() = Point2DParabolic(this) -val PointAxes.asPoint3D get() = Point3D(this) -val PointAxes.asPoint3DSpherical get() = Point3DSpherical(this) -val PointAxes.asPointSpherical get() = PointSpherical(this) -val Point.asPoint2D get() = asAxes.asPoint2D val Point.asPoint2DPolar get() = asAxes.asPoint2DPolar -val Point.asPoint2DHyperbolic get() = asAxes.asPoint2DHyperbolic -val Point.asPoint2DParabolic get() = asAxes.asPoint2DParabolic +val PointAxes.asPoint2DPolar get() = Point2DPolar(this) + val Point.asPoint3D get() = asAxes.asPoint3D +val PointAxes.asPoint3D get() = Point3D(this) + val Point.asPoint3DSpherical get() = asAxes.asPoint3DSpherical +val PointAxes.asPoint3DSpherical get() = Point3DSpherical(this) + val Point.asPointSpherical get() = asAxes.asPointSpherical +val PointAxes.asPointSpherical get() = PointSpherical(this) diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point.kt deleted file mode 100644 index 7a5c201..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point.kt +++ /dev/null @@ -1,42 +0,0 @@ -package moe.rainbowyang.math.point - -/** - * 表示坐标系上的一个点 - * - * 所有子类的所有操作均不应修改其本身,而是返回一个新的类 - * - * 所有点之间都应能够进行互相转换,可以用[PointAxes]作为中介 - * - * @author Rainbow Yang - */ -interface Point { - - companion object { - val ZERO = PointAxes() - } - - /** - * 转换为[PointAxes] - */ - val asAxes: PointAxes - - /** - * 检测该点中是否没有[Double.NaN]之类无效的值 - */ - val available: Boolean get() = asAxes.available - - /** - * 默认通过[PointAxes]进行计算,值为其模 - */ - val length: Double get() = asAxes.length - - operator fun plus(other: Point): Point = asAxes.plus(other.asAxes) - operator fun times(times: Double): Point = asAxes * times - - operator fun minus(other: Point) = plus(-other) - operator fun unaryMinus() = this * -1 - - operator fun times(times: Number): Point = this * times.toDouble() - operator fun div(div: Number): Point = this * (1 / div.toDouble()) - -} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2D.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2D.kt deleted file mode 100644 index 821f304..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2D.kt +++ /dev/null @@ -1,21 +0,0 @@ -package moe.rainbowyang.math.point - -/** - * 二维笛卡尔坐标系点 - * @author Rainbow Yang - */ -data class Point2D(val x: Double, val y: Double) : Point { - - constructor(x: Number, y: Number) : this(x.toDouble(), y.toDouble()) - constructor(point: PointAxes) : this(point[0], point[1]) - - override val asAxes by lazy { PointAxes(x, y) } - - val angle get() = Math.atan2(y, x) - - /** - * 逆时针旋转[angle]【弧度】 - */ - fun spin(angle: Double) = asPoint2DPolar.spin(angle).asPoint2D - -} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DBiangular.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DBiangular.kt deleted file mode 100644 index a2092b1..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DBiangular.kt +++ /dev/null @@ -1,15 +0,0 @@ -package moe.rainbowyang.math.point - -import moe.rainbowyang.math.line.Line - -/** - * 二维双角坐标点 - * @author Rainbow Yang - */ -data class Point2DBiangular(val angle1: Double, val angle2: Double, val a: Double) : Point { - - override val asAxes by lazy { - (Line(Point2D(a, 0), angle1) crossTo Line(Point2D(-a, 0), angle2)).asAxes - } - -} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolar.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolar.kt deleted file mode 100644 index 76cbce4..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolar.kt +++ /dev/null @@ -1,20 +0,0 @@ -package moe.rainbowyang.math.point - -import kotlin.math.* - -/** - * 二维双极坐标点 - * @author Rainbow Yang - */ -data class Point2DBipolar(val σ: Double, val τ: Double, val a: Double) : Point { - - private val c1 get() = σ - private val c2 get() = τ - - override val asAxes by lazy { - val base = a / (cosh(c2) - cos(c1)) - PointAxes(sinh(c2) * base, sin(c1) * base) - } - -} - diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolarCenter.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolarCenter.kt deleted file mode 100644 index e707011..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DBipolarCenter.kt +++ /dev/null @@ -1,18 +0,0 @@ -package moe.rainbowyang.math.point - -import kotlin.math.* - -/** - * 二维双心坐标点 - * @author Rainbow Yang - */ -data class Point2DBipolarCenter(val r1: Double, val r2: Double, val a: Double) : Point { - - override val asAxes by lazy { - val d = (r1 * r1 - r2 * r2) - val x = d / (4 * a) - val y = sqrt(16 * a * a * r1 * r1 - (d + 4 * a * a).pow(2.0)) / (4 * a) - PointAxes(x, y) - } - -} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DElliptic.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DElliptic.kt deleted file mode 100644 index 2b20a60..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DElliptic.kt +++ /dev/null @@ -1,18 +0,0 @@ -package moe.rainbowyang.math.point - -import kotlin.math.* - -/** - * 二维椭圆坐标点 - * @author Rainbow Yang - */ -data class Point2DElliptic(val μ: Double, val ν: Double, val a: Double) : Point { - - private val c1 get() = μ - private val c2 get() = ν - - override val asAxes by lazy { - PointAxes(a * cosh(c1) * cos(c2), a * sinh(c1) * sin(c2)) - } - -} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DHyperbolic.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DHyperbolic.kt deleted file mode 100644 index 909de9e..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DHyperbolic.kt +++ /dev/null @@ -1,40 +0,0 @@ -package moe.rainbowyang.math.point - -import kotlin.math.* - -/** - * 二维双曲坐标点 - * @author Rainbow Yang - */ -data class Point2DHyperbolic(val u: Double, val v: Double) : Point { - - companion object { - operator fun invoke(from: PointAxes): Point2DHyperbolic { - val (x, y) = from - return Point2DHyperbolic(-0.5 * ln(y / x), sqrt(x * y)) - } - } - - override val asAxes by lazy { PointAxes(v * E.pow(u), v * -E.pow(u)) } - - override fun toString() = "Point2DHyperbolic(u=$u, v=$v)" - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other?.javaClass != javaClass) return false - - other as Point2DHyperbolic - - if (u != other.u) return false - if (v != other.v) return false - - return true - } - - override fun hashCode(): Int { - var result = u.hashCode() - result = 31 * result + v.hashCode() - return result - } - -} diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DParabolic.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DParabolic.kt deleted file mode 100644 index b674bf2..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DParabolic.kt +++ /dev/null @@ -1,49 +0,0 @@ -package moe.rainbowyang.math.point - -import kotlin.math.sqrt - -/** - * 二维抛物线坐标点 - * @author Rainbow Yang - */ -class Point2DParabolic(val σ: Double, val τ: Double) : Point { - - operator fun component1() = σ - operator fun component2() = τ - - private val c1 get() = σ - private val c2 get() = τ - - companion object { - operator fun invoke(form: PointAxes): Point2DParabolic { - val (x, y) = form - return Point2DParabolic(sqrt(-y + sqrt(x * x + y * y)), sqrt(y + sqrt(x * x + y * y))) - } - } - - override val asAxes by lazy { PointAxes(c1 * c2, 0.5 * (c1 * c1 + c2 * c2)) } - - override fun toString(): String { - return "Point2DParabolic(σ=$σ, τ=$τ)" - } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other?.javaClass != javaClass) return false - - other as Point2DParabolic - - if (σ != other.σ) return false - if (τ != other.τ) return false - - return true - } - - override fun hashCode(): Int { - var result = σ.hashCode() - result = 31 * result + τ.hashCode() - return result - } - -} - diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point2DPolar.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point2DPolar.kt deleted file mode 100644 index 0b4f32d..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point2DPolar.kt +++ /dev/null @@ -1,37 +0,0 @@ -package moe.rainbowyang.math.point - -import kotlin.math.* - -/** - * 二维极坐标点 - * @author Rainbow Yang - */ - -data class Point2DPolar(val radius: Double, val angle: Double) : Point { - - constructor(radius: Number, angle: Number) : this(radius.toDouble(), angle.toDouble()) - - companion object { - operator fun invoke(form: Point): Point2DPolar { - val pd = form.asPoint2D - return Point2DPolar(pd.length, pd.angle) - } - } - - override val asAxes by lazy { PointAxes(radius * cos(angle), radius * sin(angle)) } - - /** - * 逆时针旋转[angle]【弧度】 - */ - fun spin(angle: Number) = Point2DPolar(radius, this.angle + angle.toDouble()) - - override fun equals(other: Any?): Boolean { - if (super.equals(other)) return true - - if (this.asAxes != (other as Point2DPolar).asAxes) return false - - return true - } - -} - diff --git a/src/main/kotlin/moe/rainbowyang/math/point/Point3D.kt b/src/main/kotlin/moe/rainbowyang/math/point/Point3D.kt deleted file mode 100644 index be0a433..0000000 --- a/src/main/kotlin/moe/rainbowyang/math/point/Point3D.kt +++ /dev/null @@ -1,27 +0,0 @@ -package moe.rainbowyang.math.point - -/** - * 三维轴坐标点 - * @author Rainbow Yang - */ -data class Point3D(val x: Double = 0.0, val y: Double = 0.0, val z: Double = 0.0) : Point { - - constructor(x: Number = 0, y: Number = 0, z: Number = 0) : this(x.toDouble(), y.toDouble(), z.toDouble()) - - companion object { - operator fun invoke(form: Point): Point3D { - val (x, y, z) = form.asAxes - return Point3D(x, y, z) - } - } - - override val asAxes by lazy { PointAxes(x, y, z) } - - fun spinAtXY(angle: Number) = asAxes.spinAtAndNew(0, 1, angle) - fun spinAtXZ(angle: Number) = asAxes.spinAtAndNew(0, 2, angle) - fun spinAtYX(angle: Number) = asAxes.spinAtAndNew(1, 0, angle) - fun spinAtYZ(angle: Number) = asAxes.spinAtAndNew(1, 2, angle) - fun spinAtZX(angle: Number) = asAxes.spinAtAndNew(2, 0, angle) - fun spinAtZY(angle: Number) = asAxes.spinAtAndNew(2, 1, angle) - -}