All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.math.geometry.Point.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.math.geometry

import fuookami.ospf.kotlin.utils.math.*
import fuookami.ospf.kotlin.utils.operator.*

data class Point(
    val position: List,
    val dim: D
) : Plus, Point>, Minus, Point> {
    companion object {
        operator fun invoke(x: Flt64, y: Flt64): Point2 {
            return Point(listOf(x, y), Dim2)
        }

        operator fun invoke(x: Flt64, y: Flt64, z: Flt64): Point3 {
            return Point(listOf(x, y, z), Dim3)
        }

        operator fun  invoke(vector: Vector): Point {
            return Point(vector.vector, vector.dim)
        }
    }

    init {
        assert(position.size == dim.size)
    }

    val size by dim::size
    val indices by dim::indices

    @Throws(ArrayIndexOutOfBoundsException::class)
    operator fun get(i: Int): Flt64 {
        return position[i]
    }

    infix fun distance(rhs: Point): Flt64 = Distance.Euclidean(this, rhs)
    fun distanceBetween(rhs: Point, type: Distance = Distance.Euclidean): Flt64 = type(this, rhs)

    override fun plus(rhs: Point) = Point(indices.map { this[it] + rhs[it] }, dim)
    override fun minus(rhs: Point) = Point(indices.map { this[it] - rhs[it] }, dim)

    operator fun plus(rhs: Vector) = Point(indices.map { this[it] + rhs[it] }, dim)
    operator fun minus(rhs: Vector) = Point(indices.map { this[it] - rhs[it] }, dim)

    override fun toString() = position.joinToString(",", "[", "]")
}

typealias Point2 = Point

@get:JvmName("Point2X")
val Point2.x get() = this[0]

@get:JvmName("Point2Y")
val Point2.y get() = this[1]

val originPoint2 = point2()

fun point2(x: Flt64 = Flt64.zero, y: Flt64 = Flt64.zero): Point2 {
    return Point2(x, y)
}

typealias Point3 = Point

@get:JvmName("Point3X")
val Point3.x get() = this[0]

@get:JvmName("Point3Y")
val Point3.y get() = this[1]

@get:JvmName("Point3Z")
val Point3.z get() = this[2]

val originPoint3 = point3()

fun point3(x: Flt64 = Flt64.zero, y: Flt64 = Flt64.zero, z: Flt64 = Flt64.zero): Point3 {
    return Point3(x, y, z)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy