Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next & Kotlin 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed May 14, 2021
1 parent 25d5c0f commit 6bb51f7
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 83 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# sytleguide
kotlin.code.style=official

# Kotlin 1.4.31: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
easyPluginVersion=0.14.3
# Kotlin 1.5.0: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
easyPluginVersion=0.15.2

# version
group=com.soywiz.korlibs.korma
version=2.0.0-SNAPSHOT

# korlibs
kdsVersion=2.0.9
kdsVersion=2.1.1

# bintray location
project.bintray.org=korlibs
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
104 changes: 58 additions & 46 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/Angle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,80 @@ import kotlin.math.PI
import kotlin.math.absoluteValue
import kotlin.math.atan2

inline class Angle(val radians: Double) : Comparable<Angle> {
override fun toString(): String = "$degrees.degrees"
@PublishedApi internal const val PI2 = PI * 2

@Suppress("MemberVisibilityCanBePrivate")
companion object {
val ZERO = Angle(0.0)
@PublishedApi internal const val DEG2RAD = PI / 180.0
@PublishedApi internal const val RAD2DEG = 180.0 / PI

fun fromRadians(radians: Double) = Angle(radians)
fun fromDegrees(degrees: Double) = Angle(degreesToRadians(degrees))
@PublishedApi internal const val MAX_DEGREES = 360.0
@PublishedApi internal const val MAX_RADIANS = PI2

fun fromRadians(radians: Float) = fromRadians(radians.toDouble())
fun fromDegrees(degrees: Float) = fromDegrees(degrees.toDouble())
@PublishedApi internal const val HALF_DEGREES = MAX_DEGREES / 2.0
@PublishedApi internal const val HALF_RADIANS = MAX_RADIANS / 2.0

fun fromRadians(radians: Int) = fromRadians(radians.toDouble())
fun fromDegrees(degrees: Int) = fromDegrees(degrees.toDouble())
@PublishedApi internal fun Angle_shortDistanceTo(from: Angle, to: Angle): Angle {
val r0 = from.radians umod MAX_RADIANS
val r1 = to.radians umod MAX_RADIANS
val diff = (r1 - r0 + HALF_RADIANS) % MAX_RADIANS - HALF_RADIANS
return if (diff < -HALF_RADIANS) Angle(diff + MAX_RADIANS) else Angle(diff)
}

internal const val PI2 = PI * 2
@PublishedApi internal fun Angle_longDistanceTo(from: Angle, to: Angle): Angle {
val short = Angle_shortDistanceTo(from, to)
return when {
short == 0.radians -> 0.radians
short < 0.radians -> 360.degrees + short
else -> (-360).degrees + short
}
}

internal const val DEG2RAD = PI / 180.0
internal const val RAD2DEG = 180.0 / PI
@PublishedApi internal fun Angle_between(x0: Double, y0: Double, x1: Double, y1: Double): Angle {
val angle = atan2(y1 - y0, x1 - x0)
return if (angle < 0) Angle(angle + PI2) else Angle(angle)
}

internal const val MAX_DEGREES = 360.0
internal const val MAX_RADIANS = PI2
inline class Angle(val radians: Double) : Comparable<Angle> {
override fun toString(): String = "$degrees.degrees"

internal const val HALF_DEGREES = MAX_DEGREES / 2.0
internal const val HALF_RADIANS = MAX_RADIANS / 2.0
@Suppress("MemberVisibilityCanBePrivate")
companion object {
inline val ZERO get() = Angle(0.0)

inline fun fromRadians(radians: Double) = Angle(radians)
inline fun fromDegrees(degrees: Double) = Angle(degreesToRadians(degrees))

fun cos01(ratio: Double) = kotlin.math.cos(PI2 * ratio)
fun sin01(ratio: Double) = kotlin.math.sin(PI2 * ratio)
fun tan01(ratio: Double) = kotlin.math.tan(PI2 * ratio)
inline fun fromRadians(radians: Float) = fromRadians(radians.toDouble())
inline fun fromDegrees(degrees: Float) = fromDegrees(degrees.toDouble())

fun degreesToRadians(degrees: Double): Double = degrees * DEG2RAD
fun radiansToDegrees(radians: Double): Double = radians * RAD2DEG
inline fun fromRadians(radians: Int) = fromRadians(radians.toDouble())
inline fun fromDegrees(degrees: Int) = fromDegrees(degrees.toDouble())

fun shortDistanceTo(from: Angle, to: Angle): Angle {
val r0 = from.radians umod MAX_RADIANS
val r1 = to.radians umod MAX_RADIANS
val diff = (r1 - r0 + HALF_RADIANS) % MAX_RADIANS - HALF_RADIANS
return if (diff < -HALF_RADIANS) Angle(diff + MAX_RADIANS) else Angle(diff)
}
inline fun cos01(ratio: Double) = kotlin.math.cos(PI2 * ratio)
inline fun sin01(ratio: Double) = kotlin.math.sin(PI2 * ratio)
inline fun tan01(ratio: Double) = kotlin.math.tan(PI2 * ratio)

fun longDistanceTo(from: Angle, to: Angle): Angle {
val short = shortDistanceTo(from, to)
return when {
short == ZERO -> ZERO
short < ZERO -> 360.degrees + short
else -> (-360).degrees + short
}
}
inline fun degreesToRadians(degrees: Double): Double = degrees * DEG2RAD
inline fun radiansToDegrees(radians: Double): Double = radians * RAD2DEG

fun between(x0: Double, y0: Double, x1: Double, y1: Double): Angle {
val angle = atan2(y1 - y0, x1 - x0)
return if (angle < 0) Angle(angle + PI2) else Angle(angle)
}
inline fun shortDistanceTo(from: Angle, to: Angle): Angle = Angle_shortDistanceTo(from, to)
inline fun longDistanceTo(from: Angle, to: Angle): Angle = Angle_longDistanceTo(from, to)
inline fun between(x0: Double, y0: Double, x1: Double, y1: Double): Angle = Angle_between(x0, y0, x1, y1)

fun between(x0: Int, y0: Int, x1: Int, y1: Int): Angle = between(x0.toDouble(), y0.toDouble(), x1.toDouble(), y1.toDouble())
fun between(x0: Float, y0: Float, x1: Float, y1: Float): Angle = between(x0.toDouble(), y0.toDouble(), x1.toDouble(), y1.toDouble())
inline fun between(x0: Int, y0: Int, x1: Int, y1: Int): Angle = between(x0.toDouble(), y0.toDouble(), x1.toDouble(), y1.toDouble())
inline fun between(x0: Float, y0: Float, x1: Float, y1: Float): Angle = between(x0.toDouble(), y0.toDouble(), x1.toDouble(), y1.toDouble())

fun between(p0: IPoint, p1: IPoint): Angle = between(p0.x, p0.y, p1.x, p1.y)
inline fun between(p0: IPoint, p1: IPoint): Angle = between(p0.x, p0.y, p1.x, p1.y)
}

override fun compareTo(other: Angle): Int = this.radians.compareTo(other.radians)
override fun compareTo(other: Angle): Int {
//return this.radians.compareTo(other.radians) // @TODO: Double.compareTo calls EnterFrame/LeaveFrame! because it uses a Double companion object
val left = this.radians
val right = other.radians
// @TODO: Handle infinite/NaN? Though usually this won't happen
if (left < right) return -1
if (left > right) return +1
return 0
}
}

inline fun cos(angle: Angle): Double = kotlin.math.cos(angle.radians)
Expand Down Expand Up @@ -126,6 +138,6 @@ val Int.radians get() = Angle.fromRadians(this)
val Float.degrees get() = Angle.fromDegrees(this)
val Float.radians get() = Angle.fromRadians(this)

val Angle.normalized get() = Angle(radians umod Angle.MAX_RADIANS)
val Angle.normalized get() = Angle(radians umod MAX_RADIANS)

fun Double.interpolate(l: Angle, r: Angle): Angle = this.interpolate(l.radians, r.radians).radians
12 changes: 9 additions & 3 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/Matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ data class Matrix(

operator fun times(that: Matrix): Matrix = Matrix().multiply(this, that)

fun toTransform(out: Transform = Transform()): Transform = out.setMatrix(this)
fun toTransform(out: Transform = Transform()): Transform {
out.setMatrixNoReturn(this)
return out
}

// Transform points
fun transform(p: IPoint, out: Point = Point()): Point = transform(p.x, p.y, out)
Expand Down Expand Up @@ -379,7 +382,7 @@ data class Matrix(
rotation = 0.0.radians
}

fun setMatrix(matrix: Matrix): Transform {
fun setMatrixNoReturn(matrix: Matrix) {
val PI_4 = PI / 4.0
this.x = matrix.tx
this.y = matrix.ty
Expand All @@ -403,7 +406,10 @@ data class Matrix(
} else {
this.rotation = 0.radians
}
}

fun setMatrix(matrix: Matrix): Transform {
setMatrixNoReturn(matrix)
return this
}

Expand Down Expand Up @@ -448,7 +454,7 @@ data class Matrix(

class Computed(val matrix: Matrix, val transform: Transform) {
companion object;
constructor(matrix: Matrix) : this(matrix, Transform().setMatrix(matrix))
constructor(matrix: Matrix) : this(matrix, Transform().also { it.setMatrixNoReturn(matrix) })
constructor(transform: Transform) : this(transform.toMatrix(), transform)
}

Expand Down
1 change: 1 addition & 0 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/Point.kt
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ inline class PointInt(val p: Point) : IPointInt, Comparable<IPointInt> {
companion object {
operator fun invoke(): PointInt = PointInt(0, 0)
operator fun invoke(x: Int, y: Int): PointInt = PointInt(Point(x, y))
operator fun invoke(that: IPointInt): PointInt = PointInt(Point(that.x, that.y))

fun compare(lx: Int, ly: Int, rx: Int, ry: Int): Int {
val ret = ly.compareTo(ry)
Expand Down
57 changes: 32 additions & 25 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/Rectangle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import com.soywiz.korma.internal.*
import com.soywiz.korma.interpolation.*

interface IRectangle {
val _x: Double
val _y: Double
val _width: Double
val _height: Double
val x: Double
val y: Double
val width: Double
val height: Double

companion object {
inline operator fun invoke(x: Double, y: Double, width: Double, height: Double): IRectangle = Rectangle(x, y, width, height)
Expand All @@ -16,29 +16,35 @@ interface IRectangle {
}
}

val IRectangle.x get() = _x
val IRectangle.y get() = _y
val IRectangle.width get() = _width
val IRectangle.height get() = _height
@Deprecated("Properties with underscores are deprecated and will be removed soon", ReplaceWith("x"), DeprecationLevel.ERROR)
val IRectangle._x get() = x
@Deprecated("Properties with underscores are deprecated and will be removed soon", ReplaceWith("y"), DeprecationLevel.ERROR)
val IRectangle._y get() = y
@Deprecated("Properties with underscores are deprecated and will be removed soon", ReplaceWith("width"), DeprecationLevel.ERROR)
val IRectangle._width get() = width
@Deprecated("Properties with underscores are deprecated and will be removed soon", ReplaceWith("height"), DeprecationLevel.ERROR)
val IRectangle._height get() = height

val IRectangle.left get() = _x
val IRectangle.top get() = _y
val IRectangle.right get() = _x + _width
val IRectangle.bottom get() = _y + _height
val IRectangle.left get() = x
val IRectangle.top get() = y
val IRectangle.right get() = x + width
val IRectangle.bottom get() = y + height

val IRectangle.topLeft get() = Point(left, top)
val IRectangle.topRight get() = Point(right, top)
val IRectangle.bottomLeft get() = Point(left, bottom)
val IRectangle.bottomRight get() = Point(right, bottom)

operator fun IRectangle.contains(that: IPoint) = contains(that.x, that.y)
operator fun IRectangle.contains(that: IPointInt) = contains(that.x, that.y)
fun IRectangle.contains(x: Double, y: Double) = (x >= left && x < right) && (y >= top && y < bottom)
fun IRectangle.contains(x: Float, y: Float) = contains(x.toDouble(), y.toDouble())
fun IRectangle.contains(x: Int, y: Int) = contains(x.toDouble(), y.toDouble())

data class Rectangle(
var x: Double, var y: Double,
var width: Double, var height: Double
override var x: Double, override var y: Double,
override var width: Double, override var height: Double
) : MutableInterpolable<Rectangle>, Interpolable<Rectangle>, IRectangle, Sizeable {
override val _x: Double get() = x
override val _y: Double get() = y
override val _width: Double get() = width
override val _height: Double get() = height

companion object {
operator fun invoke(): Rectangle = Rectangle(0.0, 0.0, 0.0, 0.0)
Expand Down Expand Up @@ -90,11 +96,6 @@ data class Rectangle(
operator fun div(scale: Int) = this / scale.toDouble()

operator fun contains(that: Rectangle) = isContainedIn(that, this)
operator fun contains(that: Point) = contains(that.x, that.y)
operator fun contains(that: IPoint) = contains(that.x, that.y)
fun contains(x: Double, y: Double) = (x >= left && x < right) && (y >= top && y < bottom)
fun contains(x: Float, y: Float) = contains(x.toDouble(), y.toDouble())
fun contains(x: Int, y: Int) = contains(x.toDouble(), y.toDouble())

infix fun intersects(that: Rectangle): Boolean = intersectsX(that) && intersectsY(that)

Expand Down Expand Up @@ -258,6 +259,7 @@ inline class RectangleInt(val rect: Rectangle) : IRectangleInt {
operator fun invoke(x: Int, y: Int, width: Int, height: Int) = RectangleInt(Rectangle(x, y, width, height))
operator fun invoke(x: Float, y: Float, width: Float, height: Float) = RectangleInt(Rectangle(x, y, width, height))
operator fun invoke(x: Double, y: Double, width: Double, height: Double) = RectangleInt(Rectangle(x, y, width, height))
operator fun invoke(other: IRectangleInt) = RectangleInt(Rectangle(other.x, other.y, other.width, other.height))

fun fromBounds(left: Int, top: Int, right: Int, bottom: Int): RectangleInt =
RectangleInt(left, top, right - left, bottom - top)
Expand All @@ -266,7 +268,7 @@ inline class RectangleInt(val rect: Rectangle) : IRectangleInt {
override fun toString(): String = "Rectangle(x=$x, y=$y, width=$width, height=$height)"
}

fun RectangleInt.setTo(that: RectangleInt) = setTo(that.x, that.y, that.width, that.height)
fun RectangleInt.setTo(that: IRectangleInt) = setTo(that.x, that.y, that.width, that.height)

fun RectangleInt.setTo(x: Int, y: Int, width: Int, height: Int): RectangleInt {
this.x = x
Expand Down Expand Up @@ -302,6 +304,11 @@ fun RectangleInt.setBoundsTo(left: Int, top: Int, right: Int, bottom: Int) = set
////////////////////

operator fun IRectangleInt.contains(v: SizeInt): Boolean = (v.width <= width) && (v.height <= height)
operator fun IRectangleInt.contains(that: IPoint) = contains(that.x, that.y)
operator fun IRectangleInt.contains(that: IPointInt) = contains(that.x, that.y)
fun IRectangleInt.contains(x: Double, y: Double) = (x >= left && x < right) && (y >= top && y < bottom)
fun IRectangleInt.contains(x: Float, y: Float) = contains(x.toDouble(), y.toDouble())
fun IRectangleInt.contains(x: Int, y: Int) = contains(x.toDouble(), y.toDouble())

fun IRectangleInt.anchoredIn(container: RectangleInt, anchor: Anchor, out: RectangleInt = RectangleInt()): RectangleInt =
out.setTo(
Expand All @@ -317,7 +324,7 @@ fun IRectangleInt.getAnchorPosition(anchor: Anchor, out: PointInt = PointInt()):
fun Rectangle.asInt() = RectangleInt(this)
fun RectangleInt.asDouble() = this.rect

val IRectangle.int get() = RectangleInt(_x, _y, _width, _height)
val IRectangle.int get() = RectangleInt(x, y, width, height)
val IRectangleInt.float get() = Rectangle(x, y, width, height)

fun IRectangleInt.anchor(ax: Double, ay: Double): PointInt =
Expand Down
7 changes: 6 additions & 1 deletion korma/src/commonMain/kotlin/com/soywiz/korma/geom/Size.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,17 @@ inline class Size(val p: Point) : MutableInterpolable<Size>, Interpolable<Size>,
interface ISizeInt {
val width: Int
val height: Int

companion object {
operator fun invoke(width: Int, height: Int): ISizeInt = SizeInt(width, height)
}
}

inline class SizeInt(val size: Size) : ISizeInt {
companion object {
operator fun invoke(): SizeInt = SizeInt(Size(0, 0))
operator fun invoke(x: Int, y: Int): SizeInt = SizeInt(Size(x, y))
operator fun invoke(that: ISizeInt): SizeInt = SizeInt(Size(that.width, that.height))
}

fun clone() = SizeInt(size.clone())
Expand All @@ -94,7 +99,7 @@ fun SizeInt.setTo(width: Int, height: Int) : SizeInt {
return this
}

fun SizeInt.setTo(that: SizeInt) = setTo(that.width, that.height)
fun SizeInt.setTo(that: ISizeInt) = setTo(that.width, that.height)

fun SizeInt.setToScaled(sx: Double, sy: Double) = setTo((this.width * sx).toInt(), (this.height * sy).toInt())
fun SizeInt.setToScaled(sx: Int, sy: Int) = setToScaled(sx.toDouble(), sy.toDouble())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import kotlin.math.*
internal val Float.niceStr: String get() = if (almostEquals(this.toLong().toFloat(), this)) "${this.toLong()}" else "$this"
internal val Double.niceStr: String get() = if (almostEquals(this.toLong().toDouble(), this)) "${this.toLong()}" else "$this"

internal infix fun Double.umod(other: Double): Double {
@PublishedApi internal infix fun Double.umod(other: Double): Double {
val remainder = this % other
return when {
remainder < 0 -> remainder + other
else -> remainder
}
}

internal infix fun Float.umod(other: Float): Float {
@PublishedApi internal infix fun Float.umod(other: Float): Float {
val remainder = this % other
return when {
remainder < 0 -> remainder + other
Expand Down
Loading

0 comments on commit 6bb51f7

Please sign in to comment.