Created
January 27, 2025 08:20
-
-
Save Rohit-554/23728cb7e4df45cab13b72032be4ee6a 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
| @Composable | |
| fun QuadraticBezierCurve( | |
| modifier: Modifier = Modifier, | |
| startPoint: Offset = Offset(50f, 200f), | |
| controlPoint: Offset = Offset(200f, 50f), | |
| endPoint: Offset = Offset(350f, 200f) | |
| ) { | |
| var currentStartPoint by remember { mutableStateOf(startPoint) } | |
| var currentControlPoint by remember { mutableStateOf(controlPoint) } | |
| var currentEndPoint by remember { mutableStateOf(endPoint) } | |
| var draggedPoint by remember { mutableStateOf<String?>(null) } | |
| Canvas( | |
| modifier = modifier | |
| .size(400.dp) | |
| .background(Color.White) | |
| .pointerInput(Unit) { | |
| detectDragGestures( | |
| onDragStart = { offset -> | |
| draggedPoint = when { | |
| (offset - currentStartPoint).getDistance() < 20f -> "start" | |
| (offset - currentControlPoint).getDistance() < 20f -> "control" | |
| (offset - currentEndPoint).getDistance() < 20f -> "end" | |
| else -> null | |
| } | |
| }, | |
| onDrag = { change, dragAmount -> | |
| change.consume() | |
| when (draggedPoint) { | |
| "start" -> currentStartPoint += dragAmount | |
| "control" -> currentControlPoint += dragAmount | |
| "end" -> currentEndPoint += dragAmount | |
| } | |
| }, | |
| onDragEnd = { draggedPoint = null } | |
| ) | |
| } | |
| ) { | |
| // Draw control lines | |
| drawLine( | |
| start = currentStartPoint, | |
| end = currentControlPoint, | |
| color = Color.LightGray, | |
| strokeWidth = 2f, | |
| pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f)) | |
| ) | |
| drawLine( | |
| start = currentControlPoint, | |
| end = currentEndPoint, | |
| color = Color.LightGray, | |
| strokeWidth = 2f, | |
| pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f)) | |
| ) | |
| // Draw Bezier curve | |
| val path = Path().apply { | |
| moveTo(currentStartPoint.x, currentStartPoint.y) | |
| quadraticBezierTo( | |
| currentControlPoint.x, currentControlPoint.y, | |
| currentEndPoint.x, currentEndPoint.y | |
| ) | |
| } | |
| drawPath( | |
| path = path, | |
| color = Color.Blue, | |
| style = Stroke(width = 4f) | |
| ) | |
| // Draw points | |
| drawPoints( | |
| points = listOf(currentStartPoint, currentEndPoint), | |
| pointMode = PointMode.Points, | |
| color = Color.Blue, | |
| strokeWidth = 16f | |
| ) | |
| drawCircle( | |
| color = Color.Red, | |
| radius = 8f, | |
| center = currentControlPoint | |
| ) | |
| } | |
| } | |
| @Preview | |
| @Composable | |
| fun PreviewBezierCurve() { | |
| QuadraticBezierCurve( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| .background(Color.White) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment