Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'angie.class.php'; //we need to use Angie in this class.
- require_once 'point.class.php'; //we need to use the point structure
- require_once 'triangle.class.php'; //we need to use these methods
- class Buddy {
- protected $speed, $x, $y, $points; //these are the variables in the class
- public function __construct($speed, $displace) {
- $this->speed = $speed; //this is his speed
- $this->x = $displace; //pass in the orignal x-position
- $this->y = 0; //he starts at 0
- $interval = 1;
- $d = 0;
- $this->points = array(); //make the points variable into an array
- $this->points[] = array("buddy" => new Point($this->x, $this->y), "angle" => 0.0, "arctan" => atan(0) . " = arctan(0 / 50)"); //add the initial point
- for ($i = 1; $i < 300; $i++) { //this will make the array 30 items long.
- $angle = $this->Angle(Angie::Position($i - 1), $this->points[$i - 1]["buddy"]);
- //get the angle of Angie's position 2 seconds ago and Buddy's position 2 seconds ago
- 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)
- $point = new Point(0.000000000000001, $this->points[$i - 1]["buddy"]->Y() + ($speed * $interval * sin($angle))); //then... set his x position to 0.000000000000001
- }
- else //else or otherwise... do the normal formula
- $point = new Point(
- ($this->points[$i - 1]["buddy"]->X() > 0 ? //if the previous point was greater than zero...
- $this->points[$i - 1]["buddy"]->X() - ($speed * $interval * cos($angle)) //then... take the old point minus the new hypotenuse length
- : $this->points[$i - 1]["buddy"]->X() + ($speed * $interval * cos($angle))), //else... take the old point plus the new hypotenuse length
- $this->points[$i - 1]["buddy"]->Y() + ($speed * $interval * sin($angle)) //the Y-coordinate
- );
- $this->points[] = array(
- "buddy" => $point, //this is the only important element in this associative array. the others are for debugging purposes.
- "angie" => Angie::Position($i),
- "rads " => $angle,
- "atan " => atan($angle) . " = " .
- "arctan((" . Angie::Position($i - 1)->Y() . " - " . $this->points[$i - 1]["buddy"]->Y() . ") / " . $this->points[$i - 1]["buddy"]->X() . ")",
- "cosine" => (($this->points[$i - 1]["buddy"]->X() < 0 ? "-" : "") . $speed * $interval * cos($angle)) . " = 2 * 7 * cos({$angle})",
- "sine " => ($speed * $interval * sin($angle)) . " = 2 * 7 * sin({$angle})"
- );
- 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).
- break;
- }
- $d += $this->points[$i]["buddy"]->distanceTo($this->points[$i - 1]["buddy"]);
- }
- //print_r($this->points); //this line is for debugging.
- echo "\nBuddy caught up with her in {$i} seconds\n"; //this tells the user after how many seconds they will meet up again.
- echo $this->csv($this->points);
- echo "\n" . Angie::Position($i)->Y();
- echo "\n$d\n";
- }
- public function csv($arr) {
- $s="Time,Buddy X, Buddy Y, Angie X, Angie Y\n";
- for($x=0;$x<sizeof($arr);$x++)
- $s.=$x.",".$arr[$x]["buddy"]->X().",".$arr[$x]["buddy"]->Y().",0," . Angie::Position($x)->Y() . "\n";
- return $s;
- }
- public function getPoint($second) {
- return $this->points[$second];
- }
- public function Angle($point1, $point2) {
- return atan(
- ($point1->Y() - $point2->Y()) //take the difference between the two
- / $point2->X() //divide by the X-coordinate
- ); //return the arc tangent of the quotient
- }
- }
- ?>
- <?php
- //The class or structure of a point as defined by me, Kyle Stevenson
- class Point {
- protected $x, $y;
- public function __construct($x, $y) {
- $this->x = $x;
- $this->y = $y;
- }
- //The X coordinate in the current coordinate-point.
- public function X() {
- return $this->x;
- }
- //The Y coordinate in the current coordinate-point.
- public function Y() {
- return $this->y;
- }
- //This uses the pythagorean theorem to find the hypotenuse or the distance between this, and the point given.
- public function distanceTo($point) {
- return sqrt(pow($this->x - $point->x, 2) + pow($this->y - $point->y, 2));
- }
- }
- ?>
- <?php
- require_once 'point.class.php';
- class Angie {
- public static function Position($seconds) {
- return new Point(0.0, 1.0 * 3.0/*658*/ * $seconds);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment