How to flip an expression

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Radical
Posts: 38
Joined: January 15th, 2017, 9:16 pm
Location: Ontario

How to flip an expression

Post by Radical » December 19th, 2017, 6:00 am

I feel like you should create multiple sections of the forum, so I don't have to feel bad about clogging it up with my own content when I submit a game or ask a question. I can just put it in it's own little "quick questions" place. But that's besides the point.

In case my code is hard to read, what I am doing is drawing an angry eye (a right angled triangle) on one of my characters, and if his direction is 1(right) then it draws it normally. But if his direction is 2(left), then it mirrors the image.

Code: Select all

	ex2 = 0;
	ey2 = 0;

	if (d == 1)
	{
		while (ey2 <= 6)
		{
			gfx.PutPixel(x + (w / 2) + (ex + ex2), y + ey + ey2, 0, 0, 0);
			ex2++;
			if (ex2 > ey2)
			{
				ex2 = 0;
				ey2++;
			}
		}
	}
	else if (d == 2)
	{
		while (ey2 <= 6)
		{
			gfx.PutPixel(x + (w / 2) + -(ex + ex2), y + ey + ey2, 0, 0, 0);
			ex2++;
			if (ex2 > ey2)
			{
				ex2 = 0;
				ey2++;
			}
		}
	}
What I am trying to do is avoid just duplicating the code, and instead make everything work with a single block. The only difference right now between the two is the little negative symbol in front of the (ex + ex2).

Of course, I could just merge ex and ex2 into a single unit and say "ex = -ex", but I want to keep both variables for convenience reasons. Can this be done?

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: How to flip an expression

Post by MrGodin » December 19th, 2017, 6:17 am

you might try this

Code: Select all

int result ;
// if d == 1 then result = 1 otherwise result = -1
 d == 1 ? result = 1 : result = -1;
// result * (ex + ex2) will make it positive or negative
while (ey2 <= 6)
      {
         gfx.PutPixel(x + (w / 2) + (result * (ex + ex2)), y + ey + ey2, 0, 0, 0);
         ex2++;
         if (ex2 > ey2)
         {
            ex2 = 0;
            ey2++;
         }
      }
Curiosity killed the cat, satisfaction brought him back

User avatar
Radical
Posts: 38
Joined: January 15th, 2017, 9:16 pm
Location: Ontario

Re: How to flip an expression

Post by Radical » December 19th, 2017, 6:21 am

Ahhh, that would work! I'm upset I didn't think of that myself. Thank you very much.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: How to flip an expression

Post by MrGodin » December 19th, 2017, 6:32 am

No problem
Curiosity killed the cat, satisfaction brought him back

User avatar
Radical
Posts: 38
Joined: January 15th, 2017, 9:16 pm
Location: Ontario

Re: How to flip an expression

Post by Radical » December 19th, 2017, 3:16 pm

Can I ask you a question MrGodin? (or anyone else reading this)

I try to avoid creating unnecessary ints in my project. By the end of my creations I always have dozens of single purpose ints like this(I realise your "result" int can be created and destroyed in the same function, but I mean in general) lying around everywhere. Is there a way around this, or do I just need to pull up my underwear, and accept that this is how it's going to be?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: How to flip an expression

Post by albinopapa » December 19th, 2017, 5:03 pm

Depending on what you are referring to, there are ways to avoid so may loose variables.

1) Throw away variables should not be global or stored in a class or struct. Instantiate them inside of functions like the int result in MrGodin's example, though I prefer to initialize on the same line as declare. const int return = d == 1 ? 1 : -1;
For one, this allows me to make my temporary variables const ( performance optimization, safety check ), and two I don't have to remember to initialize them before using them ( compiler error if used before initializing ).

2) Use for loops instead of while loops when possible. For loops allow you to initialize a variable inside the loops declaration and won't live past the loop, unlike the while loop where you must declare before the while and they live after the while.

3) Use the fact that functions that return values can be used directly instead of storing to a named temporary. const int result = 5 + Add(7,3); // result = 15;
The result of Add(7,3) is stored in memory, then added to 5 and that result is stored in const int result.

If you only need it inside a function, create it inside the function.
If you only need it inside a loop, create it inside a loop.
You can even use curly braces {} to limit the scope of the ints, so you can reinstantiate them with the same name.

Code: Select all


