Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dodd
- import kotlin.math.*
- typealias Fn = (Double) -> Double
- fun List<Double>.rangeMap(minOut: Double, maxOut: Double): List<Double> {
- val minIn = this.min()!!
- val maxIn = this.max()!!
- val deltaIn = maxIn - minIn
- val deltaOut = maxOut - minOut
- return this.map{ minOut + (it - minIn) * deltaOut / deltaIn }
- }
- const val width = 100
- const val height = 12
- const val a = 0.0
- const val b = 4 * PI
- val fn: Fn = ::sin
- fun main() {
- val indices = Array(width) { it }.toList()
- val ys = indices.map{ a + (it + 0.5) * b / width }.map(fn).rangeMap(0.5, height - 0.5)
- val steps = mutableListOf(ys.first()).apply {
- addAll(ys)
- add(ys.last())
- }
- val ds = indices.map{ (steps[it + 2] - steps[it]) * if (it == 0 || it == width - 1) 1.0 else 0.5 }
- val grads = mapOf(-1.0 to '\\', -0.0 to '-', 0.0 to '-', 1.0 to '/')
- fun grad(d: Double) = grads.getOrDefault(round(d), '|')
- fun col(index: Int): Array<Char> {
- val arr = Array(height) { ' ' }
- arr[round(ys[index] - 0.5).roundToInt()] = grad(ds[index])
- return arr
- }
- val cols = indices.map(::col)
- val rows = (height - 1 downTo 0).map{ i -> Array(width) { j -> cols[j][i] }.toList() }
- for (row in rows) {
- for (ch in row) {
- print(ch)
- }
- println()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement