forked from KoalaPlot/koalaplot-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename PolarPlot to PolarGraph, add configurable angular axis directi…
…on and origin.
- Loading branch information
Showing
7 changed files
with
270 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
44 changes: 44 additions & 0 deletions
44
src/commonMain/kotlin/io/github/koalaplot/core/polar/FloatRadialAxisModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.github.koalaplot.core.polar | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
|
||
/** | ||
* A model for a radial axis that transforms from axis space to drawing space. | ||
*/ | ||
public interface RadialAxisModel<T> { | ||
/** | ||
* Provides a list of tick values that should be rendered for the axis. | ||
*/ | ||
public val tickValues: List<T> | ||
|
||
/** | ||
* Transforms the provided [point] in this axis space to the fraction representing the point's distance | ||
* from the origin relative to the overall axis length. | ||
*/ | ||
public fun computeOffset(point: T): Float | ||
} | ||
|
||
/** | ||
* A model for a radial axis that uses Float values and is linear. | ||
* | ||
* @param tickValues Values for each tick on the axis. Must have at least 2 values. | ||
*/ | ||
public class FloatRadialAxisModel constructor( | ||
override val tickValues: List<Float>, | ||
) : RadialAxisModel<Float> { | ||
init { | ||
require(tickValues.size >= 2) { "tickValues must have at least 2 values " } | ||
} | ||
|
||
private val sortedTickValues = tickValues.sorted() | ||
private val range = sortedTickValues.last() - sortedTickValues.first() | ||
|
||
public override fun computeOffset(point: Float): Float { | ||
return (point - sortedTickValues.first()) / range | ||
} | ||
} | ||
|
||
@Composable | ||
public fun rememberFloatRadialAxisModel(tickValues: List<Float>): FloatRadialAxisModel = | ||
remember(tickValues) { FloatRadialAxisModel(tickValues) } |
This file contains 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
Oops, something went wrong.