Алгоритм расчета координат для редактора 3 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: Алгоритм расчета координат для редактора 3 fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Алгоритм расчета координат для редактора 3.php

  1. <?php
  2.  
  3. $coefficient = 300;
  4.  
  5. $levels = [
  6.     0 => [
  7.         "sector_id" => 61,
  8.         "name" => "Parter 1",
  9.         "position" => [
  10.             "radius"=> 0,
  11.             "x" => 0,
  12.             "y"=> 0
  13.         ],
  14.         "controlPoints" => [
  15.             [
  16.                 "radius" => 0,
  17.                 "x" => 198,
  18.                 "y" => 120
  19.             ],
  20.             [
  21.                 "radius" => 0,
  22.                 "x" => 198,
  23.                 "y" => 173
  24.             ],
  25.             [
  26.                 "radius" => 0,
  27.                 "x" => 198,
  28.                 "y" => 227
  29.             ],
  30.             [
  31.                 "radius" => 0,
  32.                 "x" => 198,
  33.                 "y" => 281
  34.             ],
  35.             [
  36.                 "radius" => 0,
  37.                 "x" => 216,
  38.                 "y" => 281
  39.             ],
  40.             [
  41.                 "radius" => 0,
  42.                 "x" => 235,
  43.                 "y" => 281
  44.             ],
  45.             [
  46.                 "radius" => 0,
  47.                 "x" => 254,
  48.                 "y" => 282
  49.             ],
  50.             [
  51.                 "radius" => 0,
  52.                 "x" => 254,
  53.                 "y" => 120
  54.             ],
  55.             [
  56.                 "radius" => 0,
  57.                 "x" => 198,
  58.                 "y" => 120
  59.             ],
  60.             [
  61.                 "radius" => 0,
  62.                 "x" => 198,
  63.                 "y" => 120
  64.             ]
  65.         ],
  66.         "seats" => [
  67.             0 => [
  68.                 "additional_properties" => [
  69.                     "row" => "1",
  70.                     "place_name" => "3",
  71.                     "place_count" => 1,
  72.                     "price_type" => 1,
  73.                     "sector_id" => 61
  74.                 ],
  75.                 "position" => [
  76.                     "radius" => 0,
  77.                     "x" => 80,
  78.                     "y" => 40
  79.                 ],
  80.             ],
  81.             1 => [
  82.                 "additional_properties" => [
  83.                     "row" => "1",
  84.                     "place_name" => "1",
  85.                     "place_count" => 1,
  86.                     "price_type" => 1,
  87.                     "sector_id" => 61
  88.                 ],
  89.                 "position" => [
  90.                     "radius" => 0,
  91.                     "x" => 40,
  92.                     "y" => 40
  93.                 ],
  94.             ],
  95.             2 => [
  96.                 "additional_properties" => [
  97.                     "row" => "1",
  98.                     "place_name" => "2",
  99.                     "place_count" => 1,
  100.                     "price_type" => 1,
  101.                     "sector_id" => 61
  102.                 ],
  103.                 "position" => [
  104.                     "radius" => 0,
  105.                     "x" => 60,
  106.                     "y" => 40
  107.                 ],
  108.             ],
  109.             3 => [
  110.                 "additional_properties" => [
  111.                     "row" => "1",
  112.                     "place_name" => "4",
  113.                     "place_count" => 1,
  114.                     "price_type" => 1,
  115.                     "sector_id" => 61
  116.                 ],
  117.                 "position" => [
  118.                     "radius" => 0,
  119.                     "x" => 100,
  120.                     "y" => 40
  121.                 ],
  122.             ],
  123.             4 => [
  124.                 "additional_properties" => [
  125.                     "row" => "1",
  126.                     "place_name" => "5",
  127.                     "place_count" => 1,
  128.                     "price_type" => 1,
  129.                     "sector_id" => 61
  130.                 ],
  131.                 "position" => [
  132.                     "radius" => 0,
  133.                     "x" => 120,
  134.                     "y" => 40
  135.                 ],
  136.             ],
  137.         ]
  138.     ],
  139.     1 => [
  140.         "sector_id" => 68,
  141.         "name" => "Balkon 2 top",
  142.         "position" => [
  143.             "radius"=> 0,
  144.             "x" => 0,
  145.             "y"=> 0
  146.         ],
  147.         "controlPoints" => [
  148.             [
  149.                 "radius" => 0,
  150.                 "x" => 240,
  151.                 "y" => 76
  152.             ],
  153.             [
  154.                 "radius" => 0,
  155.                 "x" => 240,
  156.                 "y" => 80
  157.             ],
  158.             [
  159.                 "radius" => 0,
  160.                 "x" => 240,
  161.                 "y" => 83
  162.             ],
  163.             [
  164.                 "radius" => 0,
  165.                 "x" => 240,
  166.                 "y" => 86
  167.             ],
  168.             [
  169.                 "radius" => 0,
  170.                 "x" => 266,
  171.                 "y" => 86
  172.             ],
  173.             [
  174.                 "radius" => 0,
  175.                 "x" => 291,
  176.                 "y" => 86
  177.             ],
  178.             [
  179.                 "radius" => 0,
  180.                 "x" => 317,
  181.                 "y" => 86
  182.             ],
  183.             [
  184.                 "radius" => 0,
  185.                 "x" => 317,
  186.                 "y" => 76
  187.             ],
  188.             [
  189.                 "radius" => 0,
  190.                 "x" => 240,
  191.                 "y" => 76
  192.             ],
  193.             [
  194.                 "radius" => 0,
  195.                 "x" => 240,
  196.                 "y" => 76
  197.             ]
  198.         ],
  199.         "seats" => [
  200.             0 => [
  201.                 "additional_properties" => [
  202.                     "row" => "1",
  203.                     "place_name" => "1",
  204.                     "place_count" => 1,
  205.                     "price_type" => 1,
  206.                     "sector_id" => 68
  207.                 ],
  208.                 "position" => [
  209.                     "radius" => 0,
  210.                     "x" => 40,
  211.                     "y" => 40
  212.                 ],
  213.             ],
  214.             1 => [
  215.                 "additional_properties" => [
  216.                     "row" => "1",
  217.                     "place_name" => "2",
  218.                     "place_count" => 1,
  219.                     "price_type" => 1,
  220.                     "sector_id" => 68
  221.                 ],
  222.                 "position" => [
  223.                     "radius" => 0,
  224.                     "x" => 60,
  225.                     "y" => 40
  226.                 ],
  227.             ],
  228.             2 => [
  229.                 "additional_properties" => [
  230.                     "row" => "1",
  231.                     "place_name" => "3",
  232.                     "place_count" => 1,
  233.                     "price_type" => 1,
  234.                     "sector_id" => 68
  235.                 ],
  236.                 "position" => [
  237.                     "radius" => 0,
  238.                     "x" => 80,
  239.                     "y" => 40
  240.                 ],
  241.             ],
  242.             3 => [
  243.                 "additional_properties" => [
  244.                     "row" => "1",
  245.                     "place_name" => "4",
  246.                     "place_count" => 1,
  247.                     "price_type" => 1,
  248.                     "sector_id" => 68
  249.                 ],
  250.                 "position" => [
  251.                     "radius" => 0,
  252.                     "x" => 100,
  253.                     "y" => 40
  254.                 ],
  255.             ],
  256.             4 => [
  257.                 "additional_properties" => [
  258.                     "row" => "1",
  259.                     "place_name" => "5",
  260.                     "place_count" => 1,
  261.                     "price_type" => 1,
  262.                     "sector_id" => 68
  263.                 ],
  264.                 "position" => [
  265.                     "radius" => 0,
  266.                     "x" => 120,
  267.                     "y" => 40
  268.                 ],
  269.             ],
  270.         ]
  271.     ],
  272.     2 => [
  273.         "sector_id" => 79,
  274.         "name" => "Balkon 3 S2",
  275.         "position" => [
  276.             "radius"=> 0,
  277.             "x" => 0,
  278.             "y"=> 0
  279.         ],
  280.         "controlPoints" => [
  281.             [
  282.                 "radius" => 0,
  283.                 "x" => 384,
  284.                 "y" => 389
  285.             ],
  286.             [
  287.                 "radius" => 0,
  288.                 "x" => 341,
  289.                 "y" => 357
  290.             ],
  291.             [
  292.                 "radius" => 0,
  293.                 "x" => 382,
  294.                 "y" => 300
  295.             ],
  296.             [
  297.                 "radius" => 0,
  298.                 "x" => 426,
  299.                 "y" => 331
  300.             ],
  301.         ],
  302.         "seats" => [
  303.             0 => [
  304.                 "additional_properties" => [
  305.                     "row" => "1",
  306.                     "place_name" => "3",
  307.                     "place_count" => 1,
  308.                     "price_type" => 1,
  309.                     "sector_id" => 61
  310.                 ],
  311.                 "position" => [
  312.                     "radius" => 0,
  313.                     "x" => 80,
  314.                     "y" => 40
  315.                 ],
  316.             ],
  317.             1 => [
  318.                 "additional_properties" => [
  319.                     "row" => "1",
  320.                     "place_name" => "1",
  321.                     "place_count" => 1,
  322.                     "price_type" => 1,
  323.                     "sector_id" => 61
  324.                 ],
  325.                 "position" => [
  326.                     "radius" => 0,
  327.                     "x" => 40,
  328.                     "y" => 40
  329.                 ],
  330.             ],
  331.             2 => [
  332.                 "additional_properties" => [
  333.                     "row" => "1",
  334.                     "place_name" => "2",
  335.                     "place_count" => 1,
  336.                     "price_type" => 1,
  337.                     "sector_id" => 61
  338.                 ],
  339.                 "position" => [
  340.                     "radius" => 0,
  341.                     "x" => 60,
  342.                     "y" => 40
  343.                 ],
  344.             ],
  345.             3 => [
  346.                 "additional_properties" => [
  347.                     "row" => "1",
  348.                     "place_name" => "4",
  349.                     "place_count" => 1,
  350.                     "price_type" => 1,
  351.                     "sector_id" => 61
  352.                 ],
  353.                 "position" => [
  354.                     "radius" => 0,
  355.                     "x" => 100,
  356.                     "y" => 40
  357.                 ],
  358.             ],
  359.             4 => [
  360.                 "additional_properties" => [
  361.                     "row" => "1",
  362.                     "place_name" => "5",
  363.                     "place_count" => 1,
  364.                     "price_type" => 1,
  365.                     "sector_id" => 61
  366.                 ],
  367.                 "position" => [
  368.                     "radius" => 0,
  369.                     "x" => 120,
  370.                     "y" => 40
  371.                 ],
  372.             ],
  373.         ]
  374.     ],
  375. ];
  376.  
  377. $maxSchemaY = 0;
  378. $lastSeatXInPreviousLevel = [];
  379. $firstSeatXInLevel = [];
  380. $coefficientXByLevel = [];
  381. $data = [];
  382.  
  383. foreach($levels as $level) {
  384.     array_walk($level['controlPoints'], function($point) use (&$maxSchemaY) {
  385.         $maxSchemaY = max($maxSchemaY, $point['y']);
  386.     });
  387. }
  388.  
  389. var_dump('maxSchemaY');
  390. var_dump($maxSchemaY);
  391. foreach($levels as $level) {
  392.     $firstSeatXInLevel = [];
  393.     array_walk($level['seats'], function($seat) use (&$firstSeatXInLevel) {
  394.         if (!isset($firstSeatXInLevel['x']) //если в секторе ещё не записан первый Х или на данный момент Х больше, чем у этого места
  395.             or $firstSeatXInLevel['x'] > $seat['position']['x']) {
  396.             $firstSeatXInLevel['x'] = $seat['position']['x']; //то записываем координаты этого места в крайнее место в секторе
  397.             $firstSeatXInLevel['y'] = $seat['position']['y'];
  398.         }
  399.     });
  400.     
  401.     var_dump('firstSeatInLevel');
  402.     var_dump($firstSeatXInLevel);
  403.     foreach ($level['seats'] as $place) {
  404.         $place['position']['y'] = $maxSchemaY - $place['position']['y'] + $coefficient;
  405.         
  406.         var_dump('lastSeatXInPreviousLevel');
  407.         var_dump($lastSeatXInPreviousLevel);
  408.         if (!empty($lastSeatXInPreviousLevel) && !isset($coefficientXByLevel[$level['sector_id']])) { //если есть данные о последнем месте предыдущего сектора и ещё нет коэффициента сдвига по координате X этого сектора, то высчитываем его
  409.             $coefficientXByLevel[$level['sector_id']] = $lastSeatXInPreviousLevel['x'] - $firstSeatXInLevel['x'] + $coefficient;
  410.         } elseif (empty($lastSeatXInPreviousLevel)) $coefficientXByLevel[$level['sector_id']] = 0;
  411.         
  412.         $place['position']['x'] = $place['position']['x'] + $coefficientXByLevel[$level['sector_id']];
  413.         
  414.         if (!isset($lastSeatXInPreviousLevel['x']) //если в секторе ещё не записан последний Х или на данный момент Х меньше, чем у этого места
  415.                or $lastSeatXInPreviousLevel['x'] < $place['position']['x']) {
  416.                     $lastSeatXInPreviousLevel['x'] = $place['position']['x']; //то записываем координаты этого места в крайнее место в секторе
  417.                     $lastSeatXInPreviousLevel['y'] = $place['position']['y'];
  418.             }
  419.         
  420.             $data[$level['sector_id']][] = [
  421.                 'category' => 'place',
  422.             'type' => 'Circle',
  423.             'additional_properties' => $place['additional_properties'],
  424.             'position' => [
  425.                 'radius' => 0,
  426.                 'x' => $place['position']['x'],
  427.                 'y' => $place['position']['y'],
  428.             ]
  429.             ];
  430.     }
  431.     
  432.     var_dump('data');
  433.     var_dump($data[$level['sector_id']]);
  434. }
File Description
  • Алгоритм расчета координат для редактора 3
  • PHP Code
  • 19 Sep-2023
  • 12.78 Kb
You can Share it: