Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package hu.toliver.vinotes.ui.screen.addtasting.components

import kotlin.math.atan2
import kotlin.math.roundToInt

/**
* Maps a drag position on the rating arc canvas to a 0–100 value.
* Valid arc angles are 0° (right, rating 100) through 180° (left, rating 0).
* Pointer positions below the arc (negative angle) clamp by horizontal side.
*/
fun ratingFromPointer(
pointerX: Float,
pointerY: Float,
centerX: Float,
centerY: Float,
): Int {
val degrees = Math.toDegrees(
atan2(
(centerY - pointerY).toDouble(),
(pointerX - centerX).toDouble(),
),
)
if (degrees < 0.0) {
return if (pointerX < centerX) 0 else 100
}
return (100.0 - degrees / 180.0 * 100.0).roundToInt().coerceIn(0, 100)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import hu.toliver.vinotes.R
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.roundToInt
import kotlin.math.sin

@Composable
Expand Down Expand Up @@ -67,12 +65,12 @@ fun RatingArcSlider(
detectDragGestures { change, _ ->
change.consume()
val center = Offset(size.width / 2f, size.height.toFloat())
val angle = atan2(
(center.y - change.position.y).toDouble(),
(change.position.x - center.x).toDouble(),
val newRating = ratingFromPointer(
pointerX = change.position.x,
pointerY = change.position.y,
centerX = center.x,
centerY = center.y,
)
val degrees = Math.toDegrees(angle).coerceIn(0.0, 180.0)
val newRating = 100 - (degrees / 180.0 * 100).roundToInt().coerceIn(0, 100)
onChange(newRating)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hu.toliver.vinotes.ui.screen.addtasting.components

import org.junit.Assert.assertEquals
import org.junit.Test

class RatingArcPointerMathTest {

private val centerX = 540f
private val centerY = 400f

@Test
fun leftEndOfArc_mapsToZero() {
assertEquals(0, ratingFromPointer(0f, centerY, centerX, centerY))
}

@Test
fun rightEndOfArc_mapsToHundred() {
assertEquals(100, ratingFromPointer(centerX * 2f, centerY, centerX, centerY))
}

@Test
fun topOfArc_mapsToFifty() {
assertEquals(50, ratingFromPointer(centerX, centerY - 200f, centerX, centerY))
}

@Test
fun belowArcLeft_clampsToZero_notWrapToHundred() {
assertEquals(0, ratingFromPointer(centerX - 100f, centerY + 80f, centerX, centerY))
}

@Test
fun belowArcRight_clampsToHundred() {
assertEquals(100, ratingFromPointer(centerX + 100f, centerY + 80f, centerX, centerY))
}

@Test
fun gapRegionJustPastZeroSide_staysAtZero() {
val justPastLeft = ratingFromPointer(centerX - 200f, centerY + 1f, centerX, centerY)
assertEquals(0, justPastLeft)
}

@Test
fun gapRegionJustPastHundredSide_staysAtHundred() {
val justPastRight = ratingFromPointer(centerX + 200f, centerY + 1f, centerX, centerY)
assertEquals(100, justPastRight)
}
}
Loading