// i only lives until end of loop
for( int i = 0; i < numPersons; ++i )
{
    
    {
        // Person and name live only in this block
        const Person& person = boy_name_list[i];
        const char* name = person.GetName();
        std::cout << "The boy at index " << i  << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed

    {
        // Person and name live only in this block
        const Person& person = girl_name_list[i]; 
        const char* name = person.GetName();
        std::cout << "The girl at index " << i << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed
}
// i is destroyed
I prefer to be a little verbose in my coding. It allows me to step through the code mentally and literally while debugging and see what the values are as I go. Plus, if you have functions returning values to functions as parameters, then you have to step through each of those functions before you get to the top level function and by that time, you've forgotten what the values were that were being passed in.
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

User avatar
Radical
Posts: 38
Joined: January 15th, 2017, 9:16 pm
Location: Ontario

Re: How to flip an expression

Post by Radical » December 19th, 2017, 6:33 pm

That is definitely some good advice. I will go back and alter my whole project. Thanks a bunch papa.

As far as taking notes goes (if that's what you meant by verbose), I do it sometimes, but I almost always forget, or it seems pointless at the time. I will work on it.

goldengamesTM

Re: How to flip an expression

Post by goldengamesTM » December 19th, 2017, 7:02 pm

I May Have Read You Post Wrong, But If You Meant This Shit

Code: Select all

/***********************/
/****user variables*****/
int new_int = 0;
int int2 = 0;
int int3_the_revenge = 0;
int int4_Another_sequel = 0;
int wow_we're_at_5 = 0;
int Another_1 = 0;
int doubt_you_made_it = 0;
int This_Far = 0;
int Still_going = 0;
int y = 0;
int x = 0;
int x1 = 0;
int y1 = 0;
int I_got_bored = 0;
/*********************/
Then You Can Do This I Guess:

Code: Select all

/***********************/
/****user variables*****/
int new_int, int2, int3_the_revenge, int4_Another_sequel, wow_we're_at_5, Another_1 , doubt_you_made_it, This_Far , Still_going , y, x,  x1, y1, I_got_bored;
/**********************/

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: How to flip an expression

Post by albinopapa » December 19th, 2017, 9:53 pm

Radical wrote:That is definitely some good advice. I will go back and alter my whole project. Thanks a bunch papa.

As far as taking notes goes (if that's what you meant by verbose), I do it sometimes, but I almost always forget, or it seems pointless at the time. I will work on it.
Verbose for me is:

Code: Select all

// i only lives until end of loop
for( int i = 0; i < numPersons; ++i )
{    
    {
        // Person and name live only in this block
        const Person& person = boy_name_list[i];
        const char* name = person.GetName();
        std::cout << "The boy at index " << i  << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed

    {
        // Person and name live only in this block
        const Person& person = girl_name_list[i]; 
        const char* name = person.GetName();
        std::cout << "The girl at index " << i << "is named: " << name << std::endl;
        std::cout << "They are: " << person.GetAge() << " years old." << std::endl;
    }
    // Person and name are destroyed
}
// i is destroyed
Not verbose:

Code: Select all

for( int i = 0; i < numPersons; ++i )
{
        std::cout << "The boy at index " << i  << "is named: " << boy_name_list[i].GetName() << std::endl;
        std::cout << "They are: " << boy_name_list[i].GetAge() << " years old." << std::endl;
        std::cout << "The girl at index " << i << "is named: " << girl_name_list[i].GetName() << std::endl;
        std::cout << "They are: " << girl_name_list[i].GetAge() << " years old." << std::endl;
}
// i is destroyed
Yeah, I meant the code being verbose, I probably used the word incorrectly. Anyway, I prefer these little steps of assigning results to intermediate temporary variables for the reasons I mentioned in the last post. As far as comments go, I really only use them when I think I might forget what the code does in a few weeks.
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

User avatar
Radical
Posts: 38
Joined: January 15th, 2017, 9:16 pm
Location: Ontario

Re: How to flip an expression

Post by Radical » December 19th, 2017, 10:03 pm

Ah I see, so you think it's worth it to make less optimal code in exchange for readability? Honestly I prefer to code that way, I just have been trying to do it as little as possible because it felt sloppy to me.

I mean, I guess I don't really know how much of a hit it would make to the frame rate. But. for example, people talk about const making things more speedy and optimized, so I figured shorthand hard-to-read code would do the same.

@goldengamesTM lol I guess I could

Post Reply