PHP задача

dikei2000

Registered
Здравейте има ли някой който може да реши следната задача,
ще бъда много благодарен

благодаря предварително


<?php
/**
* INSTRUCTIONS
* Your task is to code a working model of a simplified version of our solar system.
* Throughout this file and the corresponding html file you'll find several sections
* of code marked TODO, fill in these sections of code to power the visualisation on
* the html page. The html page utilises the HTML5 canvas element. If you complete
* the task you should be able to generate an animated version of the solar system
* showing the orbits over time.
*
* You should ONLY need to edit sections marked 'TODO'
*/


/**
* TODO add classes for Star, Planet and Moon that can be used by the calling code
* at the bottom of this file. Choose whichever object representation you feel
* is appropriate but be aware the calling code requires several specifically named
* methods, you should write these methods rather than editing the calling code.
*/
class Star
{}

class Planet
{}

class Moon
{}

class PolarCoordinate
{
public $radius = null;
public $azimuth = null;

public function __construct($radius, $azimuth)
{
$this->radius = $radius;
$this->azimuth = $azimuth;
}

/**
* Transform this coordinate into a cartesian form.
*/
public function asCartesian()
{
// TODO Calculate the x and y position as a cartesian
$x = 0; // REPLACE 0 with your calcualtion
$y = 0; // REPLACE 0 with your calcualtion

return new CartesianCoordinate($x, $y);
}
}

class CartesianCoordinate
{
public $x = null;
public $y = null;

public function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}

public function addOffset(CartesianCoordinate $offset)
{
$this->x += $offset->x;
$this->y += $offset->y;

return $this;
}
}


abstract class Renderer {
public abstract function draw($orbital_body, $day);

public function drawIncludingSatellites($orbital_body, $day) {
// TODO - draw the positions of children of the object using the draw function of the sub class
return "";
}
}

class CartesianListRenderer extends Renderer {
public function draw($orbital_body, $day) {
$coord = $orbital_body->cartesianPositionRelativeToSun($day);
echo "<p>{$orbital_body->name} - x: {$coord->x}, y: {$coord->y} </p>\r\n";
}
}

class JsonRenderer extends Renderer {
public function draw($orbital_body, $day)
{
$coord = $orbital_body->cartesianPositionRelativeToSun($day);

$parentX = 0;
$parentY = 0;
if ($orbital_body->orbits)
{
$parent_coord = $orbital_body->orbits->cartesianPositionRelativeToSun($day);
$parentX = $parent_coord->x;
$parentY = $parent_coord->y;
}

return "{name:'{$orbital_body->name}',x:{$coord->x},y:{$coord->y},radius:{$orbital_body->radius}, orbit_radius:{$orbital_body->orbital_radius}, orbit_centre_x:{$parentX}, orbit_centre_y:{$parentY}}";
}
}

class SolarPositionList {
public static function draw(OrbitalBody $sun, $day, Renderer $renderer)
{
return $renderer->drawIncludingSatellites($sun, $day);
}
}

/**
* The following objects are the only ones you're expected to use,
* ignore other elements of the solar system
*/

/**
* __construct
* @param string $name The name of the body.
* @param object $orbits The body this body orbits.
* @param float $orbital_radius The distance at which this body orbits it's parent, measured in AU multiples of Earths orbital radius from the sun.
* @param float $orbital_period Number of earth days required to complete an orbit
* @param float $initial_radians The number of radians through it's orbit the body is on day 0.
* @param float $mass The mass of the body in multiples of the weight of Earth.
* @param float $radius The radius of the body in multiples of earths radius
*/
$sun = new Star("Sol", null, 0, 0, 0, 332900, 55);
$mercury = new Planet("Mercury", $sun, 0.39, 87.9, 0, 0.055, 0.383);
$venus = new Planet("Venus", $sun, 0.72, 224, 3.14, 0.815, 0.949);
$earth = new Planet("Earth", $sun, 1, 365, 0, 1, 1);
$moon = new Moon("The Moon", $earth, 0.15, 28, 0, 0.0123, 0.0273); // Note the moons orbital_radius is heavily exaggerated.
$mars = new Planet("Mars", $sun, 1.52, 687, 0, 0.107, 0.533);
$jupiter = new Planet("Jupiter", $sun, 5.2, 4332, 0, 318, 11.209);

$sun->addSatellite($mercury);
$sun->addSatellite($venus);
$sun->addSatellite($earth);
$earth->addSatellite($moon);
$sun->addSatellite($mars);
$sun->addSatellite($jupiter);

$cartesianListRenderer = new CartesianListRenderer();
$jsonRenderer = new JsonRenderer();

$days = isset($_GET['days']) ? $_GET['days'] : 0;

//SolarPositionList::draw($sun, $days, $cartesianListRenderer);
echo "[".SolarPositionList::draw($sun, $days, $jsonRenderer)."]";

