Vivek Sharma
10/31/2021, 7:16 PMCanvas
and then rotating
it by angle 45f
, is there a way to get that end Offset
till the line is rotated
How can I get/access this Offset (?,?)
?Arsen
10/31/2021, 8:56 PMtheapache64
10/31/2021, 9:23 PMval x = nextOffset.x + lineLength * kotlin.math.sin(angle)
val y = nextOffset.y + lineLength * kotlin.math.cos(angle)
rather than rotating the canvas, you can use sin
and cos
to calculate the target x
and y
Here’s a full example
Canvas(
modifier = Modifier.fillMaxSize()
) {
var nextOffset = Offset(0f, 0f)
repeat(noOfLines) {
val angle = (0..360).random().toFloat()
val x = nextOffset.x + lineLength * kotlin.math.sin(angle)
val y = nextOffset.y + lineLength * kotlin.math.cos(angle)
val targetOffset = Offset(x, y)
drawLine(
color = colors.random(),
strokeWidth = 5f,
start = nextOffset,
end = targetOffset
)
nextOffset = targetOffset
}
}
Michael Paus
11/01/2021, 8:57 AMVivek Sharma
11/01/2021, 3:00 PM