I am trying to place a vertically positioned rectangle and another object on an empty-scene using place-images in Beginning Student Language. However, when I run the function, the rectangle only takes up half the scene horizontally when its width is set equal to the width of the scene.
I checked what happens to my code using Stepper and found where the image-width halves but I do not know why. Probably it's to do with cons which I am not very familiar with.
Here's the full code:
(require 2htdp/image)
(define WIDTH 400)
(define HEIGHT 300)
(define HMAX 100)
(define SCENE (empty-scene WIDTH HEIGHT))
(define-struct vcham [x h c])
(define c (make-vcham 0 100 "white"))
(define (render ch)
(place-images (list (rectangle (* 1 WIDTH (/ (vcham-h ch) HMAX)) 50 "solid" "maroon")
(rectangle 1 1 "solid" "blue"))
(list (make-posn 0 0) (make-posn 20 20))
SCENE))
(render c)
I have simplified this code as much as a I could, thus the 1×1 rectangle. I am also interested in elegant alternatives to implement this, but am most interested in finding out why this happens.
place-images
is like place-image
:
Places image onto scene with its center at the coordinates (x,y) and crops the resulting image so that it has the same size as scene. The coordinates are relative to the top-left of scene.
you can change (make-posn 0 0)
to (make-posn 200 25)
, or try place-images/align
(define (render ch)
(place-images/align (list (rectangle (* 1 WIDTH (/ (vcham-h ch) HMAX)) 50 "solid" "maroon")
(rectangle 1 1 "solid" "blue"))
(list (make-posn 0 0) (make-posn 20 20))
"left" "top" SCENE))