Page 1 of 1 [ 10 posts ]

#### Comparison operater ( <= and >= )

 Print view Previous topic | Next topic

#### Comparison operater ( <= and >= )

Author Message
 Post subject: Comparison operater ( <= and >= )  Posted: July 16th, 2017, 10:28 pm

Joined: July 16th, 2017, 9:49 pm
Posts: 12
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:
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:

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.

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 12:44 am

Joined: June 26th, 2012, 5:38 pm
Posts: 767
Location: USA
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:
return x>= rhs.x && y>= rhs.y;

or perhaps...

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

_________________
Computer too slow? Consider running a VM on your toaster.

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 3:00 am

Joined: July 16th, 2017, 9:49 pm
Posts: 12

it complained that
Code:
" 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.

 Attachments: File comment: Zip File Containing my Shitty Code GEngine.zip [8.98 MiB] Downloaded 4 times

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 5:01 am

Joined: June 26th, 2012, 5:38 pm
Posts: 767
Location: USA
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/1145022/difference-between-global-operator-and-member-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.

_________________
Computer too slow? Consider running a VM on your toaster.

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 6:03 am

Joined: July 16th, 2017, 9:49 pm
Posts: 12
That perfectly answers my question . 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.

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 6:19 am

Joined: December 31st, 2011, 4:53 pm
Posts: 3538
Location: Japan
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?

_________________
Chili

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 10:51 am

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
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 )

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 12:06 pm

Joined: July 16th, 2017, 9:49 pm
Posts: 12
AlbinoPapa You are exactly right that is Exactly What i am Trying to do.

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

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:
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;}
}

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 2:49 pm

Joined: June 26th, 2012, 5:38 pm
Posts: 767
Location: USA
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.

_________________
Computer too slow? Consider running a VM on your toaster.

 Post subject: Re: Comparison operater ( <= and >= )  Posted: July 17th, 2017, 6:42 pm

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
While I'm sure this works, it's way more work for the CPU than needed.
Code:
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();
}

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Display posts from previous:  Sort by

 Page 1 of 1 [ 10 posts ]

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: