我试图通过逐渐旋转由直线和圆形组合形成的图像来编码一个简单的摆动摆:
(require 2htdp/image)
(require 2htdp/universe)
(define ang 0)
(define x 1)
(define pend
(underlay/xy (line 0 250 "black")
-10 250
(circle 10 "solid" "green")))
(define (simple_pendulum t)
(when (> (abs ang) 30)
(set! x (* x -1)))
(set! ang (+ ang x))
(underlay/xy (rectangle 500 500 "solid" "white")
250 50
(rotate ang pend))) ; value of ang swings between -30 to +30 ;
(animate simple_pendulum)
虽然摆锤正好向一侧移动,但在另一侧却没有这样做。相反,钟摆摆动的基础。错误在哪里,我该如何纠正?
请注意,我只是想以恒定的速度将钟摆从一端摆到另一端。我知道这不会是一个真正的钟摆物理精确的运动模式。
答案 0 :(得分:0)
这里的问题是,在旋转之后,你指示underlay/xy
以这样的方式绘制你的图像,使得摆锤图像的左边缘位于x坐标250处。正如你所看到的,这正是它正在做什么;当摆锤向左摆动时,摆的左边缘总是在x坐标250处。
那么,如何解决呢?
你应该使用针孔。这就是我的意思:
#lang racket
(require 2htdp/image)
(require 2htdp/universe)
(define ang 0)
(define x 1)
(define pend
(put-pinhole
10 0
(underlay/xy (line 0 250 "black")
-10 250
(circle 10 "solid" "green"))))
(define (simple_pendulum t)
(when (> (abs ang) 30)
(set! x (* x -1)))
(set! ang (+ ang x))
(clear-pinhole
(underlay/pinhole (put-pinhole
250 100
(rectangle 500 500 "solid" "white"))
(rotate ang pend)))) ; value of ang swings between -30 to +30 ;
(animate simple_pendulum)