Skip to content

Instantly share code, notes, and snippets.

@Oleur
Created April 23, 2021 18:53
Show Gist options
  • Save Oleur/032fe1320bc886110e7dd6c6b85b6b4b to your computer and use it in GitHub Desktop.
Save Oleur/032fe1320bc886110e7dd6c6b85b6b4b to your computer and use it in GitHub Desktop.
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