Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Soczewka {
- /**
- * Tablica danych
- * x - odległość przedmiotu od zwierciadła
- * y - odległość obrazu od zwierciadła
- * f - ogniskowa
- * R - promień
- */
- private $data = [];
- /**
- * Sprawdź i zapisz podane dane
- */
- function __construct(array $data)
- {
- if (count($data) < 2)
- {
- trigger_error("Musisz podać minimum 2 parametry!", E_USER_ERROR);
- }
- if (! isset($data['x']) && ! isset($data['y']))
- {
- trigger_error("Musisz podać parametr x lub y", E_USER_ERROR);
- }
- if (! isset($data['R']) && ! isset($data['f']))
- {
- trigger_error("Musisz podać parametr R lub f", E_USER_ERROR);
- }
- $this->data = $data;
- }
- /**
- * Pobierz wszystkie dane
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * Oblicz wartość parametru podanego jako argument $parameter
- *
- * Dostępne parametry: f,R,x,y
- */
- public function calculate($parameter)
- {
- if (isset($this->data[$parameter]))
- return $this->data[$parameter];
- $metoda = 'calculate_'.$parameter;
- if (! method_exists($this, $metoda))
- return 'Nieprawidłowy parametr! Dostępne: f,R,x,y';
- $this->data[$parameter] = $this->{$metoda}();
- return $this->data[$parameter];
- }
- /**
- * Oblicz ogniskową soczewki
- *
- * f = R/2
- * f = ( (x * y) / (x + y) )
- */
- private function calculate_f()
- {
- if (isset($this->data['R']))
- return ($this->data['R'] / 2);
- if (isset($this->data['x']) && isset($this->data['y']))
- return ( ($this->data['x'] * $this->data['y']) / ($this->data['x'] + $this->data['y']) );
- return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: R lub x,y';
- }
- /**
- * Oblicz promień soczewki
- *
- * R = 2f
- */
- private function calculate_R()
- {
- if (isset($this->data['f']))
- return ($this->data['f'] * 2);
- if (isset($this->data['x']) && isset($this->data['y']))
- {
- $this->data['f'] = $this->calculate_f();
- return $this->calculate_R();
- }
- return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: f';
- }
- /**
- * Oblicz odległość przedmiotu od zwierciadła
- *
- * x = - ( (f * y) / (f - y) )
- */
- private function calculate_x()
- {
- if (isset($this->data['f']) && isset($this->data['y']))
- return (-( ($this->data['f'] * $this->data['y']) / ($this->data['f'] - $this->data['y']) ));
- if (isset($this->data['R']) && isset($this->data['y']))
- {
- $this->data['f'] = $this->calculate_f();
- return $this->calculate_x();
- }
- return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: f,y';
- }
- /**
- * Oblicz odległość obrazu od zwierciadła
- *
- * y = - ( (f * x) / (f - x) )
- */
- private function calculate_y()
- {
- if (isset($this->data['f']) && isset($this->data['x']))
- return (-( ($this->data['f'] * $this->data['x']) / ($this->data['f'] - $this->data['x']) ));
- if (isset($this->data['R']) && isset($this->data['x']))
- {
- $this->data['f'] = $this->calculate_f();
- return $this->calculate_y();
- }
- return 'BRAK ODPOWIEDNICH DANYCH! Wymagane: f,x';
- }
- }
- /**
- * x - odległość przedmiotu od zwierciadła
- * y - odległość obrazu od zwierciadła
- * f - ogniskowa
- * R - promień
- */
- $test1 = new Soczewka([
- 'y' => 2,
- 'R' => 3
- ]);
- var_dump($test1->getData());
- echo '<br>';
- $test1->calculate('x');
- var_dump($test1->getData());
- echo '<br>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement