DrawLines draws too long lines

Hello,

I try to draw a rectangle with a line pattern, e.g.
white
green
white
green

To complicate things, I give it shading.

My drawing routine (relevant part) is:
     # select Pen width
     if len(colors) == 1:
          K = 1
     else:
          K = 2
     # create lines
     for yi in range(int(h/(1*K)+1)):
             hue, sat, val = colors[yi % N]
             val = val * (1 + cos(fac * yi)) / 2
             newcol = hsv2rgb(hue, sat, val)
             pen = Pen(newcol, K, SOLID)
             lines.append((x0, h1 + K * yi, x1, h1 + K * yi))
             pens.append(pen)
     # draw lines
     dc.DrawLineList(lines, pens)
     # draw surrounding box
     dc.SetPen(Pen('BLACK'))
     dc.DrawLine((x0, y0), (x0, y1))
     dc.DrawLine((x0, y1), (x1, y1))
     dc.DrawLine((x0, y0), (x1, y0))
     dc.DrawLine((x1, y0), (x1, y1 + 1))

Now, if I have to draw only one (shaded) color, I use lines 1 pixel wide.
If I have more colors, I use lines 2 pixels wide.

If the line width is 1, the drawing is within the surrounding box.
But if the line width is 2, the drawing is outside the box, depending on the OS.
On MacOS, the lines extend one pixel to the right, although that is governed by
x0 and x1, that do not change.
On GTK1, the lines also extend one pixel to the right, but that extension is only
one pixel wide (per line of 2 pixels wide).
I solved this by using dc.SetClippingRegion(), but I was wondering why.

And I also want to know why I have to add 1 in the last statement,
or I do not draw the right-down corner point.

Pim Buurman
X>support

Pim Buurman wrote:

Now, if I have to draw only one (shaded) color, I use lines 1 pixel wide.
If I have more colors, I use lines 2 pixels wide.

If the line width is 1, the drawing is within the surrounding box.
But if the line width is 2, the drawing is outside the box, depending on the OS.
On MacOS, the lines extend one pixel to the right, although that is governed by
x0 and x1, that do not change.
On GTK1, the lines also extend one pixel to the right, but that extension is only
one pixel wide (per line of 2 pixels wide).
I solved this by using dc.SetClippingRegion(), but I was wondering why.

Not sure, but it could be incorrect interpretation of the platform API on one of the platforms... If you send a complete example that shows the problem I'll look into it further.

BTW, did you experiment with different cap styles? (See wxPen::SetCap)

And I also want to know why I have to add 1 in the last statement,
or I do not draw the right-down corner point.

Probably related to the above.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Pim Buurman wrote:

Hello Robin,

Here is a complete example.
I have already experiment with different cap styles, but that had no effect.

Pim Buurman
X>support

Pim Buurman wrote:

Now, if I have to draw only one (shaded) color, I use lines 1 pixel wide.
If I have more colors, I use lines 2 pixels wide.
If the line width is 1, the drawing is within the surrounding box.
But if the line width is 2, the drawing is outside the box, depending on the OS.
On MacOS, the lines extend one pixel to the right, although that is governed by
x0 and x1, that do not change.
On GTK1, the lines also extend one pixel to the right, but that extension is only
one pixel wide (per line of 2 pixels wide).
I solved this by using dc.SetClippingRegion(), but I was wondering why.

Not sure, but it could be incorrect interpretation of the platform API on one of the platforms... If you send a complete example that shows the problem I'll look into it further.

BTW, did you experiment with different cap styles? (See wxPen::SetCap)

It was a cap issue, (except on wxMac where it appears to be broken.) If you increase the pen width to 4 then you can see it better in your sample. The default is wx.CAP_ROUND and it is the rounded portion that is extending beyond the end point of the line. Setting it to wx.CAP_BUTT will fix the issue on wxMSW and wxGTK. On wxMac it looks like the pen cap is not being used at all and setting it to any value makes no difference. Please enter a bug report about this with catgory "wxMac specific".

A possible workaround for you is to just draw multiple width=1 lines instead of using thick pens.

And I also want to know why I have to add 1 in the last statement,
or I do not draw the right-down corner point.

Probably related to the above.

Actually, the docs say this (although I don't think it was always this way):

"""
Draws a line from the first point to the second. The current pen is used for drawing the line. Note that the point (x2, y2) is not part of the line and is not drawn by this function (this is consistent with the behaviour of many other toolkits).
"""

···

On 18 May 2004, at 20:15, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!