Skip to content

Commit 28ed279

Browse files
committed
create linear equation from 2 points
1 parent 6feddfc commit 28ed279

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

slider/src/main/java/io/monstarlab/mosaic/slider/LinearEquation.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ public data class LinearEquation(
77
public fun valueFromOffset(offset: Float): Float = m * offset + c
88

99
public fun offsetFromValue(value: Float): Float = (value - c) / m
10+
11+
public companion object{
12+
public fun fromTowPoints(x1:Float,y1:Float,x2:Float,y2:Float): LinearEquation {
13+
require(x2!=x1){"can't calc equation from points with similar x value"}
14+
val slope = (y2-y1)/(x2-x1)
15+
val c = y2 - slope*x2
16+
return LinearEquation(slope,c)
17+
}
18+
}
1019
}

0 commit comments

Comments
 (0)