Tiling a 2-D Polygon using C# GDI+

Tiling a Polygon

Tiling a Polygon

One of the most challenging problems I came across working on a .NET PDF Annotator and Editor application was to tile a 2-D polygon and also accurately determine the number of tiles that fill the surface of the polygon.  The tiling part was not as much of a challenge as the counting part. The tiled polygon was to be rendered on a PDF document since the application in question is a PDF Annotating and Editing tool. We looked for anything the third party .NET PDF rendering/manipulation API that was used could provide for the tile rendering but there was nothing unfortunately.

So we set out to use C#'s native GDI+ library to render the tiles for the polygon.  After trying out different approaches to accomplish the rendering of the tiles, we discovered the one that would work best. The idea was simple. Every 2-D shape on the drawing surface has a bounding rectangle that encloses the shape. Starting from the top left bounding point of the rectangle, iteratively render a rectangle (tile with whatever length and width) across the X axis until the right most edge of the rectangle and this iteration needs to happen over the Y-axis (not sure if I explained clearly enough :-) .  So now we have rectangular tiles laid out across the bounding rectangle for the polygon. GDI+ gives us a clip method that allows us to clip the rendering surface to a specific graphics path or region. Using the clip method, clip the drawing surface to be the graphics path for just the Polygon so that only tiles within the polygon's graphics path get rendered on the screen. We could apply other transformations to the rendered tiles like offseting, rotation or a gap between each tile.

Coming up with the most efficient way to count the number of tiles (including partial tiles) was a little trickier than rendering the tiles.  It turned out that the ideal approach was to essentially count each tile as it was drawn and checking whether the tile was partially or fully part of the polygon's graphics path/region. So, there is this method Region.IsVisible that lets you test whether a rectangle is partially or fully contained within a graphics region. During the rendering of each rectangular tile, the method was used to check whether the tile was going to be part of the region associated with the polygon and was counted if it was going to be. Even though, this was the most efficient solution for the problem, we did nt see 100% accuracy sometimes when rotation transformation was applied to the tiles. Still havent found an answer to this anomaly.

Related posts:

  1. Eyespot – AJAX App for Video Editing
  2. LINQ to My Domain
  3. How to REALLY do Page Preview in Java with Embedded HTML Rendering
  4. How to do Page Preview in Java with Embedded HTML Rendering
  5. 3D Data Visualization in Adobe Air

Comments: 2 so far

  1. Hi Karthik,

    Good on you bro! Thank you for sharing a tech tip that can be used by a lot of developers for building their apps.

    Regards,
    Vijay

    Comment by M. Vijay, Saturday, February 13, 2010 @ 5:02 am

  2. @Vijay – You are welcome! Our company has a pretty interesting blog feed that keep people like you interested. Stay tuned!

    Comment by Karthik, Monday, February 15, 2010 @ 1:55 pm

Leave a comment

Powered by WP Hashcash

Launch: Pathfinder Newsletter

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email


    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com