459,914 Members | 1,718 Online Need help? Post your question and get tips & solutions from a community of 459,914 IT Pros & Developers. It's quick & easy.

# Rectangle odd behavior

 P: n/a I am having a hard time understanding the logic behind the Rectangle object. My problem has to do with the way the rectangle treats the "Width" property. For example, take the following rectangle object. Rectangle myRec = new Rectangle(0, 0, 2, 2); If you draw this rectangle on the screen you will end up with a rectangle like the one shown below (The character "X" represents a pixel used to draw the rectangle and the character "O" is an untouched pixel): XXX XOX XXX Take a close look at that rectangle. I in my head, this rectangle is not 2 pixels width, for me, this rectangle is actually 3 pixels width. So here is my first question. Do you agree with me that this rectangle is really 3 pixels with and not 2? Things get more confusing when you try to draw something inside the rectangle using a function such as DrawIcon() or FillRectangle(). For example if you use the FillRectangle() function and pass it the rectangle object created above the program will draw the following filled rectangle (The character "X" represents a pixel used to draw the filled rectangle and the character "O" is an untouched pixel): XXO XXO OOO Although my rectangle was 3 pixels width, the function is ignoring the right and bottom edge of the rectangle area. Is there any logic behind this behavior? Thanks. Nov 17 '05 #1
3 Replies

 P: n/a To understand your rectangle's display behavior, realize that display of a rectangle with pixels can be considered as an approximation of an idealized mathematical rectangle. To achieve consistency between pixel display measurements and your underlying mathematical model, you must adjust your measurement technique. To see how to adjust the measurement technique, consider the degenerate version of your example. The smallest rectangle is one with zero width and zero height positioned at (0,0) (e.g. "new Rectangle(0,0,0,0)"). That rectangle is, in standard geometry, equivalent to a point positioned at (0,0). Its pixel display is typically a single "X" to represent a single point at position (0,0). Since the width and height of a point is zero, measuring the size of the object represented by "X" should yield zero width and height. Consider displaying and then measuring the following sequence of shapes: * A point: "X" * A line of length one: "XX" * A line of length two: "XXX" * A unit rectangle "new Rectangle(0,0,1,1)": XX XX To measure shapes given their non-aliased pixel representation, then, you can think of each pixel as covering an area of one square unit, centered on a point with integer coordinates. The origin pixel, then, centered at position (0,0) would cover a rectangular region extending from (-0.5,-0.5) to (0.5,0.5). So, one technique of measuring the size of shapes displayed with pixels is to measure points from the centers of the pixels instead of measuring from the pixel's "outside edge". Measuring from the pixels' centers (or from their top left corners, for that matter) gives consistent results, so your rectangle measures 2 pixels wide and high: 2 |^| XXX - XOX >2 XXX - Extending that line of reasoning to filling shapes, where fill region is bounded by the shape's border, helps explain the results of your FillRectangle() experiment. Does that help? --Rod Rene wrote: I am having a hard time understanding the logic behind the Rectangle object. My problem has to do with the way the rectangle treats the "Width" property. For example, take the following rectangle object. Rectangle myRec = new Rectangle(0, 0, 2, 2); If you draw this rectangle on the screen you will end up with a rectangle like the one shown below (The character "X" represents a pixel used to draw the rectangle and the character "O" is an untouched pixel): XXX XOX XXX Take a close look at that rectangle. I in my head, this rectangle is not 2 pixels width, for me, this rectangle is actually 3 pixels width. So here is my first question. Do you agree with me that this rectangle is really 3 pixels with and not 2? Things get more confusing when you try to draw something inside the rectangle using a function such as DrawIcon() or FillRectangle(). For example if you use the FillRectangle() function and pass it the rectangle object created above the program will draw the following filled rectangle (The character "X" represents a pixel used to draw the filled rectangle and the character "O" is an untouched pixel): XXO XXO OOO Although my rectangle was 3 pixels width, the function is ignoring the right and bottom edge of the rectangle area. Is there any logic behind this behavior? Thanks. Nov 17 '05 #2 