Q on Union and Xor Region methods

Hi,

I have been working with regions recently. I have set up a small example. Interset and Subtract medhods work as I have expected. But not the Xor and Union methods.

the example shows all four methods. we have two rectangles. the xor and union take up all the space of both rectangles, which is wrong. What am I doing incorrectly?

thanks,

jan bodnar

#!/usr/bin/python

# regions.py

import wx

class Regions(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(300, 280))

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        self.Centre()
        self.Show(True)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.SetPen(wx.Pen('#D4D4D4'))

        dc.DrawRectangle(20, 20, 50, 50)
        dc.DrawRectangle(30, 40, 50, 50)

        region1 = wx.Region(20, 20, 50, 50)
        region2 = wx.Region(30, 40, 50, 50)
        region1.SubtractRegion(region2)
        rect = region1.GetBox()
        dc.SetBrush(wx.Brush('BLUE'))
        dc.DrawRectangleRect(rect)

        dc.SetBrush(wx.Brush('WHITE'))
        dc.DrawRectangle(100, 20, 50, 50)
        dc.DrawRectangle(110, 40, 50, 50)
        region1 = wx.Region(100, 20, 50, 50)
        region2 = wx.Region(110, 40, 50, 50)
        region1.IntersectRegion(region2)
        rect = region1.GetBox()
        dc.SetBrush(wx.Brush('RED'))
        dc.DrawRectangleRect(rect)

        dc.SetBrush(wx.Brush('WHITE'))
        dc.DrawRectangle(180, 20, 50, 50)
        dc.DrawRectangle(190, 40, 50, 50)
        region1 = wx.Region(180, 20, 50, 50)
        region2 = wx.Region(190, 40, 50, 50)
        region1.UnionRegion(region2)
        rect = region1.GetBox()
        dc.SetBrush(wx.Brush('#fa8e00'))
        dc.DrawRectangleRect(rect)

        dc.SetBrush(wx.Brush('WHITE'))
        dc.DrawRectangle(20, 120, 50, 50)
        dc.DrawRectangle(30, 140, 50, 50)
        region1 = wx.Region(20, 120, 50, 50)
        region2 = wx.Region(30, 140, 50, 50)
        region1.XorRegion(region2)
        rect = region1.GetBox()
        dc.SetBrush(wx.Brush('#619e1b'))
        dc.DrawRectangleRect(rect)

app = wx.App()
Regions(None, -1, 'regions')
app.MainLoop()

vronskij@gmail.com wrote:

Hi,

I have been working with regions recently. I have set up a small
example. Interset and Subtract medhods work as I have expected. But
not the Xor and Union methods.

the example shows all four methods. we have two rectangles. the xor
and union take up all the space of both rectangles, which is wrong.
What am I doing incorrectly?

region.GetBox returns the rectangle that encompasses all of the points in the region, so if the region is not a rectangle then you'll get more pixels in the box than you expect. As an example consider a region that describes a circle. It's GetBox() will give you a rectangle (a square actually) that is DxD pixels in size, where D is the diameter of the circle.

For your sample app the best way to see what is truly in the region is to set the DC's clipping region and then draw. Anything outside of the region will not be painted.

#!/usr/bin/python

# regions.py

import wx

class Regions(wx.Frame):
     def __init__(self, parent, id, title):
         wx.Frame.__init__(self, parent, id, title, size=(300, 280))

         self.Bind(wx.EVT_PAINT, self.OnPaint)

         self.Centre()
         self.Show(True)

     def OnPaint(self, event):
         dc = wx.PaintDC(self)
         dc.SetPen(wx.Pen('#D4D4D4'))

         dc.DrawRectangle(20, 20, 50, 50)
         dc.DrawRectangle(30, 40, 50, 50)

         region1 = wx.Region(20, 20, 50, 50)
         region2 = wx.Region(30, 40, 50, 50)
         region1.SubtractRegion(region2)
         rect = region1.GetBox()
         dc.SetClippingRegionAsRegion(region1)
         dc.SetBrush(wx.Brush('BLUE'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

         dc.SetBrush(wx.Brush('WHITE'))
         dc.DrawRectangle(100, 20, 50, 50)
         dc.DrawRectangle(110, 40, 50, 50)
         region1 = wx.Region(100, 20, 50, 50)
         region2 = wx.Region(110, 40, 50, 50)
         region1.IntersectRegion(region2)
         rect = region1.GetBox()
         dc.SetClippingRegionAsRegion(region1)
         dc.SetBrush(wx.Brush('RED'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

         dc.SetBrush(wx.Brush('WHITE'))
         dc.DrawRectangle(180, 20, 50, 50)
         dc.DrawRectangle(190, 40, 50, 50)
         region1 = wx.Region(180, 20, 50, 50)
         region2 = wx.Region(190, 40, 50, 50)
         region1.UnionRegion(region2)
         dc.SetClippingRegionAsRegion(region1)
         rect = region1.GetBox()
         dc.SetBrush(wx.Brush('#fa8e00'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

         dc.SetBrush(wx.Brush('WHITE'))
         dc.DrawRectangle(20, 120, 50, 50)
         dc.DrawRectangle(30, 140, 50, 50)
         region1 = wx.Region(20, 120, 50, 50)
         region2 = wx.Region(30, 140, 50, 50)
         region1.XorRegion(region2)
         rect = region1.GetBox()
         dc.SetClippingRegionAsRegion(region1)
         dc.SetBrush(wx.Brush('#619e1b'))
         dc.DrawRectangleRect(rect)
         dc.DestroyClippingRegion()

app = wx.App()
Regions(None, -1, 'regions')
app.MainLoop()

···

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