package com.cottondb.android.design import androidx.compose.foundation.Canvas import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.StrokeCap import androidx.compose.ui.graphics.StrokeJoin import androidx.compose.ui.graphics.drawscope.Stroke import com.cottondb.android.generated.CottonMark import com.cottondb.android.generated.CottonPalette /** The generated constellation-C geometry, drawn at any Compose size. */ @Composable fun ConstellationMark( modifier: Modifier = Modifier, ink: Color = CottonPalette.ink, ) { Canvas(modifier = modifier) { val boundsWidth = CottonMark.contentMaxX - CottonMark.contentMinX val boundsHeight = CottonMark.contentMaxY - CottonMark.contentMinY if (boundsWidth <= 0f || boundsHeight <= 0f) return@Canvas val scale = minOf(size.width / boundsWidth, size.height / boundsHeight) val offsetX = (size.width - boundsWidth * scale) / 2f - CottonMark.contentMinX * scale val offsetY = (size.height - boundsHeight * scale) / 2f - CottonMark.contentMinY * scale fun point(index: Int): Offset { val source = CottonMark.points[index] return Offset(source.x * scale + offsetX, source.y * scale + offsetY) } val threads = Path().apply { CottonMark.threads.forEach { thread -> moveTo(point(thread.from).x, point(thread.from).y) lineTo(point(thread.to).x, point(thread.to).y) } } drawPath( path = threads, color = ink.copy(alpha = 0.35f), style = Stroke( width = CottonMark.threadWidth * scale, cap = StrokeCap.Round, join = StrokeJoin.Round, ), ) CottonMark.points.filter { it.isKnot }.forEach { knot -> drawCircle( color = ink, radius = CottonMark.knotRadius * scale, center = Offset(knot.x * scale + offsetX, knot.y * scale + offsetY), ) } } }