1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.42">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; min-height: 16.0px}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #a71e12}
16 span
.s1
{color: #0019b7}
17 span
.s2
{color: #000000}
18 span
.s3
{color: #a71e12}
19 span
.Apple-tab-span
{white-space:pre
}
23 <p class=
"p1"><b>hypot
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>hypotenuse
</b></p>
24 <p class=
"p2"><br></p>
25 <p class=
"p3"><b>BinaryOperator
</b></p>
26 <p class=
"p2"><br></p>
27 <p class=
"p3"><b>hypot(x, y)
</b></p>
28 <p class=
"p3"><b>x hypot: y
</b></p>
29 <p class=
"p3"><b>x.hypot(y)
</b></p>
30 <p class=
"p2"><br></p>
31 <p class=
"p3">Returns the square root of the sum of the squares of a and b. Or equivalently, the distance from the origin
</p>
32 <p class=
"p3">to the point (x, y).
<span class=
"Apple-converted-space"> </span></p>
33 <p class=
"p3">See also
<b>atan2
</b>.
</p>
34 <p class=
"p4"><br></p>
35 <p class=
"p3">In this example, hypot is used to calculate a doppler shift pitch and amplitude based on distance.
</p>
36 <p class=
"p4"><br></p>
39 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">var
</span> x, y, distance, velocity, pitchRatio, amplitude;
</p>
40 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// object travels
200 meters in
6 secs (=
120kph) passing
10 meters
<span class=
"Apple-converted-space"> </span></p>
41 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// from the listener
</p>
42 <p class=
"p5"><span class=
"Apple-tab-span"> </span>x =
10;
</p>
43 <p class=
"p5"><span class=
"Apple-tab-span"> </span>y =
<span class=
"s1">LFSaw
</span>.kr(
1/
6,
0,
100);
</p>
44 <p class=
"p5"><span class=
"Apple-tab-span"> </span>distance = hypot(x, y);
</p>
45 <p class=
"p5"><span class=
"Apple-tab-span"> </span>velocity =
<span class=
"s1">Slope
</span>.kr(distance);
</p>
46 <p class=
"p5"><span class=
"Apple-tab-span"> </span>pitchRatio = (
344 - velocity) /
344;
<span class=
"Apple-converted-space"> </span><span class=
"s3">// speed of sound is
344 meters/sec
</span></p>
47 <p class=
"p5"><span class=
"Apple-tab-span"> </span>amplitude =
10 / distance.squared;
</p>
48 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">FSinOsc
</span>.ar(
1000 * pitchRatio,
0, amplitude)
</p>
49 <p class=
"p5">}.play)
</p>
50 <p class=
"p4"><br></p>
51 <p class=
"p3">The next example uses the distance to modulate a delay line.
</p>
52 <p class=
"p2"><br></p>
55 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">var
</span> x, y, distance, velocity, pitchRatio, amplitude, motorSound;
</p>
56 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// object travels
200 meters in
6 secs (=
120kph) passing
10 meters
<span class=
"Apple-converted-space"> </span></p>
57 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// from the listener
</p>
58 <p class=
"p5"><span class=
"Apple-tab-span"> </span>x =
10;
</p>
59 <p class=
"p5"><span class=
"Apple-tab-span"> </span>y =
<span class=
"s1">LFSaw
</span>.kr(
1/
6,
0,
100);
</p>
60 <p class=
"p5"><span class=
"Apple-tab-span"> </span>distance = hypot(x, y);
</p>
61 <p class=
"p5"><span class=
"Apple-tab-span"> </span>amplitude =
40 / distance.squared;
</p>
62 <p class=
"p5"><span class=
"Apple-tab-span"> </span>motorSound =
<span class=
"s1">RLPF
</span>.ar(
<span class=
"s1">FSinOsc
</span>.ar(
200,
0,
<span class=
"s1">LFPulse
</span>.ar(
31.3,
0,
0.4)),
400,
0.3);
</p>
63 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s1">DelayL
</span>.ar(motorSound,
110/
344, distance/
344, amplitude)
</p>
64 <p class=
"p5">}.play)
</p>