Page 1 of 1

Comparison operater ( <= and >= )

Posted: July 16th, 2017, 10:28 pm
by GracefulComet
So i am a returning fan. i seem to have lost my username and had to make a new one. cuz i was unable to search & find my user name via my email. probably deactivated due to inactivity or something. either way just made a new account. I wasn't very active anyways.

so i was previously on Old Intermediate Lessons number 6 i think i kinda just got lost because of how long the lessons were at that time. so I went on my own code path for a while.

i am now sort of creating my own 2D Game engine( or framework, which ever you prefer to call it) with the goals of being platform independent.
so i picked ups SDL2(simple direct media library) and began learning that API and have been slowly chipping away progress when i have the time.

so I'm at the point where I am creating lots of geometric math things such as Vectors and shapes, such as rectangles and circles.
what i have that is working great thus far
&operator +=
&operator -=
&operator *=
&operator /'=


now when trying to start to work on collision detection I felt like i could make my life a lot easier by
creating a less than or equal to (<=) and greater than or equal to (>=)

never had to overload these operators before so i look it up and cppreference recomends doing it like

Code: Select all

inline bool operator< (const X& lhs, const X& rhs){ /* do actual comparison */ }
inline bool operator> (const X& lhs, const X& rhs){ return rhs < lhs; }
inline bool operator<=(const X& lhs, const X& rhs){ return !(lhs > rhs); }
inline bool operator>=(const X& lhs, const X& rhs){ return !(lhs < rhs); }

so i get alot of compiler complaints when i try to do it their way
it might be because it is inside of a 2dVector class so it assumes the left hand side is a &this

so after fiddling with it based off of compile errors my code now looks like this with no errors.

Code: Select all


bool Vec2df::operator >=(Vec2df rhs){
    bool check1 = false;
    bool check2 = false;

if(x >= rhs.x){check1 = true;}
if(y >= rhs.y){check2 = true;}
if(check1 ==true)
{
   if(check2 == true)
      {
      return true;
      }else{return false;}
   }
}
rinse and repeat for <=


so to wrap this up and get to the point.
will this bite me in the ass later, since im doing it differently than the recommended way. it seems in my minor tests its working fine.
is there a better way you might recommend doing this?

i just need a outsiders perspective because after staring at code for too long my brain has gone a little numb.

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 12:44 am
by cameron
What compiler errors are you getting? It is also helpful to post the code you are having difficulties with as it helps to resolve the problem a lot faster.

That bottom code block is a little redundant as well. It would be simpler to do one of the following

Code: Select all

return x>= rhs.x && y>= rhs.y;
or perhaps...

Code: Select all

return !(x < rhs.x || y < rhs.y)

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 3:00 am
by GracefulComet
to answer the first question.

it complained that

Code: Select all

" bool&  Vec2DF::operator<=(Vec2DF lhs, Vec2DF rhs)" Function Signature MUST have a single argument
then i made the changes you see in the last code section of my previous post.

second. I totally agree posting the code is usually the fastest. but i was hesitant Because i am Restructuring it and rebuilding some less friendly code... to be honest i started restructuring it after i found i introduced a memory leak. but that wasn't why i was posting here

Kind of working on a big commit for its github repo.

Moving onto your advise. only reason did my nest if statements to achieve my return on the function was personal readability. i don't think their would be any downside to my way vs. your way except my way uses a little more stack memory( or is it the heap... i get memory types mixed up if i dont have a visual in front of me)


i could provide the vmath.h and vmath.cpp but i just wanted to know if i was misreading the compiler error because my code couldn't match the Internets code without errors.

or i could just commit as is and you can sift through my awful code. but you would need CMake and SDL2 and SDL2_image ....
or maybe i could load up a windows VM and set it up in Visual Studio and zip it.... yeah sharing the code sound like a huge pain in the ass the more i think about it.
( im not useing windows for coding instead i am in Linux)


im sorry if im being very difficult.

EDIT it seems i already had a VM with windows with a working copy of the project. just had to copy and paste the files and over write. i lucked out.

Instructions: extract Zip file. open the .sln file i recommend building with the option RelWithDebugInfo. only x64 available right now sorry.
It will fail complaining about some dll. in the lib/x64 folder, copy and paste all of the .dll files into
Graceful-Engine-Master/RelWithDebugInfo
also copy and past the Assets folder into Graceful-Engine-Master/RelWithDebugInfo

nothing exciting. right now all it has is a single sprite that you can move with left right up and down arrow keys.

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 5:01 am
by cameron
Ok, I looked through your first post a little bit more thoroughly and think I see the problem. I don't have a lot of time right now and if this doesn't help ill look at the code tomorrow morning if possible. Try friending those global operators in the class. This should give them access to the class object.

