mirror of
https://github.com/fankes/pagecurl-multiplatform.git
synced 2025-09-06 02:35:25 +08:00
Extract polygon
This commit is contained in:
@@ -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,
|
||||
|
@@ -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,
|
||||
|
@@ -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<Offset>) {
|
||||
|
||||
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)
|
||||
}
|
||||
|
47
pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/Polygon.kt
Normal file
47
pagecurl/src/main/kotlin/eu/wewox/pagecurl/utils/Polygon.kt
Normal file
@@ -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<Offset>) {
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user