Hello, this is my first time engaging with the Racket community. Thanks for being here. Let me know if I should do anything better to get your help on this.
I’m using draw-line to draw (pen width = 1) lines in the hope they align to pixels on a Windows machine.
Depending on where I insert such a line (as a snip in a pasteboard), I noticed that it may or may not end-up being precisely where I expect it to on a grid I'm keeping track of. I read about scaling and experimented with pen widths, smoothing, and alignment scale to no avail.
I boiled down the issue to the code below that I hope you can understand upon reading, or just run it to see my point. I know I might appear like I'm quibbling over pixels, but with many lines it looks very bad.
My question to you is: how could I get my line drawings to fall onto pixel locations reliably in this setting? What understanding or settings am I missing?
#lang racket/gui
;simple line to draw, with size and direction (horiz/vert) and snip-class wiring
(define line-snip%
(class snip% (super-new)
(init-field size horiz/vert? snip-class)
(send this set-snipclass snip-class)
(define/override (draw dc x y left top right bottom dx dy caret)
(send* dc
(set-pen "Gray" 1 'solid)
(set-smoothing 'unsmoothed)
(set-alignment-scale 2))
(if horiz/vert?
(send dc draw-line (+ x 0) y (+ x size) y)
(send dc draw-line x (+ y 0) x (+ y size))))))
;windowing
(let* ([frame (new frame% [label "Imprecise Alignment Test"]
[width 400] [height 400])]
[board (new pasteboard%)]
[canvas (new editor-canvas% [parent frame] [editor board])]
[snip-class (make-object snip-class%)])
;wiring
(send (get-the-snip-class-list) add snip-class)
;tests
(for* ([pair (in-list '([50 "Lines touch. Good."]
[100 "Corner has gap. How to fix?"]))]
[horiz/vert? (in-list '(#t #f))])
(let* ([placement (first pair)]
[line (make-object line-snip% 50 horiz/vert? snip-class)]
[label (make-object string-snip% (second pair))])
(send board insert line placement placement)
(when horiz/vert?
(send board insert label (+ placement 10) (+ placement 10)))
(send frame show #t))))
Please note that changing the alignment scale above from 2 to 1 fixes this issue as it appears here, but does not fix it when I try to build a spreadsheet-like grid, where I place horizontal and vertical lines at regular intervals, or try to place things in between them, or do more complex pixel-precise actions or placements.
I appreciate your time and expertise for any direction you may offer.