Sign in to follow this  

valign, halign, alignment code

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in VS2005 and C++, alignment of boxes on a screen.

class Box
{
int x;
int y;
int height;
int width;
}

i have two boxes within a bigger box. Say call the bigger box parent and the two smaller ones child boxes.

I want to code different ways of aligning the two child boxes within the parent.
1) child boxes centered within parent
2) child boxes left aligned within parent
3) child boxes right aligned within parent
4) aligning the boxes along the y-axis or x-axis of the parent
5) aligning the boxes starting from the bottom of the parent box
6) combining one or more of the ways above

// these are my flags
horizontal_align = { left, center, right }
vertical_align = { left, center, right }
axis_align(can't think of a better way to name this) = { top_to_bottom, left_to_right }

you get what i mean, this code i am sure has been done before.


Share this post


Link to post
Share on other sites
This is trivial. Assuming that your x and y coordinates are the top left center of the box, with (0,0) being the top left corner and the coordinates that you are aligning to are called leftWall, rightWall, bottomWall and topWall, the pseudocode looks something like this:

Left align: x = leftWall;
Right align: x = rightWall - width;
Middle horizontal: x = leftWall + (rightWall - leftWall - width)/2.0;

Top align: y = topWall;
Bottom align: y = bottomWall - height;
Middle vertical: y = topWall + (bottomWall - topWall - height)/2.0;

Share this post


Link to post
Share on other sites

This topic is 3457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this