Tuesday, March 1, 2011

How can I draw a concave corner rectangle in WPF?

How can I draw a concave corner rectangle in WPF?

From stackoverflow
  • Care to extrapolate?

  • do you mean a rectangle with concave corners?, ie: 
         ____________________
        |                    |
      __|                    |__
     |                          |
     |                          |
     |                          |
     |__                      __|
        |                    |
        |____________________|
    

    Given a rectangle of dimension w x h with corner radius r There are 4 corners:

    A :  0,0
    B :  w,0
    C :  w,h
    D :  0,h
    

    There is an implicit minimum size of

    w = 2r
    h = 2r
    

    There are 4 Circle Centers, A,B,C,D

    And there's thus a grid of edge points:

    (0,0)--(0+r,0)---(w-r,0)---(w,0)
    |                              |
    (0,0+r)                  (w,0+r)
    |                              |
    |                              |
    (0,h-r)                  (w,h-r)
    |                              |
    (0,h)--(0+r,h)---(w-r,h)---(w,h)
    

    Then its merely a case of computing the arc from one point to another.

  • You can create this by Path Operation under Expression blend (Menu - Object-> Path operations) I have created a Path by subtracting four ellipse against a rectangle.

    the Path.Data for a concave rectangle is given below, @Kent Fredric logic in the previous answer will be helpful to implement one.

    alt text

    M17.200002,0L120.4,0 120.4,2.3066998E-06C120.4,6.7378696,128.10079,12.200001,137.60001,12.200001L137.60001,85.400003C128.10077,85.400003,120.4,90.862138,120.4,97.6L17.200002,97.6C17.200002,90.862151,9.4993697,85.400003,0,85.400003L0,12.199999C9.4993663,12.200015,17.200002,6.7378725,17.200002,0z
    

    See the blog post here http://jobijoy.blogspot.com/2008/11/concave-cornered-rectangle-blend-tip.html

    Another way to get this is to create a WPF Custom Shape like bellow

    public class ConcaveRectangle:System.Windows.Shapes.Shape
    
    Bob Kerlinger : Thank you, I asked this question on a different forum and got the same solution by using the Path function in WPF.
  • I should point out that if it's concave, the four sides aren't both identical and straight; thus it's not a rectangle. But I digress.

0 comments:

Post a Comment