Extract polygon

This commit is contained in:
Oleksandr Balan
2022-05-14 22:43:52 +02:00
parent 81d8898d24
commit b00345dd86
4 changed files with 51 additions and 48 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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)
}

View 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
}