rect<T> subtraction

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

rect<T> subtraction

Post by chronologicaldot »

I propose adding the following methods to the engine's rect<T> class:

Code: Select all

 
rect<T>& operator-= (const rect<T>& other)
{
this.UpperLeftCorner -= other.UpperLeftCorner;
this.LowerRightCorner -= other.LowerRightCorner;
 
return *this;
}
 
rect<T> operator- (const rect<T>& other)
{
return rect<T>(*this) -= other;
}
 
These functions return the difference in size. I find them useful, for example, when making one rectangle transition into another because the result of these functions is a rectangle containing the remaining changes.

The functions aren't an odd-fit for the engine; they do almost the same thing as operator-(position2d<T> other) and operator-=(position2d<T> other) with the exception that the same vector/position isn't being applied to both the upper and lower corner vectors.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: rect<T> subtraction

Post by mongoose7 »

The result has no interpretation as a rectangle. You really need a scalar which is ULC - ULC - LRC + LRC.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: rect<T> subtraction

Post by hendu »

I'll echo mongoose7. I'd expect that operator to produce a clipping rectangle, ie an intersection of the two, but it does something different.

Image
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: rect<T> subtraction

Post by chronologicaldot »

mongoose7 wrote:The result has no interpretation as a rectangle. You really need a scalar which is ULC - ULC - LRC + LRC.
I guess I phrased myself incorrectly - it's not returning a direct size difference (which would be a scalar) - it's returning the different in the corners.
If I'm looking to make one rectangle transition to another one, I have to identify the changes in four aspects: upper left x, upper left y, lower right x, lower right y. That's a rectangle.

I can see hendu's point, but at the same time, I'd call that function "intersection", and one that expands the rectangle "union" (depending on how it works). What the method I've given above would be called, I don't know. "Difference"? Sounds like subtraction, which is why I used that operator.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: rect<T> subtraction

Post by mongoose7 »

My point is that it is *not* a rectangle and therefore has no place in the SDK. Period.
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: rect<T> subtraction

Post by chronologicaldot »

It could still be in the SDK, just under a different function name.
The result may not be a *true* rectangle, but the return can be saved in a rect<T> class.
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Re: rect<T> subtraction

Post by Darktib »

It is not a difference: I think substracting 2 rectangles is interpreted as a boolean substraction, and with that operation, rectangle - rectangle != rectangle. Example with a cube and a sphere (in 3D):

The 2 objects:
Image

Their substraction:
Image
you can see it is neither a cube nor a sphere.

It is the same for rectangle substraction in 2D.
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: rect<T> subtraction

Post by chronologicaldot »

Once again, that's not what I meant. The return CLASS is a rect<T>. IT IS NOT A REAL RECTANGLE! :(

By the way, what you're saying is the same as what hendu is saying, except I believe that it's been done in the function constrainTo(), which, geometrically, returns the overlap of the two rectangles (I need to double-check that - I could be wrong).

I see what you're trying to say, but I'm talking about a difference in location that I would need to move a rectangle or changing scaling. Hm... I can see this is going to be annoying to explain without presenting a picture. But atm it's late, so I'll do it later. Try this thought experiment: What information would you need to make one rectangle become like another rectangle over a certain period of time. Of course, you may come up with a different solution than I did, but here's the gist of my solution:

box1 has dimensions ulc1 and lrc1. box2, which is being transitioned to, has dimensions ulc2 and lrc2. The speed of transition is Speed. Time is given by timeMs, as usual. Hence, the only thing I need to know is the four axis distances over which the change occurs, which turn out to be:
(ulc2.x - ulc1.x = distULCx)
(ulc2.y - ulc1.y = distULCy)
(lrc2.x - lrc1.x = distLRCx)
(lrc2.y - lrc1.y = distLRCy)

Then the new values of the rectangle-being-changes at timeMs are:

if ( abs( timeMs * Speed ) <= abs(distULCx) )
{ ulc1.x = timeMs * Speed; }
if ( abs( timeMs * Speed ) <= abs(distULCy) )
{ ulc1.y = timeMs * Speed; }
if ( abs( timeMs * Speed ) <= abs(distLRCx) )
{ lrc1.x = timeMs * Speed; }
if ( abs( timeMs * Speed ) <= abs(distLRCy) )
{ lrc1.y = timeMs * Speed; }

I figure it's easiest to find the values distULCx, distULCy, distLRCx, and distLRCy with the following:

(ulc2 - ulc1 => distULCx, distULCy)
(lrc2 - lrc1 => distLRCx, distLRCy)

which, of course, is a rect<T> class return, which is what I give above in the functions in my first post.

So now I ask again, if you're not going to call it "difference" (even though it's the POSITIONAL DIFFERENCE between the corners of two rectangles), what WOULD you call it?????
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: rect<T> subtraction

Post by CuteAlien »

No idea what I would call it, doesn't seem to be a very common operation. I usually just add such little tool-functions as static functions (or extern functions in a namespace) to some tool/util/helper class in my own applications. There's always a handful of application specific tool-functions you will need. But if you find some commonly used name for this kind of operation (maybe search through a few other libraries, I often do that when looking for names) we might consider adding it as well. Otherwise rather not (there is always an infinite amount of useful operations you can do with 2 points, but if it just saves typing a single line it should be common to use before we add it to avoid getting too fat interfaces).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
AReichl
Posts: 269
Joined: Wed Jul 13, 2011 2:34 pm

Re: rect<T> subtraction

Post by AReichl »

You should not overload an operator ( here '-' ) just because inside you also substract "something" from "something else".
If that operation does not have a real mathematical meaning, use a well named function instead.

Think of a programming language which has no operator overloading - what would you do?
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: rect<T> subtraction

Post by CuteAlien »

No worries, operator overloading for this won't be done.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Re: rect<T> subtraction

Post by Darktib »

AFAIK, the best name for the following operation is 'delta':

Code: Select all

 
(ulc2.x - ulc1.x = distULCx)
(ulc2.y - ulc1.y = distULCy)
(lrc2.x - lrc1.x = distLRCx)
(lrc2.y - lrc1.y = distLRCy)
 
->

Code: Select all

 
dist = rect2.delta(rect1); // or something similar
 
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: rect<T> subtraction

Post by chronologicaldot »

AFAIK, the best name for the following operation is 'delta':
Sounds right.
Post Reply