Draw circle sector in Android

Today I had program a heading indicator for a Android map-view (osmdroid). My idea was to use a circle sector (similar to most map applications).

After some experiments, I came up with the following code. It uses a Path and basic trigonometric functions:

private fun Canvas.drawCircleSector(
    x: Float,
    y: Float,
    radius: Float,
    bearing: Int,
    width: Int,
    paint: Paint
) {
    val bearingMarker = Path()

    bearingMarker.moveTo(x, y)
    for (i in (-width / 2)..(width / 2)) {
        val rSin = sin((bearing + i) * Math.PI / 180)
        val rCos = cos((bearing + i) * Math.PI / 180)
        bearingMarker.lineTo(x + rSin.toFloat() * radius, y - rCos.toFloat() * radius)
    }
    bearingMarker.lineTo(x, y)

    this.drawPath(bearingMarker, paint)
}
Show Comments