Anyways... what you are doing now works great in most circumstances. (just fine for your circumstance)

In other cases where the operator takes a type that is different from the type of itself, it may be beneficial to use globals.

Take this as a reference https://stackoverflow.com/questions/114 ... r-operator

Consider a scenario where lhs = class with overloaded operators, and rhs is an integral type. Basically, when lhs is the class it works fine, but when rhs is first the operator will not be invoked and will result in an error.

The difference between what you had and what I had is a little bit more than extra stack space. Your's results in (extra) branching as well. The compiler may be able to optimize it out. It generally isn't a big deal, and if it is more readable for you that's great. For me; however, the one I posted is cleaner.

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 6:03 am
by GracefulComet
That perfectly answers my question 8-) . looks like now i can

A. understand why in class fits my needs perfectly.

B. if i need to expand to work with other types that are not Left side Vec2Df how to fix it by useing there operator overload as a global.


I appreciate the time. and explanation. i was having some code tunnel vision but now i understand.

now i can get back to adding more functionality to my math functions, fix my broken gofactory( game object manager) and find that memory leak.its probably in the collision stuff that i plan on deleting and doing over from scratch.

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 6:19 am
by chili
Looks like Cam got you sorted there. I'm a little more interested in the premise of what you are trying to do there. Usually we don't implement comparison for vectors, because it is ambiguous. Unlike with scalars, there is no single obvious ordering. How are you planning to order your vectors and what sorts of applications do you think this ordering will have?

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 10:51 am
by albinopapa
The first thing that comes to mind is if a point is inside a rectangle.

if( point >= topleft && point <= bottomright )

as opposed to
if(point.x >= topleft.x && point.x <= bottomright.x &&
point.y >= topleft.y && point.y <= bottomright.y )

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 12:06 pm
by GracefulComet
AlbinoPapa You are exactly right that is Exactly What i am Trying to do. :D

then i was going to move onto check if a rectangle has any 4 points of another rectangle inside of it as some cheap collision detection.

( after some deep thinking i now am not sure if this is the best possible answer. for example i am pretty sure this would fail if a really thin tall rectangle was intersecting with wide flat rectangle, making a plus sign. my collision detection i was suggesting would fail.)

after starting to write something that resembled AlbinoPapas second code snippet i thought it would be just easier to made a comparison operator instead of writing that gnarly if statement 4 different times. and easier to read too.

last night trying to go to sleep i wondered if my logic/method might have been flawed though for my code. ambiguous as you put it chili. you might be right, it might come bite me in the ass :lol:

ultimately after coding on my own following all kinds documentation and what not, i just needed someone to discuss code with. since nobody i know programs, and if they do its some old html or some shit.

these tutorials and forums are the shit and a god save. keep up all of this good shit. :)

EDIT:
@chili this is some of the new code i came up with for detecting if a rectangle is colliding with another. i also include some helper functions for my readability and to make it easier for my little monkey brain to reason with
I thought it was pretty good reason to make <= and >= operator overloads. Please if say something if any of this codes logic is flawed.

Code: Select all

Vec2DF Rect::GetTopLeft(){return position;}
Vec2DF Rect::GetTopRight(){return (position += Vec2DF(0.0f, size.y));}
Vec2DF Rect::GetBotLeft(){return (position += Vec2DF(size.x,0.0f));}
Vec2DF Rect::GetBotRight(){return position += size;}

bool Rect::RectColisionCheck(Rect compare){
if(((compare.GetTopLeft() >= this->GetTopLeft())&&(compare.GetTopLeft() <= this->GetBotRight()))
 || ((compare.GetTopRight() >= this->GetTopLeft())&&(compare.GetTopRight() <= this->GetBotRight()))
 || ((compare.GetBotLeft() >= this->GetTopLeft())&&(compare.GetBotLeft() <= this->GetBotRight()))
 || ((compare.GetBotRight() >= this->GetTopLeft())&&(compare.GetBotRight() <= this->GetBotRight()))
        ){return true;}else{return false;}
}

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 2:49 pm
by cameron
Glad I was able to help out. I have been out of c++ for some time now due to family/work/school reasons and I am trying to retain it. :D

Re: Comparison operater ( <= and >= )

Posted: July 17th, 2017, 6:42 pm
by albinopapa
While I'm sure this works, it's way more work for the CPU than needed.

Code: Select all

	bool Rect::RectColisionCheck( Rect compare )
	{
		// return 
		// 	compare.position.x <= position.x + size.x && compare.position.x + compare.size.x >= position.x &&
		// 	compare.position.y <= position.y + size.y && compare.position.y + compare.size.y >= position.y;
		// Looking for a pattern, you can now say
		return compare.GetTopLeft() <= this->GetBotRight && compare.GetBotRight() >= this->GetTopLeft();
	}