• ## Sign in to Neowin Faster!

Create an account on Neowin to contribute and support the site.

# C# Best way to determine the # of times a single product could fit within a

## Recommended Posts

Kalint    228

Math time!

So I'm working on a project where you have those plastic adjustable compartment boxes, and you want to fill one of those compartments with a certain product. I want to figure out how many can fit instead said box.

At the most basic level, I could do.

`		public static int VolCalc(int mLength, int mWidth, int mHeight, out int mVol)		{			// Solve for product and compartment volume.			mVol = mLength * mWidth * mHeight;			return mVol;		}		public static int TotalCalc(int mPVol, int mBVol, out int mTot)		{			// Solve for # of times a product can fill the box.			var pMultiply = 0;			for (mTot = 0; pMultiply <= mBVol;)			{				mTot++;				pMultiply = mPVol * mTot;			}			// Since I'm using integers, remove the last one to account for <=.			mTot -= 1;			return mTot;		}[/CODE]`

Would you consider this the best way to approach this problem?

#### Share this post

##### Share on other sites
lunamonkey    114

Math time!

So I'm working on a project where you have those plastic adjustable compartment boxes, and you want to fill one of those compartments with a certain product. I want to figure out how many can fit instead said box.

At the most basic level, I could do.

`		public static int AreaCalc(int mLength, int mWidth, int mHeight, out int mArea)		{			// Solve for product and compartment area.			mArea = mLength + mWidth + mHeight;			return mArea;		}		public static int TotalCalc(int mPArea, int mBArea, out int mTot)		{			// Solve for # of times a product can fill the box.			var pMultiply = 0;			for (mTot = 0; pMultiply <= mBArea;)			{				mTot++;				pMultiply = mPArea * mTot;			}			// Since I'm using integers, remove the last one to account for <=.			mTot -= 1;			return mTot;		}[/CODE]`

Would you consider this the best way to approach this problem?

The first function would be volume for a start, and it would be the product (multiply), not an addition.

Then just divide the total volume of the room, by the smaller boxes volume.

Room is say 4 * 4 * 4 = 64 squares in volume.

The box is 2 * 2 * 2 = 8 squares in volume

64/8 = 8 boxes in total.

The real issue is dealing with partial boxes, so you might have to use some MOD in there. The calc would try to stretch or squeeze boxes, so you can only deal in integer boxes in each direction.

Do your own homework....

#### Share this post

##### Share on other sites
Kalint    228

Oh yeah volume duh 3d.

#### Share this post

##### Share on other sites
Kalint    228

The first function would be volume for a start, and it would be the product (multiply), not an addition.

Then just divide the total volume of the room, by the smaller boxes volume.

Room is say 4 * 4 * 4 = 64 squares in volume.

The box is 2 * 2 * 2 = 8 squares in volume

64/8 = 8 boxes in total.

The real issue is dealing with partial boxes, so you might have to use some MOD in there. The calc would try to stretch or squeeze boxes, so you can only deal in integer boxes in each direction.

Do your own homework....

I'll look into the changes.

Do your own homework....

No.

## 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

• ### Recently Browsing   0 members

No registered users viewing this page.