Calculation of boxes / Volume / Size
I need to put m boxes of n different sizes inside another rectangular box!
What is the calculation to obtain the smallest box (with the lowest volume possible) based on the m boxes of n different sizes?
I know the size of the boxes m, and also know how many are.
Keep in mind that for example:
two boxes with 3 x 3 x 3
only better organized in a box 6 x 3 x 3
and the formula works well:
sum of the smallest measure X the largest measure X the other major measure
Another example is two boxes:
4 X 5 X 7
6 X 8 X 2
sum of the smallest measure = (4 + 2) X (8) X (7)
However, when we have 10 boxes for example, this does not work at all!
How to organize, and what is the volume of the smallest possible box based on the dimensions of the boxes I have?
I do not know if this question can only be answered with basic tools (I tried via optimization (Derivative) and I could not)
I need to make a calculation via PHP from mailboxes! And I can not solve the problem Mathematically before transforming into code
I don't have problems with PHP code, i have problems with Math, i don`t know how to solve this problem with the best way possible!
I have used the code below, but as explained, it will not work for a lot of boxes, only for 2 or 3.
$length = 0;
$height = 0;
$width = 0;
foreach ($products as $key => $product) {
for ($quantity = 0; $quantity < $product['quantity']; $quantity ++) {
$length = $length + (float) $product['dimension'][0];
}
if($product['dimension'][1] > $height) {
$height = (float) $product['dimension'][1];
}
if($product['dimension'][2] > $width) {
$width = (float) $product['dimension'][2];
}
}
$box = ($length . ' x '. $height . ' x '. $width);
Related
I've been searching this for a long time now and I want to know if any of you has some kind of resource or knowledge of some kind of algorithm that can take an image and return the percentage of most significant colors in an image. But not any color, I want to make the percentages fit with a predefined constant palette of 12 colors (the same you use in image search on google for sorting).
However, the script I made works in the sense that it gets the colors that are most PRESENT, but not necessarily most significant.
For example, take this image of the first black hole. It has mostly only black with some smudges of red/white/yellow/brown colors. But in proportions these are considered as almost nothing by checking only their amount.
Example: red = 1%, yellow = 3% and black = 96% (not exact values but accurate).
The problem is that even though they are not the most in amount, they are clearly the MAIN colors in the image as for how the human eye sees them. Is there an algorithm for that or a technique? Thank you for reading.
Let's say your image has resolution WxH.
You say you have a palette of 12 colors.
In order to make an algorithm that sort this colors based on how much they appear in the image, you could create 3 variables:
A global counter counter of valid colors within the image;
array of frequencies with length equals to palette size;
array of colors representing the palette.
A possible algorithm could be:
for(int i = 0; i < W; i++){
for(int j = 0; j < H; j++){
Color dif = new Color(255, 255, 255, 1);
Color currDif;
int minIndex = -1;
for(int k = 0; k < palette.length; k++){
currDif = palette[k] - image[i][j];
if(dif > currDif){
dif = currDif;
minIndex = k;
}
}
if( CloseEnough(dif, palette[minIndex]) ){
frequency[minIndex]++;
counter++;
}
}
}
Then, to verify the percentages, one can just:
for(int i = 0; i < 12; i++){
print("Color i appears (Palette[i] / counter) %");
}
I considered if you find a color that is not close enough to any color in the palette, you ignore it, but obviously you can consider it by simply incrementing the counter anyway, so these colors will be the percentage left.
The functions CloseEnough and < are of your choice to make, but they could work like this:
bool CloseEnough(Color c1, Color c2){
return abs(c1.r - c2.r) < 30 && abs(c1.g - c2.g) < 30 && abs(c1.b - c2.b) < 30;
//note that 30 can be quite small
//this value can be modified based on testing and result quality
}
//the closer to black a color is, the smaller it will be
bool < (Color c1, Color c2){
return (c1.r + c1.g + c1.b) < (c2.r + c2.b + c2.g);
}
There may be better solutions out there, but hope it helps.
What I want to do is generate a float between 1.00 to 200.00, let's call this number X. The X determines how much a user "wins", think of it like a multiplier on a casino. The user bets 10$, X is 23.21, the user wins 10*23.21.
If the house and the user should have the same odds (+/- 0) in the long run, the chance of X being, for example, 200.00 should be 1/199 - 1/200. This means Y=1/(X-1) - 1/X where Y = percentage of the chance of X.
The percentage Y should be randomized and I was thinking to do a frand(0, 100, 15). Where:
function frand($min, $max, $decimals = 0)
{
$scale = pow(10, $decimals);
return mt_rand($min * $scale, $max * $scale) / $scale;
}
This means we will know Y, and therefor the next step should be using Y=1/(X-1) - 1/X to determine X. However, I do not know how I can achieve this in PHP. Other ways for the same function:
Y = 1/(X - 1) - 1/X
X^2 - X = 1/Y
X(X - 1) = 1/
So, my question is; how do I solve the X OR is there a better way to achieve what I am trying?
I currently have a map that is 1600 x 1600 stored in MySQL(2,560,000 records). I'm rendering a simple 25x25 map out to users for interaction. Users can "claim" tiles on this map. I'd like to be able to calculate the number of open faces for tiles owned by a given user. I can divide this out by the total tiles owned to determine an arbitrary efficientcy rating.
All map coordinates are simply stored as X/Y values.
I'm looking for something that can potentially process an array of said X/Y values and determine how many open faces are accessible for each owned group. For example...
0 = player
x x x x x
x x 0 x x
x x x x x
4 open faces
x x x x x
x x 0 x x
x x 0 x x
x x x x x
6 open faces
x x x x x
x x x 0 x
x x 0 x x
x x x x x
8 open faces
Right now I'm doing some inefficient array looping to calculate this out. I have a simple counter, then I'm looping through an array of all values and am looking for values +-1 in each direction of X and Y to reduce the count. Each loop either adds 0-4 to the total counter based on the number of finds. The inherent problem with this method is that as a group grows, it will take longer and longer to calculate out. Since it's possible for one group to consume 20,000 points o rmore, it's quite a burden.
Any help is greatly appreciated.
One approach would involve creating a Point class. For example:
class Point {
public $x;
public $y;
public function __construct($x, $y){
$this->x = $x;
$this->y = $y;
}
public function getNeighbors(){
// TODO: What if we are at the edge of the board?
return array(
new Point($x+1, $y+1),
new Point($x+1, $y-1),
new Point($x-1, $y+1),
new Point($x-1, $y-1),
);
}
}
Create instances from that class for each point occupied by a user:
// Generate array of Points from database
$user_points = array(new Point(134, 245), new Point(146, 456));
Iterate through to generate all the neighbors:
// Get a flat array of neighbor Points
$neighbors = array_merge(array_map(function($point){
return $point->getNeighbors();
}, $user_points));
// TOOD: Remove Points that are equal to values in $user_points
Then, lastly, submit a COUNT query for the "neighbor" points to determine how many are occupied by other users and remove those from the total.
(Note: I've added TODOs where more work is to be done.)
The inherent problem with this method is that as a group grows, it will take longer and longer to calculate out.
You should consider using an in-memory key-value store, like Redis. But yes, the look-up time (for occupied blocks), in time complexity, appears linear with regard to the number of entries.
Here's the final block of simple code I came up with to determine the geo efficiency. Some names of things have been changed. :P
I'm running with notices on, and everything's ajax, so I decided to go with single isset checks on a multi-dimensional instead of something else.
$sql = 'SELECT map_x, map_y FROM Map WHERE person_id = :person_id';
$query = $DB->prepare($sql);
$query->execute(array(':nation_id' => $this->person_id));
$counter = 0;
$coords = array();
while($row = $query->fetch())
{
++$counter;
$coords[$row['map_x']][$row['map_y']] = 1;
}
$faces = 0;
foreach($coords as $x => $batch)
{
foreach($batch as $y => $junk)
{
$hits = 4;
if(isset($coords[$x + 1][$y]))
{
--$hits;
}
if(isset($coords[$x - 1][$y]))
{
--$hits;
}
if(isset($coords[$x][$y - 1]))
{
--$hits;
}
if(isset($coords[$x][$y + 1]))
{
--$hits;
}
$faces += $hits;
}
}
I'm fairly new to PHP and I'm trying to design a car's gear ratio calculator that takes certain vehicle's dimensions as input. One way to calculate them is trough using geometric progression method. Below are the codes I'm trying to run:
<?
$alowgearf = 14.37;
$ahigear = 3.293;
$noOfGear = 5;
$loopLimit = $noOfGear - 2;
$gears = array(0 => "$alowgearf", "$noOfGear - 1" => "$ahigear");
$nlnh = $alowgearf/$ahigear;
$cgp = pow($nlnh, 1/($noOfGear - 1));
for ( $n = 1; $n <= $loopLimit ; $n++ ) {
$m = $n - 1;
$gear = $gears[$m]/$cgp;
$gears[$n] = $gear;
}
print_r($gears);
?>
The formula for the GPM method of calculating gear ratio is as follows :
N[i+1] = N[i]/Cgp,
where N is gear ratio, i is the gear number and Cgp is the geometric progression constant.
By supplying the formula with the lowest and highest gear ratio and the desired number of gears, we can calculate the rest of the gear ratios.
The problem is, the codes above outputs nothing. I've been trying to fix the code but I can't detect any problem with them. I've been toying with other loop function such as do-while but the results are the same. What's the problem? What I've been doing wrong?
I have a problem drawing different functions with PHP (GD, of course).
I managed to draw different functions but whenever the parameters of the function change - the function floats wherever it wants.
Let us say that I have a first function y=x^2 and I have to draw it from -5 to 5. This means that the first point would be at (-5;25). And I can move that to whatever point I want if I know that. But if I choose y=2x^2 with an interval x=(-5;5). The first point is at (-5;50). So I need help in calculating how to move any function to, let's say, (0;0).
The functions are parabola/catenary alike.
What you want to do is find the maximum boundaries of the graph you are making. To do this you have to check each inflection point as well as the range bounds. Store each coordinate pair in an array
Part 1 [Range Bounds]:
Collect the coordinates from the range bounds.
<?php
$ybound[] = f($minX);
$ybound[] = f($maxX);
Part 2 [Inflections]:
This part is more difficult. You can either have a series of equations to solve for inflections for each type of parabola, or you can just brute force it. To do this, just choose a small increment, (what ever your small increment is for drawing the line), I will use 0.1
<?php
for($x = $minX; $x <= $maxX; $x += 0.1) {
$ybound[] = f($x);
}
Note, if you brute force, you can skip Part 1, otherwise, it would be faster if you could figure out the inflections for the scope of your project
Part 3 [Min Max]:
Now you get the min and max values from the array of possible y values.
<?php
$minY = min($ybound);
$maxY = max($ybound);
Part 4 [Shift]:
Now that you have this, it should be very simple to adjust. You take the top left corner and set that to 0,0 by adjusting each new coordinate to that value.
<?php
$shiftX = -$minX;
$shiftY = $maxY;
With this info, you can also determine your image size
<?php
$imageX = $maxX - $minX;
$imageY = $maxY - $minY;
Then as you generate your coordinates, you will shift each one, by adding the shift value to the coordinate.
<?php
for($x = -$minX; $x <= $maxX; $x += 0.1) {
$ycoor = $shiftY - f($x);
$xcoor = $x + $shiftX;
//draw ...
}
Drawing the axis is also easy,
<?php
$xaxis = $shiftY;
$yaxis = $shiftX;
(I think I have all my signs correct. Forgive me if they are off)
You first need to determine the bounding box of your function. Then, you calculate the width and the height, and you normalize so it fits into a rectangle whose top left coordinate is (0,0). Maybe you will also need to scale the figure to get it at a specific size.