Guest User

Untitled

a guest
Jan 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'angie.class.php'; //we need to use Angie in this class.
  4. require_once 'point.class.php'; //we need to use the point structure
  5. require_once 'triangle.class.php'; //we need to use these methods
  6.  
  7. class Buddy {
  8. protected $speed, $x, $y, $points; //these are the variables in the class
  9.  
  10. public function __construct($speed, $displace) {
  11. $this->speed = $speed; //this is his speed
  12. $this->x = $displace; //pass in the orignal x-position
  13. $this->y = 0; //he starts at 0
  14. $interval = 1;
  15. $d = 0;
  16.  
  17. $this->points = array(); //make the points variable into an array
  18. $this->points[] = array("buddy" => new Point($this->x, $this->y), "angle" => 0.0, "arctan" => atan(0) . " = arctan(0 / 50)"); //add the initial point
  19.  
  20. for ($i = 1; $i < 300; $i++) { //this will make the array 30 items long.
  21. $angle = $this->Angle(Angie::Position($i - 1), $this->points[$i - 1]["buddy"]);
  22. //get the angle of Angie's position 2 seconds ago and Buddy's position 2 seconds ago
  23.  
  24. if ($this->points[$i - 1]["buddy"]->X() - ($speed * 2 * cos($angle)) < 0) { //if the next X coordinate will be less than zero (Buddy will run into the water)
  25. $point = new Point(0.000000000000001, $this->points[$i - 1]["buddy"]->Y() + ($speed * $interval * sin($angle))); //then... set his x position to 0.000000000000001
  26. }
  27. else //else or otherwise... do the normal formula
  28. $point = new Point(
  29. ($this->points[$i - 1]["buddy"]->X() > 0 ? //if the previous point was greater than zero...
  30. $this->points[$i - 1]["buddy"]->X() - ($speed * $interval * cos($angle)) //then... take the old point minus the new hypotenuse length
  31. : $this->points[$i - 1]["buddy"]->X() + ($speed * $interval * cos($angle))), //else... take the old point plus the new hypotenuse length
  32. $this->points[$i - 1]["buddy"]->Y() + ($speed * $interval * sin($angle)) //the Y-coordinate
  33. );
  34. $this->points[] = array(
  35. "buddy" => $point, //this is the only important element in this associative array. the others are for debugging purposes.
  36. "angie" => Angie::Position($i),
  37. "rads " => $angle,
  38. "atan " => atan($angle) . " = " .
  39. "arctan((" . Angie::Position($i - 1)->Y() . " - " . $this->points[$i - 1]["buddy"]->Y() . ") / " . $this->points[$i - 1]["buddy"]->X() . ")",
  40. "cosine" => (($this->points[$i - 1]["buddy"]->X() < 0 ? "-" : "") . $speed * $interval * cos($angle)) . " = 2 * 7 * cos({$angle})",
  41. "sine " => ($speed * $interval * sin($angle)) . " = 2 * 7 * sin({$angle})"
  42. );
  43.  
  44. if ($this->points[$i]["buddy"]->Y() > $this->points[$i]["angie"]->Y()) { //if Buddy has ran farther than Angie, he has caught up to her so exit the loop (break).
  45. break;
  46. }
  47.  
  48. $d += $this->points[$i]["buddy"]->distanceTo($this->points[$i - 1]["buddy"]);
  49. }
  50.  
  51. //print_r($this->points); //this line is for debugging.
  52. echo "\nBuddy caught up with her in {$i} seconds\n"; //this tells the user after how many seconds they will meet up again.
  53. echo $this->csv($this->points);
  54. echo "\n" . Angie::Position($i)->Y();
  55. echo "\n$d\n";
  56. }
  57.  
  58. public function csv($arr) {
  59. $s="Time,Buddy X, Buddy Y, Angie X, Angie Y\n";
  60. for($x=0;$x<sizeof($arr);$x++)
  61. $s.=$x.",".$arr[$x]["buddy"]->X().",".$arr[$x]["buddy"]->Y().",0," . Angie::Position($x)->Y() . "\n";
  62. return $s;
  63. }
  64.  
  65. public function getPoint($second) {
  66. return $this->points[$second];
  67. }
  68.  
  69. public function Angle($point1, $point2) {
  70. return atan(
  71. ($point1->Y() - $point2->Y()) //take the difference between the two
  72. / $point2->X() //divide by the X-coordinate
  73. ); //return the arc tangent of the quotient
  74. }
  75. }
  76.  
  77. ?>
  78. <?php
  79.  
  80. //The class or structure of a point as defined by me, Kyle Stevenson
  81. class Point {
  82. protected $x, $y;
  83.  
  84. public function __construct($x, $y) {
  85. $this->x = $x;
  86. $this->y = $y;
  87. }
  88.  
  89. //The X coordinate in the current coordinate-point.
  90. public function X() {
  91. return $this->x;
  92. }
  93.  
  94. //The Y coordinate in the current coordinate-point.
  95. public function Y() {
  96. return $this->y;
  97. }
  98.  
  99. //This uses the pythagorean theorem to find the hypotenuse or the distance between this, and the point given.
  100. public function distanceTo($point) {
  101. return sqrt(pow($this->x - $point->x, 2) + pow($this->y - $point->y, 2));
  102. }
  103. }
  104.  
  105. ?>
  106. <?php
  107.  
  108. require_once 'point.class.php';
  109.  
  110. class Angie {
  111. public static function Position($seconds) {
  112. return new Point(0.0, 1.0 * 3.0/*658*/ * $seconds);
  113. }
  114. }
  115.  
  116. ?>
Add Comment
Please, Sign In to add comment