1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
---
title: Math.atan2()
slug: Web/JavaScript/Referencje/Obiekty/Math/atan2
tags:
- JavaScript
- Math
- Method
translation_of: Web/JavaScript/Reference/Global_Objects/Math/atan2
---
<p>{{JSRef}}</p>
<h2 id="Podsumowanie" name="Podsumowanie">Podsumowanie</h2>
<p>Zwraca kąta (w radianach) utworzony przez oś OX i prostą przechodzącą przez punkt o podanych współrzędnych.</p>
<h2 id="Sk.C5.82adnia" name="Sk.C5.82adnia">Składnia</h2>
<pre class="syntaxbox"><code>Math.atan2(<var>y</var>, <var>x</var>)</code></pre>
<h3 id="Parametry" name="Parametry">Parametry</h3>
<dl>
<dt><code>y, x</code> </dt>
<dd>liczby.</dd>
</dl>
<h2 id="Opis" name="Opis">Opis</h2>
<p>Metoda <code>atan2</code> zwraca wartość liczbową pomiędzy -π a π, reprezentującą kąt theta punktu <code>(x, y)</code>. Kąt ten, mierzony w radianach, został utworzony przez dodatnią oś OX i punkt <code>(x,y)</code>, z obrotem w kierunku przeciwnym do ruchu wskazówek zegara. Należy zwrócić uwagę na to, że pierwszym argumentem tej funkcji jest współrzędna y, a x jest argumentem drugim.</p>
<p>Do metody Math.<code>atan2</code>() przekazywane są osobno argumenty <code>x</code> i <code>y</code>, podczas gdy do metody <code>atan</code> przekazywany jest stosunek tych dwóch wartości.</p>
<p>Ponieważ <code>atan2()</code> jest statyczną metodą obiektu {{jsxref("Math")}}, zawsze odwołujemy się do niej poprzez <code>Math.atan2()</code>, a nie jak do metody instancji obiektu Math utworzonej przez użytkownika.</p>
<h2 id="Przyk.C5.82ady" name="Przyk.C5.82ady">Przykłady</h2>
<h3 id="Przyk.C5.82ad:_Zastosowanie_Math.atan2" name="Przyk.C5.82ad:_Zastosowanie_Math.atan2">Przykład: Zastosowanie <code>Math.atan2()</code></h3>
<pre class="brush: js language-js"><code class="language-js">Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span><span class="token number">90</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"> // 1.4056476493802699
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span><span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">90</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"> // 0.16514867741462683
</span>
Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±PI.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">+</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±PI for x > 0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">0</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±0 for x > 0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span><span class="token operator">-</span>y<span class="token punctuation">,</span> ±<span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // -PI/2 for y > 0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>y<span class="token punctuation">,</span> ±<span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // PI/2 for y > 0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±y<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">Infinity</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±PI for finite y > 0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±y<span class="token punctuation">,</span> <span class="token operator">+</span><span class="token number">Infinity</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±0 for finite y > 0.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">Infinity</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"> // ±PI/2 for finite x.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">Infinity</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">Infinity</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"> // ±3*PI/4.
</span>Math<span class="token punctuation">.</span><span class="token function">atan2<span class="token punctuation">(</span></span>±<span class="token number">Infinity</span><span class="token punctuation">,</span> <span class="token operator">+</span><span class="token number">Infinity</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"> // ±PI/4.</span></code></pre>
<h2 id="Zobacz_tak.C5.BCe" name="Zobacz_tak.C5.BCe">Zobacz także</h2>
<ul>
<li>{{jsxref("Math.acos()")}}</li>
<li>{{jsxref("Math.asin()")}}</li>
<li>{{jsxref("Math.atan()")}}</li>
<li>{{jsxref("Math.cos()")}}</li>
<li>{{jsxref("Math.sin()")}}</li>
<li>{{jsxref("Math.tan()")}}</li>
</ul>
|