hypot()
[Math]
double hypot(double x, double y);
Description
Returns $\sqrt{x^2 + y^2}$ without undue overflow or underflow.
This is the length of the hypotenuse of a right-angled triangle with sides of length x and y, or the distance of the point (x, y) from the origin..
Parameters
x
: The value of the first side of the triangle.
y
: The value of the second side of the triangle.
Returns
The length of the hypotenuse of a right-angled triangle with sides of length x and y.
Example Code
The code reads the value of two potentiometers connected to pins
INX
and INY
and place the length of the
hypotenuse of a right-angled triangle with sides of length x
and y on pin OUT
.
void setup() {
(0, "INY"); // set the label of pin 0 to "INY"
pinLabel(1, "INX"); // set the label of pin 1 to "INX"
pinLabel(2, "OUT"); // set the label of pin 2 to "OUT"
pinLabel(2, OUTPUT); // set pin 2 as output
pinMode(3, "OUT2"); // set the label of pin 3 to "OUT2"
pinLabel(3, OUTPUT); // set pin 3 as output
pinMode
(4, "VCC"); // set the label of pin 4 to "VCC"
pinLabel(4); // set pin 4 as power pin
powerPin(5, "GND"); // set the label of pin 5 to "GND"
pinLabel(5); // set pin 5 as ground pin
groundPin}
void loop() {
// read the value of the potentiometers
int val_x = analogRead(0);
int val_y = analogRead(1);
// map the value of the potentiometers from 0 to 1023
// to the range of 0 to 10
double x = map(val_x, 0, 1023, 0, 10);
double y = map(val_y, 0, 1023, 0, 10);
// calculate the length of the hypotenuse of a right-angled triangle
// with sides of length x and y
double z = hypot(x, y);
// map the value of z from 0 to 14.14 to 0 to 1023
int pwm = (int) map(z, 0, 14.14, 0, 1023);
// write the value of pwm to pin 2
(2, pwm);
analogWrite}
Notes and Warnings
This function permits a zero pointer as a directive to skip storing the exponent.