11package com.smarttoolfactory.cropper.util
22
3+ import android.graphics.Matrix
34import androidx.compose.foundation.shape.GenericShape
45import androidx.compose.ui.geometry.Offset
56import androidx.compose.ui.geometry.Rect
67import androidx.compose.ui.geometry.Size
78import androidx.compose.ui.graphics.Path
9+ import androidx.compose.ui.graphics.asAndroidPath
810import androidx.compose.ui.unit.LayoutDirection
911import com.smarttoolfactory.cropper.model.AspectRatio
1012import kotlin.math.cos
@@ -26,6 +28,7 @@ import kotlin.math.sin
2628 * ```
2729 */
2830fun createPolygonPath (cx : Float , cy : Float , sides : Int , radius : Float ): Path {
31+
2932 val angle = 2.0 * Math .PI / sides
3033
3134 return Path ().apply {
@@ -43,6 +46,28 @@ fun createPolygonPath(cx: Float, cy: Float, sides: Int, radius: Float): Path {
4346 }
4447}
4548
49+
50+ /* *
51+ * Create a polygon shape
52+ */
53+ fun createPolygonShape (sides : Int , degrees : Float = 0f): GenericShape {
54+ return GenericShape { size: Size , _: LayoutDirection ->
55+
56+ val radius = size.width.coerceAtMost(size.height) / 2
57+ addPath(
58+ createPolygonPath(
59+ cx = size.width / 2 ,
60+ cy = size.height / 2 ,
61+ sides = sides,
62+ radius = radius
63+ )
64+ )
65+ val matrix = Matrix ()
66+ matrix.postRotate(degrees, size.width / 2 , size.height / 2 )
67+ this .asAndroidPath().transform(matrix)
68+ }
69+ }
70+
4671/* *
4772 * Creates a [Rect] shape with given aspect ratio.
4873 */
0 commit comments