From ae6876022c4777d2f440f2907afa7edf0fc453e1 Mon Sep 17 00:00:00 2001 From: cemal Date: Mon, 19 Feb 2024 14:31:01 +0100 Subject: [PATCH] Added glossary words, code words, intersection words to the Ven Diagram --- src/main/kotlin/main/Compare.kt | 104 ++++++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/main/Compare.kt b/src/main/kotlin/main/Compare.kt index 66b6000..d6d339d 100644 --- a/src/main/kotlin/main/Compare.kt +++ b/src/main/kotlin/main/Compare.kt @@ -25,7 +25,9 @@ import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.drawscope.drawIntoCanvas -import kotlin.math.max +import androidx.compose.ui.text.* +import androidx.compose.ui.unit.Constraints + @Composable fun compareResults( @@ -87,8 +89,8 @@ fun compareResults( if (!showTableAndLegend.value) { IntersectionCircles( glossaryWords.map { it.name }, - codeWords, - intersectionWords + glossaryWords, + codeWords ) } @@ -238,33 +240,111 @@ fun commonWordsDataRow( cellContent(if (isCommon) "Oui" else "Non", modifier) } } - +@OptIn(ExperimentalTextApi::class) @Composable -fun IntersectionCircles(glossaryWords: List, codeWords: List, glossaryCodeWords: List) { - val intersectionSize = glossaryCodeWords.size.coerceIn(0, 10) / 10f // Normalize to [0, 1] +fun IntersectionCircles( + glossaryCodeWords: List, + glossaryWords: List, + codeWords: List +) { + // Take up to 10 words for each category + val glossaryWordsSubset = glossaryWords.take(10).map { it.name } + val codeWordsSubset = codeWords.take(10) + val intersectionWords = glossaryCodeWords.take(10) - val circleRadius = 150f // Fixed radius for both circles + val intersectionSize = intersectionWords.size / 10f // Normalize to [0, 1] + + val circleRadius = 300f // Fixed radius for both circles val circleDistance = (1 - intersectionSize) * 2 * circleRadius // Distance between the centers of the circles - Canvas(modifier = Modifier.size(300.dp)) { + val textMeasurer = rememberTextMeasurer() + + Canvas(modifier = Modifier.size(500.dp)) { // Draw glossary circle - drawCircle(color = Color(0xFFA1C084), center = Offset(x = size.width / 2 - circleDistance / 2, y = size.height / 2), radius = circleRadius) + drawCircle( + color = Color(0xFFA1C084), + center = Offset(x = size.width / 2 - circleDistance / 2, y = size.height / 2), + radius = circleRadius + ) // Draw code circle - drawCircle(color = Color(0xFFD9B504), center = Offset(x = size.width / 2 + circleDistance / 2, y = size.height / 2), radius = circleRadius) + drawCircle( + color = Color(0xFFE9C46A), + center = Offset(x = size.width / 2 + circleDistance / 2, y = size.height / 2), + radius = circleRadius + ) + + // Draw intersection with clipping drawIntoCanvas { canvas -> val path = Path().apply { - addOval(Rect(center = Offset(x = size.width / 2 - circleDistance / 2, y = size.height / 2), radius = circleRadius)) + addOval( + Rect( + center = Offset(x = size.width / 2 - circleDistance / 2, y = size.height / 2), + radius = circleRadius + ) + ) } canvas.save() canvas.clipPath(path) - drawCircle(color = Color(0xFF6E7271), center = Offset(x = size.width / 2 + circleDistance / 2, y = size.height / 2), radius = circleRadius) + drawCircle( + color = Color(0xFF6E7271), + center = Offset(x = size.width / 2 + circleDistance / 2, y = size.height / 2), + radius = circleRadius + ) canvas.restore() } + + // Draw words on the circles + val glossaryCircleCenter = Offset(x = size.width / 2 - circleDistance / 2, y = size.height / 2) + val codeCircleCenter = Offset(x = size.width / 2 + circleDistance / 2, y = size.height / 2) + + glossaryWordsSubset.forEachIndexed { index, word -> + drawIntoCanvas { _ -> + drawText( + textMeasurer, + word, + Offset( + (glossaryCircleCenter.x - circleRadius / 1.5).toFloat(), + glossaryCircleCenter.y - circleRadius / 2 + index * 20f + ), + ) + } + } + + codeWordsSubset.forEachIndexed { index, word -> + drawIntoCanvas { _ -> + drawText( + textMeasurer, + word, + Offset( + codeCircleCenter.x, + codeCircleCenter.y - circleRadius / 2 + index * 20f + ), + ) + } + } + + intersectionWords.forEachIndexed { index, word -> + drawIntoCanvas { _ -> + drawText( + textMeasurer, + word, + Offset( + size.width / 2 - circleDistance / 2 + circleRadius / 2, + size.height / 2 - circleRadius / 2 + index * 20f + ), + ) + } + } + } } +@Composable +fun vennDiagramLegend() { + +}