PHP & Geometry - how to gracefully calculate the overall orientation of an object (length / width / height)?

I am trying to get PHP to configure some array values ​​so that in all instances of objects passing through the functionality, it compares the length and height of the length of these array values ​​and aligns them as best as possible.

Imagine 3 cigarette boxes. They have the same size, shape and volume, but one rises, one lies flat and the other is on the short side.

Then imagine a 4th cigarette box. It is the same length and width as other packages, however it has more packs of cigarettes inside the container, which makes it height (or width / length, depending on user input) for other packages.

The function should still try to orient the package to the source package in the array.

Visual example

. , , . , , , , , 2 . 3 , , , , , .

PHP-, .

, array_diffs array_intersects, , , 1 .

UPDATE:

, , 3 . .

, , , . , 1 2 ...

  • AxBxC ,
  • AxBxB 2 ,
  • AxAxA ,

... ...

<?php

$params['object'] = array();
/* START TEST 1 */
$params['object'][] = array(    'height'=>'30',
                                'width'=>'10',
                                'length'=>'20');
$params['object'][] = array(    'height'=>'20',
                                'width'=>'30',
                                'length'=>'30');
/* END TEST 1 */
/* START TEST 2 
$params['object'][] = array(    'height'=>'30',
                                'width'=>'30',
                                'length'=>'20');
$params['object'][] = array(    'height'=>'30',
                                'width'=>'20',
                                'length'=>'30');
/* END TEST 2 */
/* START TEST 3 
$params['object'][] = array(    'height'=>'30',
                                'width'=>'16',
                                'length'=>'14');
$params['object'][] = array(    'height'=>'30',
                                'width'=>'20',
                                'length'=>'30');
/* END TEST 3 */

echo '<PRE>';
echo 'INPUT<BR>';
print_r($params['object']);

//Check for common orientations
/* PLEASE KEEP BETWEEN HERE --- */
$rotated = array();
foreach($params['object'] as $on=>$outer){
    if(in_array($on,$rotated)){ continue; }
    $rotated[$on] = $on;
    if($oo['width']==$oo['length'] && $oo['length']==$oo['height']){ continue; }
    $oo = array('width'=>$outer['width'],
                'length'=>$outer['length'],
                'height'=>$outer['height']);
    foreach($params['object'] as $in=>$inner){
        if(in_array($in,$rotated)){ continue; }
        if($in==$on){ continue; }
        $ii = array('width'=>$inner['width'],
                    'length'=>$inner['length'],
                    'height'=>$inner['height']);
        /* --- TO HERE */

        //DETERMINE HOW TO ROTATE THESE???
        //IF ROTATED, MARK AS ROTATED[$in] TO PREVENT FURTHER ROTATIONS TO OCCUR TO THIS ITEM

        $rotated[$in] = $in;
    }
}

echo 'OUTPUT<BR>';
print_r($params['object']);

/* TEST 1 OUTPUT SHOULD BE - ALIGNED SAME ORIENTATION, BUT EACH OBJ IS DIF WIDTH
- same logic should work if any of the H/W/L are in different orders but values consistant in any 2 pairs
Array
(
    [0] => Array
        (
            [height] => 30
            [width] => 10
            [length] => 20
        )

    [1] => Array
        (
            [height] => 30
            [width] => 30
            [length] => 20
        )

)
*/
/* TEST 2 OUTPUT SHOULD BE - SAME VALUES, SO ALIGN COMPLETELY
- same logic should work if any of the H/W/L are in different orders
Array
(
    [0] => Array
        (
            [height] => 30
            [width] => 30
            [length] => 20
        )

    [1] => Array
        (
            [height] => 30
            [width] => 30
            [length] => 20
        )

)
*/
/* TEST 3 OUTPUT SHOULD BE - NO CHANGE BECAUSE LESS THAN 2 COMPATIBLE MEASUREMENTS, NO ADD TO $rotation[$in]
Array
(
    [0] => Array
        (
            [height] => 30
            [width] => 16
            [length] => 14
        )

    [1] => Array
        (
            [height] => 30
            [width] => 20
            [length] => 30
        )

)
*/

?>
+4

Source: https://habr.com/ru/post/1619764/


All Articles