Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. <?php
  2.  
  3. //klasa,która rysuje wykres funkcji liniowej
  4. /*
  5.  
  6. y = a*x+b
  7.  
  8. dane do podania - a,b,kolor (zgodnty z css)
  9.  
  10. stałe:
  11. x1
  12. x2
  13.  
  14. właściwości
  15. $wykresy - tablica dwuwymiarowa
  16.  
  17. array(
  18. array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
  19. array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
  20. array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
  21. array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
  22. array('a'=>0, 'b'=>0, 'kolor'=>'#f00')
  23. );
  24.  
  25. metody:
  26. rysuj
  27. translacjaX(liczba)
  28. translacjaY(liczba)
  29. obliczY(x)
  30. dodajFunkcje(a,b,kolor)
  31.  
  32.  
  33. */
  34.  
  35.  
  36. class FunkcjaLiniowa {
  37. public const X1 = -20;
  38. public const X2 = 20;
  39.  
  40. private $wykresy;
  41.  
  42. public function __construct() {
  43. $this->wykresy = array();
  44. }
  45.  
  46. public function dodajFunkcje($a,$b,$kolor = '#f00') {
  47. array_push($this->wykresy, array('a'=>$a,'b'=>$b,'kolor'=>$kolor));
  48. print_r($this->wykresy);
  49. }
  50.  
  51. private function obliczY($x,$a,$b) {
  52. return $a * $x + $b;
  53. }
  54.  
  55. private function translacjaX($pozycja) {
  56. return $pozycja*10 + 250;
  57. }
  58. private function translacjaY($pozycja) {
  59. return 250 -$pozycja*10;
  60. }
  61.  
  62. public function rysuj() {
  63. $x1 = $this->translacjaX(self::X1);
  64. $x2 = $this->translacjaX(self::X2);
  65.  
  66.  
  67. //echo "x1: ".self::X1." y1: $y1 x2: ".self::X2." y2: $y2<br>";
  68. echo '
  69. <style>
  70. .os {
  71. stroke:#000;
  72. stroke-width: 2;
  73. }
  74. .os.funkcja {
  75. stroke: #00f;
  76. }
  77. </style>
  78. <svg class="wykres" width="500" height="500">
  79. <line x1="250" y1="0" x2="250" y2="500" class="os os-Y"/>
  80. <line x1="0" y1="250" x2="500" y2="250" class="os os-X"/>';
  81.  
  82. foreach($this->wykresy as $wykres) {
  83. $y1 = $this->translacjaY($this->obliczY(self::X1,$wykres['a'],$wykres['b']));
  84. $y2 = $this->translacjaY($this->obliczY(self::X2,$wykres['a'],$wykres['b']));
  85.  
  86. echo '<line x1="'.$x1.'" y1="'.$y1.'" x2="'.$x2.'" y2="'.$y2.'" class="os funkcja" style="stroke:'.$wykres['kolor'].'"/>';
  87. }
  88.  
  89. echo ' </svg>
  90. ';
  91. }
  92. }
  93.  
  94. //uzycie klasy
  95. $wykres = new FunkcjaLiniowa();
  96.  
  97. $wykres->dodajFunkcje(0,2);
  98. $wykres->rysuj();
  99. $wykres->dodajFunkcje(1,1,'green');
  100. $wykres->rysuj();
  101. $wykres->dodajFunkcje(3,3,'blue');
  102. $wykres->rysuj();
  103.  
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement