Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //klasa,która rysuje wykres funkcji liniowej
- /*
- y = a*x+b
- dane do podania - a,b,kolor (zgodnty z css)
- stałe:
- x1
- x2
- właściwości
- $wykresy - tablica dwuwymiarowa
- array(
- array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
- array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
- array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
- array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
- array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
- );
- metody:
- rysuj
- translacjaX(liczba)
- translacjaY(liczba)
- obliczY(x)
- dodajFunkcje(a,b,kolor)
- */
- class FunkcjaLiniowa {
- public const X1 = -20;
- public const X2 = 20;
- private $wykresy;
- public function __construct() {
- $this->wykresy = array();
- }
- public function dodajFunkcje($a,$b,$kolor = '#f00') {
- array_push($this->wykresy, array('a'=>$a,'b'=>$b,'kolor'=>$kolor));
- print_r($this->wykresy);
- }
- private function obliczY($x,$a,$b) {
- return $a * $x + $b;
- }
- private function translacjaX($pozycja) {
- return $pozycja*10 + 250;
- }
- private function translacjaY($pozycja) {
- return 250 -$pozycja*10;
- }
- public function rysuj() {
- $x1 = $this->translacjaX(self::X1);
- $x2 = $this->translacjaX(self::X2);
- //echo "x1: ".self::X1." y1: $y1 x2: ".self::X2." y2: $y2<br>";
- echo '
- <style>
- .os {
- stroke:#000;
- stroke-width: 2;
- }
- .os.funkcja {
- stroke: #00f;
- }
- </style>
- <svg class="wykres" width="500" height="500">
- <line x1="250" y1="0" x2="250" y2="500" class="os os-Y"/>
- <line x1="0" y1="250" x2="500" y2="250" class="os os-X"/>';
- foreach($this->wykresy as $wykres) {
- $y1 = $this->translacjaY($this->obliczY(self::X1,$wykres['a'],$wykres['b']));
- $y2 = $this->translacjaY($this->obliczY(self::X2,$wykres['a'],$wykres['b']));
- echo '<line x1="'.$x1.'" y1="'.$y1.'" x2="'.$x2.'" y2="'.$y2.'" class="os funkcja" style="stroke:'.$wykres['kolor'].'"/>';
- }
- echo ' </svg>
- ';
- }
- }
- //uzycie klasy
- $wykres = new FunkcjaLiniowa();
- $wykres->dodajFunkcje(0,2);
- $wykres->rysuj();
- $wykres->dodajFunkcje(1,1,'green');
- $wykres->rysuj();
- $wykres->dodajFunkcje(3,3,'blue');
- $wykres->rysuj();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement