block Pythagoras "Determine the hypotenuse or leg of a right triangle"
extends Interfaces.SI2SO;
parameter Boolean u1IsHypotenuse = false "= true, if u1 is the hypotenuse and y is one leg";
Interfaces.BooleanOutput valid "= true, if y is a valid result"
annotation (Placement(transformation(extent = {
{100, -70},
{120, -50}})));
protected
Real y2 "Square of y";
equation
if not u1IsHypotenuse then
y2 = u1 ^ 2 + u2 ^ 2;
y = sqrt(y2);
valid = true;
else
y2 = u1 ^ 2 - u2 ^ 2;
valid = 0 <= y2;
y = if noEvent(0 <= y2) then sqrt(y2) else 0;
end if;
annotation (
Icon(graphics = {
Polygon(
points = {
{34, -80},
{34, 80},
{-36, -40},
{34, -80}},
fillColor = {192, 192, 192},
fillPattern = FillPattern.Solid),
Line(
points = {
{-100, 60},
{22, 60}},
pattern = LinePattern.Dash),
Line(
points = {
{34, 0},
{100, 0}},
pattern = LinePattern.Dash),
Line(
points = {
{-100, -60},
{0, -60}},
pattern = LinePattern.Dash),
Line(
visible = u1IsHypotenuse,
points = {
{22, 60},
{34, 60}},
pattern = LinePattern.Dash),
Line(
visible = u1IsHypotenuse,
points = {
{-12, 0},
{34, 0}},
pattern = LinePattern.Dash)}),
Documentation(info = "<html>\n<p>This block determines the hypotenuse <code>y = sqrt(u1^2 + u2^2)</code>\nif the boolean parameter <code>u1IsHyotenuse = false</code>.\nIn this case the two inputs <code>u1</code> and\n<code>u2</code> are interpreted as the legs of a right triangle\nand the boolean output <code>valid</code> is always equal to\n<code>true</code>.</p>\n\n<p>If <code>u1IsHyotenuse = true</code>, input <code>u1</code> is interpreted as hypotenuse and <code>u2</code>\nis one of the two legs of a right triangle.\nThen, the other of the two legs of the right triangle is the output, determined by\n<code>y = sqrt(u1^2 - u2^2)</code>, if <code>u1^2 - u2^2 ≥ 0</code>; in this case the\nboolean output <code>valid</code> is equal to <code>true</code>. In case of <code>u1^2 - u2^2 < 0</code>, the\noutput <code>y = 0</code> and <code>valid</code> is set to <code>false</code>.</p>\n</html>"));
end Pythagoras;