Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tk canvas: why are some lines a pixel too short?

14 views
Skip to first unread message

Pat Wallace

unread,
Jul 13, 2006, 5:46:03 AM7/13/06
to
My Tk application draws its own characters, using lines. I have noticed
that some characters look OK when drawn big but go ugly when redrawn
smaller - so it's not the font to blame. For example my uppercase X seems
to have one few pixel fewer lit up on the two right-hand ends compared
with the left-hand ends.

The following Tcl script demonstrates the kind of thing I mean. It draws
lines radially outwards from a center point, every 10 degrees, constant
radius:

package require Tk
canvas .c -width 100 -height 100
pack .c
set x 50
set y 50
set r 4.5
for {set i 0} {$i < 36} {incr i} {
set t [expr $i*0.174532925]
.c create line $x $y [expr $x+$r*sin($t)] [expr $y+$r*cos($t)]
}

You can get different effects by changing x, y and r. With the values
used above, the supposedly symmetrical pattern has a distinctly flat
lower-right quadrant. (You may need to use a magnifying glass.) Using
deliberately oddball values, such as x=y=50.1, or r=4.7, have an influence
on the pixel pattern, as you'd expect, but the unwanted asymmetry is
frequently seen.

What is the cause, and can I somehow stop it happening? n.b. (i) my
application doesn't know about the mapping from world coordinates to pixel
coordinates, and doesn't want to know, and (ii) I have tried changing the
capstyle to "projecting" etc., but on 1-pixel-wide lines I don't think
this has any effect.

It is as if the line drawing is truncating coordinates rather than
rounding.


Patrick Wallace
____________________________________________________________________________
Space Science & Technology Dept
Rutherford Appleton Laboratory
Chilton, Didcot,
Oxon OX11 0QX, UK
____________________________________________________________________________

mauric...@gmail.com

unread,
Jul 13, 2006, 6:05:36 AM7/13/06
to
try: canvas ... -bd 0 -highlightt 0

suchenwi

unread,
Jul 13, 2006, 6:10:01 AM7/13/06
to
The problem can be easily demonstrated by the following "X":
pack [canvas .c]
.c create line 10 10 20 30
.c create line 10 30 20 10

End points of the two lines seem to be truncated by one pixel.

Pat Wallace

unread,
Jul 13, 2006, 6:49:59 AM7/13/06
to
In article <1152785401.0...@s13g2000cwa.googlegroups.com>,

Excellent: much better than my long-winded example.

By the way, I found one workaround for my particular application - plot
the line twice, in the two directions.

Patrick Wallace
________________________________________________________________________

Geoff Battye

unread,
Jul 13, 2006, 7:23:34 AM7/13/06
to
Use the -capstyle option to control how the ends of lines are drawn.
E.g.

pack [canvas .c]
.c create line 10 10 20 30 -capstyle round
.c create line 10 30 20 10 -capstyle round

Geoff.

Pat Wallace

unread,
Jul 13, 2006, 8:02:58 AM7/13/06
to
In article <e95afm$7s5$1...@gemini.csx.cam.ac.uk>,

Hm. Interesting: I had already tried capstyle projecting and that
appeared to do nothing. But capstyle round does make a difference.

There's still an asymmetry, though. Try this:

package require Tk
canvas .c -width 100 -height 100 -bd 0 -highlightt 0


pack .c
set x 50
set y 50

set r 4.9


for {set i 0} {$i < 36} {incr i} {
set t [expr $i*0.174532925]

.c create line $x $y [expr $x+$r*sin($t)] [expr $y+$r*cos($t)] \
-capstyle round
}

Patrick Wallace
_______________________________________________________________________

Geoff Battye

unread,
Jul 13, 2006, 8:44:14 AM7/13/06
to

Copying and pasting your code straight into tclsh gives me symmetric
output. I've grabbed a snapshot and zoomed in to double check.

Geoff.

Pat Wallace

unread,
Jul 13, 2006, 10:07:27 AM7/13/06
to
In article <e95f6u$j5i$1...@gemini.csx.cam.ac.uk>,
Geoff Battye <ge...@mrc-lmb.cam.ac.uk> wrote:
<snip>

>Copying and pasting your code straight into tclsh gives me symmetric
>output. I've grabbed a snapshot and zoomed in to double check.

The results must be dependent on screen resolution and perhaps software
version. I've tried an ancient wish on an Alpha Tru64 system and a
fairly recent tclsh under MS Windows; both give a result that contains a
departure from symmetry of just one pixel, but it's there. And adding the
-capstyle round doesn't fix the character-drawing problem that I started
with.

Patrick Wallace
__________________________________________________________________________

Christian Gollwitzer

unread,
Jul 13, 2006, 11:32:16 AM7/13/06
to
Pat Wallace wrote:
> My Tk application draws its own characters, using lines. I have noticed
> that some characters look OK when drawn big but go ugly when redrawn
> smaller - so it's not the font to blame.

You know that rendering fonts at small sizes is a very advanced art?
Todays outline fonts (like Type1, Truetype, Opentype) build on a complex
magic called "Hinting", which may even be a font program, to do
acceptable scan-conversion on small sizes. Rendering each character
individual by drawing lines will definitely have severe limits.

Read e.g. this:

http://fontforge.sourceforge.net/overview.html#Hints

and for TrueType
http://www.microsoft.com/typography/TrueTypeHintingIntro.mspx

Christian

0 new messages