diff --git a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlDraw.kt b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlDraw.kt index d36af07..2ef52c1 100644 --- a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlDraw.kt +++ b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlDraw.kt @@ -28,7 +28,7 @@ import java.lang.Float.max import kotlin.math.atan2 @ExperimentalPageCurlApi -public fun Modifier.drawCurl( +internal fun Modifier.drawCurl( config: CurlConfig = CurlConfig(), posA: Offset, posB: Offset, diff --git a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlGesture.kt b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlGesture.kt index e156e38..28b0d74 100644 --- a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlGesture.kt +++ b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/page/CurlGesture.kt @@ -18,7 +18,7 @@ import eu.wewox.pagecurl.utils.rotate import kotlin.math.PI @ExperimentalPageCurlApi -public fun Modifier.curlGesture( +internal fun Modifier.curlGesture( enabled: Boolean, direction: CurlDirection, onStart: () -> Unit, diff --git a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/MathUtils.kt b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/MathUtils.kt index 0bd9bcc..e2bf793 100644 --- a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/MathUtils.kt +++ b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/MathUtils.kt @@ -1,53 +1,9 @@ package eu.wewox.pagecurl.utils import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.graphics.Path import kotlin.math.cos import kotlin.math.sin -internal data class Polygon(val vertices: List) { - - private val size: Int = vertices.size - - fun translate(offset: Offset): Polygon = - Polygon(vertices.map { it + offset }) - - fun offset(value: Float): Polygon { - val edgeNormals = List(size) { - val edge = vertices[index(it + 1)] - vertices[index(it)] - Offset(edge.y, -edge.x).normalized() - } - - val vertexNormals = List(size) { - (edgeNormals[index(it - 1)] + edgeNormals[index(it)]).normalized() - } - - return Polygon( - vertices.mapIndexed { index, vertex -> - vertex + vertexNormals[index] * value - } - ) - } - - fun toPath(): Path = - Path().apply { - vertices.forEachIndexed { index, vertex -> - if (index == 0) { - moveTo(vertex.x, vertex.y) - } else { - lineTo(vertex.x, vertex.y) - } - } - } - - private fun index(i: Int) = ((i % size) + size) % size -} - -private fun Offset.normalized(): Offset { - val distance = getDistance() - return if (distance != 0f) this / distance else this -} - internal fun Offset.rotate(angle: Float): Offset { val sin = sin(angle) val cos = cos(angle) @@ -64,8 +20,8 @@ internal fun lineLineIntersection( if (denominator == 0f) return null val x = ((line1a.x * line1b.y - line1a.y * line1b.x) * (line2a.x - line2b.x) - - (line1a.x - line1b.x) * (line2a.x * line2b.y - line2a.y * line2b.x)) / denominator + (line1a.x - line1b.x) * (line2a.x * line2b.y - line2a.y * line2b.x)) / denominator val y = ((line1a.x * line1b.y - line1a.y * line1b.x) * (line2a.y - line2b.y) - - (line1a.y - line1b.y) * (line2a.x * line2b.y - line2a.y * line2b.x)) / denominator + (line1a.y - line1b.y) * (line2a.x * line2b.y - line2a.y * line2b.x)) / denominator return Offset(x, y) } diff --git a/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/Polygon.kt b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/Polygon.kt new file mode 100644 index 0000000..4cd7087 --- /dev/null +++ b/pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/Polygon.kt @@ -0,0 +1,47 @@ +package eu.wewox.pagecurl.utils + +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.Path + +internal data class Polygon(val vertices: List) { + + private val size: Int = vertices.size + + fun translate(offset: Offset): Polygon = + Polygon(vertices.map { it + offset }) + + fun offset(value: Float): Polygon { + val edgeNormals = List(size) { + val edge = vertices[index(it + 1)] - vertices[index(it)] + Offset(edge.y, -edge.x).normalized() + } + + val vertexNormals = List(size) { + (edgeNormals[index(it - 1)] + edgeNormals[index(it)]).normalized() + } + + return Polygon( + vertices.mapIndexed { index, vertex -> + vertex + vertexNormals[index] * value + } + ) + } + + fun toPath(): Path = + Path().apply { + vertices.forEachIndexed { index, vertex -> + if (index == 0) { + moveTo(vertex.x, vertex.y) + } else { + lineTo(vertex.x, vertex.y) + } + } + } + + private fun index(i: Int) = ((i % size) + size) % size +} + +private fun Offset.normalized(): Offset { + val distance = getDistance() + return if (distance != 0f) this / distance else this +}