[comp.windows.x] XDrawRectangle w/ negative width/height?

mxh5795@cs.rit.edu (Michael X Houwers) (02/06/91)

Hello.  I was wondering if there are any known bugs in the function
XDrawRectangle in Xlib.  I was attempting to write a limited drawing
program (boredom is a wonderful motivator...), which, at the moment
draws lines and rectangles with a "rubberbanding" effect.  To draw a
line in the drawing window, you hold down the left mouse button and
move the mouse to the position you want for the other point.  The 
rectangle works in much the same way, by specifying one corner, then
moving the pointer to the opposite corner, rubberbanding until the
user releases the button.

The strange behavior I noticed came when I attempted to rubberband a
rectangle in any direction which would cause the width or height to
be negative.  If this happened, it appeared that a small corner of
the old rectangle (I am using GXxor to draw over the old rectangle)
is not getting redrawn correctly.  So as a result, there are lots
of small corners of rectangles running around the screen.

I looked at the Xlib Reference Manual (from O'Reilly) and found
no caveats regarding a negative width and/or height.

So, the question is:  can you specify a negative width or height for
a rectangle with no unexpected side effects, and, if that is legal,
are there any known bugs when doing this?

Any help would be greatly appreciated..

Thanks,
mike houwers
--
---------------------------------------------------------------------------
Michael Houwers                     "I'm not sleazy, I"m morally challenged"
bitnet:  MWH5795@RITVAXA            
internet:  mxh5795@cs.rit.edu

jackm@sparrow.pica.army.MIL (Jack Moskowitz) (02/06/91)

According to  O'Reilly  in  "Xlib  Reference  Manual"  under
XDrawRectangle (p151), the declarations for width and height
are unsigned.   This implies that  the values are  positive.
No negative widths or heights.

What you have to do is  keep track of the initial point  and
then if the pointer moves left  or or above this point,  you
have to  switch the  "origin" and  change sign  of width  or
height.

I do this all the time and it works fine.

> Date: 6 Feb 91 01:19:56 GMT
> From: mxh5795@cs.rit.edu (Michael X Houwers)
> Organization: Rochester Institute of Technology, Rochester, NY
> Subject: XDrawRectangle w/ negative width/height?
> Message-Id: <2124@cs.rit.edu>
> Newsgroups: comp.windows.x
> Sender: xpert-request@expo.lcs.mit.edu
> To: xpert@expo.lcs.mit.edu
> 
> Hello.  I was wondering if there are any known bugs in the function
> XDrawRectangle in Xlib.  I was attempting to write a limited drawing
> program (boredom is a wonderful motivator...), which, at the moment
> draws lines and rectangles with a "rubberbanding" effect.  To draw a
> line in the drawing window, you hold down the left mouse button and
> move the mouse to the position you want for the other point.  The 
> rectangle works in much the same way, by specifying one corner, then
> moving the pointer to the opposite corner, rubberbanding until the
> user releases the button.
> 
> The strange behavior I noticed came when I attempted to rubberband a
> rectangle in any direction which would cause the width or height to
> be negative.  If this happened, it appeared that a small corner of
> the old rectangle (I am using GXxor to draw over the old rectangle)
> is not getting redrawn correctly.  So as a result, there are lots
> of small corners of rectangles running around the screen.
> 
> I looked at the Xlib Reference Manual (from O'Reilly) and found
> no caveats regarding a negative width and/or height.
> 
> So, the question is:  can you specify a negative width or height for
> a rectangle with no unexpected side effects, and, if that is legal,
> are there any known bugs when doing this?
> 
> Any help would be greatly appreciated..
> 
> Thanks,
> mike houwers
> --
> ---------------------------------------------------------------------------
> Michael Houwers                     "I'm not sleazy, I"m morally challenged"
> bitnet:  MWH5795@RITVAXA            
> internet:  mxh5795@cs.rit.edu

veerabad@buster.uucp (Vibhu Veerabadrappa) (02/06/91)

In article <2124@cs.rit.edu> mxh5795@cs.rit.edu (Michael X Houwers) writes:
>
>
>
>Hello.  I was wondering if there are any known bugs in the function
>XDrawRectangle in Xlib.  I was attempting to write a limited drawing

I do not know if there are any bugs, but I too feel it is rather wierd. 
But if when I tried to draw a rectangle with negative width and height, 
without the rubberbanding, it works fine ... 

>program (boredom is a wonderful motivator...), which, at the moment
>draws lines and rectangles with a "rubberbanding" effect.  To draw a
>line in the drawing window, you hold down the left mouse button and

[ Stuff deleted ]

>So, the question is:  can you specify a negative width or height for
>a rectangle with no unexpected side effects, and, if that is legal,
>are there any known bugs when doing this?
>
>Any help would be greatly appreciated..

One way to eliminate the trail, as I did, is to draw the rectangle from
the "other" corner, with positive width and height.

>
>Thanks,

Sorry, I could not answer your question fully.

>mike houwers

----Vibhu.
 
>--
>---------------------------------------------------------------------------
>Michael Houwers                     "I'm not sleazy, I"m morally challenged"
>bitnet:  MWH5795@RITVAXA            
>internet:  mxh5795@cs.rit.edu

mouse@lightning.mcrcim.mcgill.EDU (02/07/91)

> So, the question is:  can you specify a negative width or height for
> a rectangle with no unexpected side effects, and, if that is legal,
> are there any known bugs when doing this?

At the protocol level, XDrawRectangle turns into a PolyRectangle
request.  The width and height for the rectangles in a PolyRectangle
requests are CARD16, which is an unsigned type.  Therefore, you
definitely are not going to get what you seem to be expecting.

The Xlib document describes the width and height arguments to
XDrawRectangle as "unsigned int", which perhaps should tell you
something :-)

Just what you *will* get depends on your Xlib, your compiler, and your
machine: when you pass negative width and/or height values to
XDrawRectangle, what does it generate in the transmitted request?  I
would guess that the most common thing is for it to blindly take the
low-order 16 bits, which usually means that negative numbers turn into
very large numbers (eg, -100 becomes 65436).

As far as I can see, it's your responsibility to make sure that the
width and height are always positive by interchanging values when
necessary.  It doesn't even take much code:

MyDrawRectangle(disp,d,gc,x,y,w,h)
Display *disp;
Drawable d;
GC gc;
int x;
int y;
int w;
int h;
{
 if (w < 0)
  { x += w;
    w = - w;
  }
 if (h < 0)
  { y += h;
    h = - h;
  }
 XDrawRectangle(disp,d,gc,x,y,(unsigned int)w,(unsigned int)h);
}

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

vrenjak@rm1.UUCP (Milan Vrenjak) (02/08/91)

In article <2124@cs.rit.edu>, mxh5795@cs.rit.edu (Michael X Houwers) writes:
|> 
|> 
|> 
|> The strange behavior I noticed came when I attempted to rubberband a
|> rectangle in any direction which would cause the width or height to
|> be negative.  If this happened, it appeared that a small corner of
|> the old rectangle (I am using GXxor to draw over the old rectangle)
|> is not getting redrawn correctly.  So as a result, there are lots
|> of small corners of rectangles running around the screen.
|> 
|> I looked at the Xlib Reference Manual (from O'Reilly) and found
|> no caveats regarding a negative width and/or height.
|> 
|> So, the question is:  can you specify a negative width or height for
|> a rectangle with no unexpected side effects, and, if that is legal,
|> are there any known bugs when doing this?
|> 
|> Any help would be greatly appreciated..
|> 
|> Thanks,
|> mike houwers
|> --

I found the same problem on Sun's sparcstation under X11R4.
Douglas Young's "The X Window System, Programming and Applications
with Xt (OSF/MOTIF Edition)" described that this scenario may
happend and inverted the x,y coordinates to make it positive.
As to why, this is all I got from p277 -

	"Some X servers do not draw polygonal figures correctly if the
	 second point is less than the first in either direction. The
	 auxillary function check_points() checks for this case
	 and reverses the coordinates if necessary." 
		... and shows this function

Hope this helps