Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2.  
  3. class Soczewka {
  4.    
  5.     /**
  6.      * Tablica danych
  7.      * x - odległość przedmiotu od zwierciadła
  8.      * y - odległość obrazu od zwierciadła
  9.      * f - ogniskowa
  10.      * R - promień
  11.      */
  12.     private $data = [];
  13.    
  14.     /**
  15.      * Sprawdź i zapisz podane dane
  16.      */
  17.     function __construct(array $data)
  18.     {
  19.         if (count($data) < 2)
  20.         {
  21.             trigger_error("Musisz podać minimum 2 parametry!", E_USER_ERROR);
  22.         }
  23.        
  24.         if (! isset($data['x']) && ! isset($data['y']))
  25.         {
  26.             trigger_error("Musisz podać parametr x lub y", E_USER_ERROR);
  27.         }
  28.        
  29.         if (! isset($data['R']) && ! isset($data['f']))
  30.         {
  31.             trigger_error("Musisz podać parametr R lub f", E_USER_ERROR);
  32.         }
  33.        
  34.         $this->data = $data;
  35.     }
  36.    
  37.     /**
  38.      * Pobierz wszystkie dane
  39.      */
  40.     public function getData()
  41.     {
  42.         return $this->data;
  43.     }
  44.    
  45.     /**
  46.      * Oblicz wartość parametru podanego jako argument $parameter
  47.      *
  48.      * Dostępne parametry: f,R,x,y
  49.      */
  50.     public function calculate($parameter)
  51.     {
  52.         if (isset($this->data[$parameter]))
  53.             return $this->data[$parameter];
  54.        
  55.         $metoda = 'calculate_'.$parameter;
  56.        
  57.         if (! method_exists($this, $metoda))
  58.             return 'Nieprawidłowy parametr! Dostępne: f,R,x,y';
  59.        
  60.         $this->data[$parameter] = $this->{$metoda}();
  61.        
  62.         return $this->data[$parameter];
  63.     }
  64.    
  65.     /**
  66.      * Oblicz ogniskową soczewki
  67.      *
  68.      * f = R/2
  69.      * f = ( (x * y) / (x + y) )
  70.      */
  71.     private function calculate_f()
  72.     {
  73.         if (isset($this->data['R']))
  74.             return ($this->data['R'] / 2);
  75.        
  76.         if (isset($this->data['x']) && isset($this->data['y']))
  77.             return ( ($this->data['x'] * $this->data['y']) / ($this->data['x'] + $this->data['y']) );
  78.        
  79.         return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: R lub x,y';
  80.     }
  81.    
  82.     /**
  83.      * Oblicz promień soczewki
  84.      *
  85.      * R = 2f
  86.      */
  87.     private function calculate_R()
  88.     {
  89.         if (isset($this->data['f']))
  90.             return ($this->data['f'] * 2);
  91.        
  92.         if (isset($this->data['x']) && isset($this->data['y']))
  93.         {
  94.             $this->data['f'] = $this->calculate_f();
  95.             return $this->calculate_R();
  96.         }
  97.        
  98.         return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: f';
  99.     }
  100.    
  101.     /**
  102.      * Oblicz odległość przedmiotu od zwierciadła
  103.      *
  104.      * x = - ( (f * y) / (f - y) )
  105.      */
  106.     private function calculate_x()
  107.     {
  108.         if (isset($this->data['f']) && isset($this->data['y']))
  109.             return (-( ($this->data['f'] * $this->data['y']) / ($this->data['f'] - $this->data['y']) ));
  110.        
  111.         if (isset($this->data['R']) && isset($this->data['y']))
  112.         {
  113.             $this->data['f'] = $this->calculate_f();
  114.             return $this->calculate_x();
  115.         }
  116.        
  117.         return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: f,y';
  118.     }
  119.    
  120.     /**
  121.      * Oblicz odległość obrazu od zwierciadła
  122.      *
  123.      * y = - ( (f * x) / (f - x) )
  124.      */
  125.     private function calculate_y()
  126.     {
  127.         if (isset($this->data['f']) && isset($this->data['x']))
  128.             return (-( ($this->data['f'] * $this->data['x']) / ($this->data['f'] - $this->data['x']) ));
  129.        
  130.         if (isset($this->data['R']) && isset($this->data['x']))
  131.         {
  132.             $this->data['f'] = $this->calculate_f();
  133.             return $this->calculate_y();
  134.         }
  135.        
  136.         return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: f,x';
  137.     }
  138.    
  139. }
  140.  
  141. /**
  142.  * x - odległość przedmiotu od zwierciadła
  143.  * y - odległość obrazu od zwierciadła
  144.  * f - ogniskowa
  145.  * R - promień
  146.  */
  147.  
  148. $test1 = new Soczewka([
  149.     'y' => 2,
  150.     'R' => 3
  151.     ]);
  152.    
  153. var_dump($test1->getData());
  154. echo '<br>';
  155.  
  156. $test1->calculate('x');
  157.    
  158. var_dump($test1->getData());
  159. echo '<br>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement