Advertisement
Guest User

heya asd fasd f

a guest
Oct 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Online PHP Script Execution</title>
  4. </head>
  5. <body>
  6. <?php
  7. class Point {
  8.  
  9. public $x;
  10. public $y;
  11.  
  12. function __construct($x, $y) {
  13. $this->$x = $x;
  14. $this->$y = $y;
  15. }
  16.  
  17. function distanceTo($point) {
  18. $x2 = ($this->$x - $point->$x) * ($this->$x - $point->$x);
  19. $y2 = ($this->$y - $point->$y) * ($this->$y - $point->$y);
  20. return $x2 + $y2;
  21. }
  22.  
  23. }
  24.  
  25. class Optimization {
  26.  
  27. public $points;
  28. public $fixed;
  29.  
  30. function __construct($array) {
  31. $points = $array;
  32. $fixed = array(
  33. new Point(-1, 1),
  34. new Point(0, 1),
  35. new Point(1, 1),
  36. new Point(-1, 0),
  37. new Point(0, 0),
  38. new Point(1, 0),
  39. new Point(-1, -1),
  40. new Point(0, -1),
  41. new Point(1, -1)
  42. );
  43. }
  44.  
  45. function calcDistance() {
  46. $distance = 0;
  47. for ($i = 0; $i < 9; $i++) {
  48. $distance += $points[$i]->distanceTo($fixed[$i]);
  49. }
  50. return $distance;
  51. }
  52.  
  53. function go() {
  54. $min = 10000;
  55. for ($i = 0; $i < 9; $i++) {
  56. $d = $this->calcDistance();
  57. if ($d < $min) {
  58. $min = $d;
  59. }
  60. $points = next_permutation($points);
  61. }
  62.  
  63. }
  64.  
  65. function swap($elements, $i, $j) {
  66. $temp = $elements[$i];
  67. $elements[$i] = $elements[$j];
  68. $elements[$j] = $temp;
  69. }
  70.  
  71. function reverse($elements, $i, $j) {
  72. for ($x = 0; $x < (int)$j/2; $x++) {
  73. swap($elements, $i+$x, $j-$x);
  74. }
  75. }
  76.  
  77. function next_permutation($elements) {
  78. $i = 7;
  79. while (i >= 0 && $elements[$i] < $elements[$i+1]) {
  80. $i--;
  81. }
  82.  
  83. if ($i < 0) {
  84. reverse($elements, 0, 8);
  85. } else {
  86. $j = 8;
  87. while ($j > $i + 1 && $elements[$j] <= $elements[$i]) {
  88. $j--;
  89. }
  90. swap($elements, $i, $j);
  91. reverse($elements, $i + 1, 8);
  92. }
  93. return $elements;
  94. }
  95.  
  96. }
  97.  
  98. $points = array(
  99. new Point(-1, 1),
  100. new Point(0, 1),
  101. new Point(1, 1),
  102. new Point(-1, 0),
  103. new Point(0, 0),
  104. new Point(1, 0),
  105. new Point(-1, -1),
  106. new Point(0, -1),
  107. new Point(1, -1)
  108. );
  109.  
  110. $o = new Optimization($points);
  111. $o->go();
  112.  
  113. ?>
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement