473,382 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Rectangle odd behavior

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 2446
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
6
by: kimos | last post by:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) { Rectangle r;
5
by: DazedAndConfused | last post by:
I have a rectangle around text that I want to fill in with color. I do not know the height of the rectangle until I actually go through and draw out the text. Is there a way of filling in the...
6
by: Mythran | last post by:
I have a question regarding the RECT structure required for PInvoke calls. I was reviewing the Rectangle structure (System.Drawing.Rectangle) and it occurred to me that I may be able to use this...
0
by: Brian A. Cline | last post by:
I have a SplitContainer that holds a TreeView and ListView. When using the splitter to resize them, a focus rectangle is shown during and after the resize event. Is there any way to prevent this? I...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
4
by: RobinS | last post by:
I am drawing a rectangle on a picture that has already been drawn on the graphics area (a user control). It works something like this: //in the MouseDown event m_isDragging = true; m_oldX =...
1
by: PeteCresswell | last post by:
I've got a TreeView that's loaded with names. Screen snap at http://tinyurl.com/329a8m A yellow rectangle appears as the cursor navigates the tree list. If the cursor is left on an entry, the...
1
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.