Scheme/Racket:画布操作

4

1) 标题所说,当我调整窗口大小时,我绘制的对象会消失,但矩形保持不变。

2) 原点从左上角开始,但我希望它在左下角。

3) 我找不到缩放功能,除了在绘图库中以外,如果我想要实现这样的功能,一个选项是通过绘制更大的对象并刷新画布来进行“缩放”吗?

(define top-frame (new frame%
                         [label "KR"]
                         [width 500]
                         [height 500]))
;Make a frame by instantiating the frame% class

(define image (pict->bitmap (rectangle 50 50)))

(define canvas (new canvas%
                    [parent top-frame]
                    [paint-callback (lambda (canvas dc)
                                      (send dc draw-bitmap image 0 0))]))

(define drawer (send canvas get-dc))

(send top-frame show #t)
; Show the frame by calling its show method

(define (draw-object x)
  (sleep/yield 0.1)
  (case (first x) 
    [("LINE") (send drawer draw-line
                    (second x) (third x)
                    (fourth x) (fifth x))]
    [("CIRCLE") (send drawer draw-bitmap (pict->bitmap (circle (round (fourth x)))) (round (second x)) (round (third x)))]
    [("POINT") (send drawer draw-point (round (second x)) (round (third x)))]
    [else "Not drawing anything!"]))

(draw-object (find-specific-values (third list-of-objects)))
(map draw-object (map find-specific-values list-of-objects))
1个回答

4

广告 1)"...当我调整窗口大小时,我绘制的对象会消失,...".

当你改变一个窗口的大小时,系统需要重绘窗口的内容。重绘事件被发出,最终Racket GUI层将调用paint-callback。因此:创建一个函数来执行所有的绘图操作。从paint-callback中调用它。参考这个类似的问题:https://dev59.com/QnHYa4cB1Zd3GeqPHhtM#16086594

广告2)一种选择是在绘图上下文中进行坐标转换。在dc<%>的文档中查看set-transformation。大概是这样:

(send dc set-transformation 
        (vector (trans->vector t)
                0   0 ; x and y origin
                1  -1 ; x and y scale
                0)))

在y轴上设置-1会翻转y轴。您可能需要移动原点。

3) 可以通过改变x和y的比例尺然后重新绘制来进行缩放。您可以尝试将比例尺设置为1/2 -1/2。


1
谢谢你指引我正确的方向 :) 对于其他有兴趣的人,当翻转y轴时,你需要添加一个y原点偏移量,该偏移量等于你框架的高度,这样你的0,0点就位于左下角。 - KRC
也许我漏掉了什么,但是 trans->vector 在哪里定义的?或者这是一个你应该编写的函数吗? - Peter
1
这个例子有点脱离上下文,所以 trans->vector 没有在任何地方定义。它应该返回一个向量 (vector xx xy yx yy x0 y0),对应于将 (x,y) 转换为 xnew = xx * x + xy * y + x0ynew = yx * x + yy * y + y0。详见此处:https://github.com/soegaard/metapict/blob/master/metapict/trans.rkt - soegaard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接