By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,914 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
3 Replies


P: n/a
Rod
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

P: n/a
I am not quite sure if I understand but are you saying that a rectangle of
width = 1 and height = 1 can't be drawn as a single pixel "X" because then
it would clash with the definition of a single pixel?

So in order to get rid of the ambiguity between drawing a pixel and a
rectangle with a width = 1 and height = 1 it was decided to draw the
rectangle the way is drawn right now?

I have a felling I am wrong because if what I am saying is true, then the
rectangle definition would have nothing to do with the way is drawn which is
not the case right now. For example, if I instantiate a rectangle such as:

myRec = new Rectangle(0,0,1,1)

The "Right" property of the rectangle "myRec.Right" is currently equal to 1.
But this should only true ONLY when drawing the rectangle, in reality, the
"Right" property of the rectangle should be 0. in other words the Graphics
object should make the adjustment when it draws the rectangle.

I probably didn't explain my self very well. By the way, I assuming that
when we talk about drawing we are talking about having the graphics object
configure as:

myGraphics.PageUnit = GraphicsUnit.Pixel;
myGraphics.PageScale = 1;

Thanks

"Rod" <no********@rodasmith.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 #3

P: n/a
Rod
Think of your 1x1 rectangle "drawn" on the standard, 2 dimensional
Cartesian plane. Place one corner of your rectangle at the point (0,0)
one at (1,1). Now imagine that each point with integer coordinates on
the Cartesian plane (bounded by your screen size) is represented by a
pixel.

With that image, you can see your rectangle passing through four of the
points that map to pixels: {(0,0), (0,1), (1,0), (1,1)}. Since your
rectangle passes through those four points, it displays with four
pixels.

Rene wrote:
I am not quite sure if I understand but are you saying that a rectangle of
width = 1 and height = 1 can't be drawn as a single pixel "X" because then
it would clash with the definition of a single pixel?

So in order to get rid of the ambiguity between drawing a pixel and a
rectangle with a width = 1 and height = 1 it was decided to draw the
rectangle the way is drawn right now?

I have a felling I am wrong because if what I am saying is true, then the
rectangle definition would have nothing to do with the way is drawn which is
not the case right now. For example, if I instantiate a rectangle such as:

myRec = new Rectangle(0,0,1,1)

The "Right" property of the rectangle "myRec.Right" is currently equal to 1.
But this should only true ONLY when drawing the rectangle, in reality, the
"Right" property of the rectangle should be 0. in other words the Graphics
object should make the adjustment when it draws the rectangle.

I probably didn't explain my self very well. By the way, I assuming that
when we talk about drawing we are talking about having the graphics object
configure as:

myGraphics.PageUnit = GraphicsUnit.Pixel;
myGraphics.PageScale = 1;

Thanks

"Rod" <no********@rodasmith.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 #4

This discussion thread is closed

Replies have been disabled for this discussion.