Racket/3D-boll: Skillnad mellan sidversioner

Från Täpp-Anders
Hoppa till navigeringHoppa till sök
Skapade sidan med '<pre> #lang racket (require plot) (require racket/pretty) ;; En pixelboll iracket ;; Från Buzzern skrivet under påskhelgen (define π (acos -1)) (define VERTICAL-SPLIT 16) (define HORIZONTAL-SPLIT 16) (define PARTICLE-SIZE (* VERTICAL-SPLIT HORIZONTAL-SPLIT)) (define particles (for/vector ([i (in-range PARTICLE-SIZE)]) (make-vector 3))) ;; from scheme.com since normal scheme lacks for/vector (define make-matrix (lambda (rows columns) (do ((...'
Ingen redigeringssammanfattning
Rad 1: Rad 1:
<pre>
<pre>
#lang racket
#lang racket
(require plot)
(require plot)
Rad 70: Rad 69:
(generate-dots particles)
(generate-dots particles)
(plot3d (points3d particles))
(plot3d (points3d particles))


</pre>
</pre>

Versionen från 7 april 2026 kl. 18.06

#lang racket
(require plot)
(require racket/pretty)
;; En pixelboll iracket
;; Från Buzzern skrivet under påskhelgen

(define π (acos -1))
(define VERTICAL-SPLIT 16)
(define HORIZONTAL-SPLIT 16)
(define PARTICLE-SIZE (* VERTICAL-SPLIT HORIZONTAL-SPLIT))
(define particles (for/vector ([i (in-range PARTICLE-SIZE)])
                    (make-vector 3)))

;; from scheme.com since normal scheme lacks for/vector
(define make-matrix
  (lambda (rows columns)
    (do ((m (make-vector rows))
         (i 0 (+ i 1)))
        ((= i rows) m)
        (vector-set! m i (make-vector columns)))))


(define (to->deg rad)
  (* rad (/ 180 π)))

(define (print-dots amount)
  (let myloop ((count 0))
    (if (>= count amount)
        '()
        (begin
        (println count)
        (myloop (+ count 1))))))

(define (generate-xyz radius θ ϕ)
  (let* ([x (* radius (sin θ) (cos ϕ))]
         [y (* radius (sin θ) (sin ϕ))]
         [z (* radius (cos θ))]
         )
  (vector x y z)))

(define (test-loop amount)
  (let loop ((count 0))
    (if (>= count amount)
        '()
        (begin
        (println count)
        (loop (+ count 1))))))

(define (get-angle index split)
  (* 2 π index (/ split)))

(define (generate-dots particles)
  (let* ([index-y -1])
  (for ([row (in-vector particles)]
        [index (in-naturals)])
    (set! index-y (if (= 0 (modulo index HORIZONTAL-SPLIT))
        (+ index-y 1)
        index-y))
    ;(printf " ~a ~a\n" (modulo index HORIZONTAL-SPLIT) index-y)
    (define new-θ (get-angle (modulo index HORIZONTAL-SPLIT) HORIZONTAL-SPLIT))
    (define new-ϕ (get-angle (modulo index-y VERTICAL-SPLIT) VERTICAL-SPLIT))
    (printf " ~a ~a \n" (to->deg new-θ) (to->deg new-ϕ))
    ;;(vector-set! row 0 )
    (vector-set! particles index (generate-xyz 1.0 new-θ new-ϕ)))))

;;(test-loop HORIZONTAL-SPLIT)
;;(generate-xyz 1 π (* 2.0 π))
(generate-dots particles)
(plot3d (points3d particles))