Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_GET["tri"])) {
- $checked = "checked";
- }
- ?>
- <form action="rand.php" method="get" style="font-family:arial">
- <span id="table">
- <table border="0" cellpadding="2">
- <th>Point 1</th><th>Point 2</th><tr />
- <td>x co-ord <input type="text" name="x1" value="<?php echo $_GET["x1"]; ?>" /></td><td>x co-ord <input type="text" name="x2" value="<?php echo $_GET["x2"]; ?>" /></td><tr />
- <td>y co-ord <input type="text" name="y1" value="<?php echo $_GET["y1"]; ?>" /></td><td>y co-ord <input type="text" name="y2" value="<?php echo $_GET["y2"]; ?>" /></td><tr />
- <td><input type="submit" value="Generate Line" /> <input type="button" onClick="parent.location='?rand'" value="Random Line" /></td><td>
- </table>
- </span>
- </form>
- <?php
- create_image();
- print "<img src=image.png?".date("U").">";
- function create_image() {
- $im = @imagecreate(1160, 1000) or die("Cannot Initialize new GD image stream");
- $background_color = imagecolorallocate($im, 255, 255, 255); // yellow
- $red = imagecolorallocate($im, 255, 0, 0); // red
- $blue = imagecolorallocate($im, 0, 0, 255); // blue
- $gray = imagecolorallocate($im, 190, 190, 190);
- imageline ($im, 480, 0, 480, 960, $red);
- imageline ($im, 0, 480, 960, 480, $red);
- $grady = 953;
- while ($i < 97) {
- imagestring($im, 2, 475, $grady, "-", $red);
- imagestring($im, 2, $gradx-2, 477, "|", $red);
- $grady = $grady - 10;
- $gradx = $gradx + 10;
- $i++;
- }
- if (isset($_GET["rand"])) {
- header("location:?x1=" . mt_rand(-48,48) . "&y1=" . mt_rand(-48,48) . "&x2=" . mt_rand(-48,48) . "&y2=" . mt_rand(-48,48));
- }
- $x1 = 480 + $_GET["x1"]*10;
- $y1 = 480 - ($_GET["y1"])*10;
- $x2 = 480 + $_GET["x2"]*10;
- $y2 = 480 - ($_GET["y2"])*10;
- $m = ($_GET["y2"] - $_GET["y1"]) / ($_GET["x2"] - $_GET["x1"]);
- $midx = ($_GET[ "x1"] + $_GET["x2"]) / 2;
- $midy = ($_GET[ "y1"] + $_GET["y2"]) / 2;
- $yinter = $_GET["y1"] - ($m*$_GET["x1"]);
- $yinter = $_GET["y1"] - ($m*$_GET["x1"]);
- # Define points
- imageline ($im, $x1, $y1, $x2, $y2, $blue); # User line
- imagestring($im, 2, $x1, $y1, "(" . round($_GET["x1"], 2) . ", " . round($_GET["y1"], 2) . ")", $red);
- imagestring($im, 2, $x2, $y2, "(" . round($_GET["x2"], 2) . ", " . round($_GET["y2"], 2) . ")", $red);
- imagefilledellipse($im, 480+($midx)*10, 480-($midy)*10, 5, 5, $red);
- imagefilledellipse($im, round($x1, 2), round($y1, 2), 5, 5, $red);
- imagefilledellipse($im, round($x2, 2), round($y2, 2), 5, 5, $red);
- imagestring($im, 2, 480+($midx)*10, 480-($midy)*10, "(" . round($midx, 2) . ", " . round($midy, 2) . ")", $red);
- /* Remove comment markers to plot y-intercept
- imagefilledellipse($im, 480, 480-($yinter)*10, 5, 5, $red);
- imagestring($im, 2, 490, 490-($yinter)*10, "(0, " . round($yinter, 2) . ")", $red);
- imageline($im, 480, 480-($yinter)*10, 490, 490-($yinter)*10, $red);
- */
- # Create triangle
- if (isset($_GET["tri"])) {
- imageline ($im, $x1*-1, $y1, $x2, $y2, $blue);
- imageline ($im, $x1, $y1, $x2*-1, $y2, $blue);
- }
- # Show info
- imagestring($im, 2, 1000, 20, "Gradient: " . round($m, 2), $red);
- imagestring($im, 2, 1000, 40, "Y-intercept: " . round($yinter, 2), $red);
- imagestring($im, 2, 1000, 60, "Mid-point: (" . round($midx, 2) . ", " . round($midy, 1) . ")", $red);
- imagestring($im, 2, 1000, 80, "Equation: y = " . round($m, 2) . "x + " . $yinter, $red);
- imagestring($im, 2, 1000, 100, "Written by Jake Browning", $gray);
- imagepng($im,"image.png");
- imagedestroy($im);
- }
- ?>
Add Comment
Please, Sign In to add comment