?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Techlightenment Technical Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
/**
* SolarSystem
* Our solar system class containing several setup parameters
*/
function SolarSystem(canvasID) {
this.centre_x = 400;
this.centre_y = 400;
this.scale = 70;
this.body_colour = "Black";
this.orbit_colour = "Green";
this.label_colour = "Blue";
this.canvas = document.getElementById(canvasID);

this.context;

// Check browser supports canvas
if (this.canvas && this.canvas.getContext) {
this.context = this.canvas.getContext('2d');
}
}
/**
* plotOrbiter
* plot the orbit and position of a single orbiter
*/
SolarSystem.prototype.plotOrbiter = function(body) {
var x_pos, y_pos, orbit_centre_x, orbit_centre_y, orbit_radius, body_radius;

// Scale positions to better fit our canvas.
x_pos = this.centre_x + (body.x * this.scale);
y_pos = this.centre_y + (body.y * this.scale);

orbit_centre_x = body.orbit_centre_x * this.scale;
orbit_centre_y = body.orbit_centre_y * this.scale;
orbit_radius = body.orbit_radius * this.scale;

// Scale the body up to a more reasonable (but incorrect) size for viewing
body_radius = Math.log(body.radius * this.scale);

if (this.context) {
this.drawOrbit(orbit_radius, orbit_centre_x, orbit_centre_y);
this.drawBody(body.name, body_radius, x_pos, y_pos);
this.drawLabel(body.name, x_pos + body_radius + 2, y_pos + (body_radius / 2));
}
};
/**
* drawOrbit
* plot an orbital path
*/
SolarSystem.prototype.drawOrbit = function(orbit_radius, orbit_centre_x, orbit_centre_y) {
if (orbit_radius > 0) {
var context = this.context;
context.fillStyle = "rgba(0,0,0,0)";
context.strokeStyle = this.orbit_colour;
context.beginPath();

// TODO - Specify the orbit position

context.closePath();
context.fill();
context.stroke();
}
};
/**
* drawBody
* plot the position and name of asingle body
*/
SolarSystem.prototype.drawBody = function(name, body_radius, x_pos, y_pos) {
var context = this.context;
context.fillStyle = this.body_colour;
context.beginPath();

// TODO - Specify the body position and size

context.closePath();
context.fill();
};
/**
* drawLabel
* draw a label for a body
*/
SolarSystem.prototype.drawLabel = function(name, x_pos, y_pos) {
var context = this.context;
context.fillStyle = this.label_colour;
context.font = '10px Times New Roman';
context.fillText(name, x_pos, y_pos);
};
/**
* clearCanvas
* clear the canvas
*/
SolarSystem.prototype.clearCanvas = function() {
this.canvas.width = this.canvas.width;
};
/**
* draw
* Given a set of orbital bodies, draw the orbital position of each
*/
SolarSystem.prototype.draw = function(bodies) {
sol.clearCanvas();
bodies.forEach(function(body) {
sol.plotOrbiter(body);
});
};
/**
* animate
* Animate the position of the orbital bodies over time.
* @param SolarSystem solar_system a single solar system instance
* @param int days number of days into the cycle to begin
* @param int increment number of days to add between 'frames'
*/
SolarSystem.animate = function(solar_system, days) {
// TODO - animate the system by repeatedly requesting new positions ofr the bodies from the server.
};
SolarSystem.active = true;
SolarSystem.lastDay = 0;
SolarSystem.increment = 1;

$(document).ready(function () {
sol = new SolarSystem('myCanvas');
SolarSystem.animate(sol, 0);
//sol.draw(test_bodies);
});

/**
* This test data represents the initial positions of the bodies.
*/
var test_bodies = [
{name:'Sol',x:0,y:0,radius:55, orbit_radius:0, orbit_centre_x:0, orbit_centre_y:0},
{name:'Mercury',x:0.39,y:0,radius:0.383, orbit_radius:0.39, orbit_centre_x:0, orbit_centre_y:0},
{name:'Venus',x:-0.71999908684383,y:0.0011467100998705,radius:0.949, orbit_radius:0.72, orbit_centre_x:0, orbit_centre_y:0},
{name:'Earth',x:1,y:0,radius:1, orbit_radius:1, orbit_centre_x:0, orbit_centre_y:0},
{name:'The Moon',x:1.15,y:0,radius:0.0273, orbit_radius:0.15, orbit_centre_x:1, orbit_centre_y:0},
{name:'Mars',x:1.52,y:0,radius:0.533, orbit_radius:1.52, orbit_centre_x:0, orbit_centre_y:0},
{name:'Jupiter',x:5.2,y:0,radius:11.209, orbit_radius:5.2, orbit_centre_x:0, orbit_centre_y:0}
];

</script>
</head>
<body>
<canvas id="myCanvas" width="800" height="800" style="border: 1px solid black;">
Sorry your browser does not support Canvas.
</canvas>
<form>
<input type="button" value="Stop" onclick="SolarSystem.active = false;" />
<input type="button" value="Start" onclick="SolarSystem.active = true; SolarSystem.animate(sol, SolarSystem.lastDay, SolarSystem.increment);" />
</form>
</body>
</html>
 

Back
Горе