Advertisement
tomdodd4598

Untitled

Jul 21st, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. package dodd
  2.  
  3. import kotlin.math.*
  4.  
  5. typealias Fn = (Double) -> Double
  6.  
  7. fun List<Double>.rangeMap(minOut: Double, maxOut: Double): List<Double> {
  8. val minIn = this.min()!!
  9. val maxIn = this.max()!!
  10. val deltaIn = maxIn - minIn
  11. val deltaOut = maxOut - minOut
  12.  
  13. return this.map{ minOut + (it - minIn) * deltaOut / deltaIn }
  14. }
  15.  
  16. const val width = 100
  17. const val height = 12
  18.  
  19. const val a = 0.0
  20. const val b = 4 * PI
  21.  
  22. val fn: Fn = ::sin
  23.  
  24. fun main() {
  25. val indices = Array(width) { it }.toList()
  26.  
  27. val ys = indices.map{ a + (it + 0.5) * b / width }.map(fn).rangeMap(0.5, height - 0.5)
  28.  
  29. val steps = mutableListOf(ys.first()).apply {
  30. addAll(ys)
  31. add(ys.last())
  32. }
  33.  
  34. val ds = indices.map{ (steps[it + 2] - steps[it]) * if (it == 0 || it == width - 1) 1.0 else 0.5 }
  35.  
  36. val grads = mapOf(-1.0 to '\\', -0.0 to '-', 0.0 to '-', 1.0 to '/')
  37.  
  38. fun grad(d: Double) = grads.getOrDefault(round(d), '|')
  39.  
  40. fun col(index: Int): Array<Char> {
  41. val arr = Array(height) { ' ' }
  42. arr[round(ys[index] - 0.5).roundToInt()] = grad(ds[index])
  43. return arr
  44. }
  45.  
  46. val cols = indices.map(::col)
  47.  
  48. val rows = (height - 1 downTo 0).map{ i -> Array(width) { j -> cols[j][i] }.toList() }
  49.  
  50. for (row in rows) {
  51. for (ch in row) {
  52. print(ch)
  53. }
  54. println()
  55. }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement