p150h.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;; New way of doing 150
  2. ;; Helpers functions to get the sides and intersection vectors
  3. (define-module (unsolved p150h))
  4. (use-modules (unsolved p150))
  5. (define-public (make-sides triangle)
  6. (define row-length (triangle-rows triangle))
  7. (define (make-left-rows)
  8. (define vect (make-vector (1+ row-length) 0))
  9. (do [(j 0 (1+ j))]
  10. [(>= j row-length) vect]
  11. (do [(i j (1+ i))]
  12. [(>= i row-length)]
  13. (vector-set!
  14. vect (1+ j) (+ (vector-ref vect (1+ j))
  15. (vector-ref
  16. (vector-ref triangle i) j))))
  17. (vector-set!
  18. vect (1+ j) (+ (vector-ref vect j)
  19. (vector-ref vect (1+ j))))))
  20. (define (make-right-rows)
  21. (define vect (make-vector (1+ row-length) 0))
  22. (do [(i 0 (1+ i))]
  23. [(>= i row-length) vect]
  24. (do [(j 0 (1+ j))]
  25. [(>= j (- row-length i))]
  26. (vector-set!
  27. vect (1+ i) (+ (vector-ref vect (1+ i))
  28. (vector-ref
  29. (vector-ref triangle (+ i j)) j))))
  30. (vector-set!
  31. vect (1+ i) (+ (vector-ref vect i)
  32. (vector-ref vect (1+ i))))))
  33. ;; TODO: figure out best way to represent...
  34. (define (make-bot-rows)
  35. (define vect (make-vector (1+ row-length) 0))
  36. (do [(i 0 (1+ i))]
  37. [(>= i row-length) vect]
  38. (do [(j 0 (1+ j))]
  39. [(>= j (- row-length i))]
  40. (vector-set!
  41. vect (1+ i)
  42. (+ (vector-ref vect (1+ i))
  43. (vector-ref
  44. (vector-ref triangle (- (1- row-length) i)) j))))
  45. (vector-set!
  46. vect (1+ i) (+ (vector-ref vect i)
  47. (vector-ref vect (1+ i))))))
  48. (list
  49. (make-left-rows) (make-right-rows) (make-bot-rows)))
  50. ;; TODO: consider creating setters and getters for sides-vector
  51. ;; I imagine that I need to create the intersection arrays on the fly...
  52. ;; Def turn this into a macro!
  53. (define-public (make-intersections triangle)
  54. (define triangle-length (triangle-rows triangle))
  55. (define (make-l&r-intersections)
  56. (define vect (make-vector (1+ triangle-length) 0))
  57. (vector-set! vect 0 (make-vector (1+ triangle-length) 0))
  58. (do [(j 0 (1+ j))]
  59. [(>= j triangle-length) vect]
  60. (let ([row-vect (make-vector (- (1+ triangle-length) j) 0)])
  61. (do [(i 0 (1+ i))]
  62. [(>= i (- triangle-length j))]
  63. (vector-set! row-vect (1+ i)
  64. (+ (vector-ref (vector-ref vect j) (1+ i))
  65. (vector-ref (vector-ref triangle (+ i j)) j))))
  66. (vector-set! vect (1+ j) row-vect))))
  67. (define (make-l&b-intersections)
  68. (define vect (make-vector (1+ triangle-length) 0))
  69. (vector-set! vect 0 (make-vector (1+ triangle-length) 0))
  70. (do [(j 0 (1+ j))]
  71. [(>= j triangle-length) vect]
  72. (let ([row-vect (make-vector (- (1+ triangle-length) j) 0)])
  73. (do [(i 0 (1+ i))]
  74. [(>= i (- triangle-length j))]
  75. (vector-set! row-vect (1+ i)
  76. (+ (vector-ref (vector-ref vect j) (1+ i))
  77. (vector-ref (vector-ref triangle (- triangle-length
  78. (1+ i)))
  79. j))))
  80. (vector-set! vect (1+ j) row-vect))))
  81. (define (make-r&b-intersections)
  82. (define vect (make-vector (1+ triangle-length) 0))
  83. (vector-set! vect 0 (make-vector (1+ triangle-length) 0))
  84. (do [(j 0 (1+ j))]
  85. [(>= j triangle-length) vect]
  86. (let ([row-vect (make-vector (- (1+ triangle-length) j) 0)])
  87. (do [(i 0 (1+ i))]
  88. [(>= i (- triangle-length j))]
  89. (vector-set! row-vect (1+ i)
  90. (+ (vector-ref (vector-ref vect j) (1+ i))
  91. (vector-ref (vector-ref triangle (- triangle-length
  92. (1+ i)))
  93. (- triangle-length (+ i (1+ j)))))))
  94. (vector-set! vect (1+ j) row-vect))))
  95. (list
  96. (make-l&r-intersections)
  97. (make-l&b-intersections)
  98. (make-r&b-intersections)))
  99. ;; Why not just build them first, then I don't need to deal with triangle in computation?
  100. ;; Suppose we could make that argument
  101. ;; Basically its going to update every time we recur with l-idx and r-idx
  102. ;; Need to modularize this alot...
  103. (define (init-l&r-intersections triangle)
  104. (let ([intersect-vect (make-vector (triangle-rows triangle) 0)])
  105. (l&r-intersect-lp! triangle intersect-vect 0)
  106. intersect-vect))
  107. ;; TODO: switch i,j
  108. (define (l&r-intersect-lp! triangle vect curr-idx)
  109. (let ([j curr-idx]
  110. [triangle-length (triangle-rows triangle)])
  111. (do [(i 0 (1+ i))]
  112. [(>= i (- triangle-length j))]
  113. (vector-set! vect i (+ (vector-ref
  114. (vector-ref triangle (+ i j))
  115. i)
  116. (vector-ref vect i))))))
  117. ;; Hmm this seems a little hacky?
  118. ;; Yeah I don't think i want to deal with it...
  119. ;; If we don't get r-idx, then do l-idx, otherwise r-idx
  120. (define (l/r-intersect-lp! vect triangle curr-idx)
  121. (let ([j curr-idx]
  122. [triangle-length (triangle-rows triangle)])
  123. (do [(i 0 (1+ i))]
  124. [(>= i (- triangle-length j))]
  125. (vector-set! vect i (+ (vector-ref
  126. (vector-ref triangle (+ i j))
  127. i)
  128. (vector-ref vect i))))))
  129. (define (update-l&r-intersections triangle intersections-vect curr-idx)
  130. (l&r-intersect-lp! triangle intersections-vect curr-idx))
  131. (define (update-l-intersections triangle intersections-vect curr-idx)
  132. (l-intersect-lp! triangle intersections-vect curr-idx))