Created
December 8, 2016 15:10
-
-
Save skorotkiewicz/946cfa186c7216a034500370926b973f to your computer and use it in GitHub Desktop.
Trigonometry.php andTrigMain.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once("Trigonometry.php"); | |
header("Content-type: image/png"); | |
function graphTrig() | |
{ | |
$trig = new Trigonometry(); | |
$setgraph = $trig->setGraph("cos"); | |
$width = $trig->setWidth(400); | |
$height = $trig->setHeight(200); | |
$setstart = $trig->setStartDegree(5); | |
$setend = $trig->setEndDegree(420); | |
$trig->drawGraph(); | |
} | |
echo graphTrig(); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Trigonometry | |
{ | |
// properties | |
public $graph; | |
public $height; | |
public $width; | |
public $startDegree; | |
public $endDegree; | |
public $errors = array(); | |
public function setGraph($graph) | |
{ | |
switch ($this->graph) | |
{ | |
case strtolower($this->graph == "sin"): | |
$this->graph = array(); | |
$this->graph = array_push($this->graph, "sin"); | |
break; | |
case strtolower($this->graph == "cos"): | |
$this->graph = array_push($this->graph, "cos"); | |
break; | |
case strtolower($this->graph == "tan"): | |
$this->graph = array_push($this->graph, "tan"); | |
break; | |
} | |
$this->graph = $graph; | |
} | |
public function setWidth($width) | |
{ | |
switch ($this->width) | |
{ | |
case (!is_numeric($this->width)): | |
$errors[0] = "Width should be any number."; | |
print_r($this->errors[0]); | |
break; | |
case $this->width >= 3000: | |
$errors[1] = "Width is too big."; | |
print_r($this->errors[1]); | |
break; | |
} | |
$this->width = $width; | |
} | |
public function setHeight($height) | |
{ | |
switch ($this->height) | |
{ | |
case (!is_numeric($this->height)): | |
$errors[2] = "Height should be any number."; | |
print_r($this->errors[2]); | |
break; | |
case $this->height >= 3000: | |
$errors[3] = "Height is too big."; | |
print_r($this->errors[3]); | |
break; | |
} | |
$this->height = $height; | |
} | |
public function setStartDegree($startDegree) | |
{ | |
switch ($this->startDegree) | |
{ | |
case (!is_numeric($this->startDegree)): | |
$errors[4] = "Degree should be any number."; | |
print_r($this->errors[4]); | |
break; | |
case (is_null($this->startDegree)): | |
$errors[5] = "Degree should be greater than 0."; | |
print_r($this->errors[5]); | |
break; | |
} | |
$this->startDegree = $startDegree; | |
} | |
public function setEndDegree($endDegree) | |
{ | |
switch ($this->endDegree) | |
{ | |
case (!is_numeric($this->endDegree)): | |
$errors[6] = "Degree should be any number."; | |
print_r($this->errors[6]); | |
break; | |
case (is_null($this->endDegree)): | |
$errors[7] = "Degree should be greater than 0."; | |
print_r($this->errors[7]); | |
break; | |
} | |
$this->endDegree = $endDegree; | |
} | |
public function getGraph() | |
{ | |
return $this->graph; | |
} | |
public function getWidth() | |
{ | |
return $this->width; | |
} | |
public function getHeight() | |
{ | |
return $this->height; | |
} | |
public function getEndDegree() | |
{ | |
return $this->endDegree; | |
} | |
public function getStartDegree() | |
{ | |
return $this->startDegree; | |
} | |
public function drawGraph() | |
{ | |
$boxheight = $this->getHeight(); | |
$boxwidth = $this->getWidth(); | |
$startdeg = $this->getStartDegree(); | |
$enddeg = $this->getEndDegree(); | |
$offset = $boxheight/2; | |
$area = imagecreatetruecolor($boxwidth, $boxheight); | |
$col_poly = imagecolorallocate($area, 255, 255, 255); | |
for ($c = $startdeg; $c < $enddeg; $c++) | |
{ | |
$this_x = $c; | |
$next_x = $this_x + 1; | |
if ($this->getGraph() == "sin") { | |
$this_y = sin(deg2rad($this_x)) * $offset + $offset; | |
$next_y = sin(deg2rad($next_x)) * $offset + $offset; | |
imageline($area, $this_x, $this_y, $next_x, $next_y, $col_poly); | |
} else if ($this->getGraph() == "cos") { | |
$this_y = cos(deg2rad($this_x)) * $offset + $offset; | |
$next_y = cos(deg2rad($next_x)) * $offset + $offset; | |
imageline($area, $this_x, $this_y, $next_x, $next_y, $col_poly); | |
} else if ($this->getGraph() == "tan") { | |
$this_y = tan(deg2rad($this_x)) * $offset + $offset; | |
$next_y = tan(deg2rad($next_x)) * $offset + $offset; | |
imageline($area, $this_x, $this_y, $next_x, $next_y, $col_poly); | |
} | |
} | |
imagepng($area); | |
imagedestroy($area); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment