animating cardioid in racket

标题 (x²+y²-1)³-x²y³=0 变种拆解

#lang racket

(require plot racket/gui racket/draw)

(define epsilon 0.1)

(define (heart [n 233])
  (define (range r f)
    (if (< (- n f) epsilon)
        r
        (range (cons f r) (+ f epsilon))))
  (define ps (range '() (- n)))
  (define xs
    (λ (t) (* 16 (expt (sin t) 3))))
  (define ys
    (λ (t) (- (* 13 (cos t)) (* 5 (cos (* 2 t))) (* 2 (cos (* 3 t))) (cos (* 4 t)))))
  (define (points r fp ns)
    (map (λ (t)
           (append r (fp t)))
         ns))
  (lines-interval
         (map vector
              (points '() xs ps)
              (points '() ys ps))
         '(#(0 1))
         #:color 'red
         #:line1-color 'red))

(define f (new frame% [label "todokanaikoi"]
               [width 800]
               [height 800]))
(define c (new canvas% [parent f]
               [vert-margin 10]
               [horiz-margin 10]))

(send f show #t)

(define (tearing-apart dx)
  (plot/dc (heart dx)
           (send c get-dc)
           80 80
           (- (send f get-width) 200)
           (- (send f get-height) 200)
           #:title "tear apart"
           )
  (sleep/yield .2)
  (if (< dx epsilon)
      (display "it's over")
      (tearing-apart (- dx epsilon))))

(tearing-apart 4.0)


下载(可执行文件):

Linux todokanaikoi.tgz
Windows todokanaikoi.zip