Created
April 23, 2021 18:53
-
-
Save Oleur/032fe1320bc886110e7dd6c6b85b6b4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PolyShape(private val sides: Int, private val radius: Float) : Shape { | |
override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density): Outline { | |
return Outline.Generic(path = Path().apply { polygon(sides, radius, size.center) }) | |
} | |
} | |
fun Path.polygon(sides: Int, radius: Float, center: Offset) { | |
val angle = 2.0 * Math.PI / sides | |
moveTo( | |
x = center.x + (radius * cos(0.0)).toFloat(), | |
y = center.y + (radius * sin(0.0)).toFloat() | |
) | |
for (i in 1 until sides) { | |
lineTo( | |
x = center.x + (radius * cos(angle * i)).toFloat(), | |
y = center.y + (radius * sin(angle * i)).toFloat() | |
) | |
} | |
close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment