Created
September 1, 2024 10:07
-
-
Save hamurcuabi/159ee1304cb9ce800126e65e817efad1 to your computer and use it in GitHub Desktop.
Bogo with kmp,compose,canvas
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
| package com.hamurcuabi.bogo | |
| import androidx.compose.foundation.Canvas | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.Column | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.fillMaxWidth | |
| import androidx.compose.material.MaterialTheme | |
| import androidx.compose.runtime.* | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.geometry.Offset | |
| import androidx.compose.ui.graphics.Color | |
| import kotlinx.coroutines.delay | |
| import org.jetbrains.compose.ui.tooling.preview.Preview | |
| import kotlin.random.Random | |
| @Composable | |
| @Preview | |
| fun App() { | |
| MaterialTheme { | |
| Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { | |
| BogoSortVisualizer() | |
| } | |
| } | |
| } | |
| @Composable | |
| fun BogoSortVisualizer() { | |
| val list = remember { | |
| mutableStateListOf( | |
| 34 to getRandomColor(), | |
| 12 to getRandomColor(), | |
| 78 to getRandomColor(), | |
| 40 to getRandomColor(), | |
| 50 to getRandomColor() | |
| ) | |
| } | |
| LaunchedEffect(true) { | |
| bogoSort(list) | |
| } | |
| Box( | |
| contentAlignment = Alignment.Center, | |
| modifier = Modifier | |
| .fillMaxSize() | |
| ) { | |
| Canvas( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| ) { | |
| val padding = 16f | |
| val sizeWidth = size.width | |
| val sizeHeight = size.height | |
| val rectWidth = (sizeWidth / list.size) - padding | |
| val maxNumber = list.maxBy { it.first }.first.toFloat() | |
| list.forEachIndexed { index, item -> | |
| val rectHeight = (item.first / maxNumber) * sizeHeight | |
| val newSize = size.copy(rectWidth, rectHeight) | |
| var x = (rectWidth + padding) * index | |
| val y = (sizeHeight - newSize.height).toFloat() | |
| drawRoundRect( | |
| color = item.second, | |
| size = newSize, | |
| topLeft = Offset(x = x, y = y) | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| fun getRandomColor(): Color { | |
| return Color( | |
| red = Random.nextFloat(), | |
| green = Random.nextFloat(), | |
| blue = Random.nextFloat(), | |
| alpha = 1.0f | |
| ) | |
| } | |
| suspend fun bogoSort(list: MutableList<Pair<Int, Color>>) { | |
| while (!isSorted(list.map { it.first })) { | |
| list.shuffle() | |
| delay(50) | |
| } | |
| } | |
| fun isSorted(list: List<Int>): Boolean { | |
| return list.zipWithNext().all { (a, b) -> a <= b } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment