Given a rhombus with diagonals a and b, which contains an inscribed circle. The task is to find the area of that circle in terms of a and b.
Examples:
Input: l = 5, b = 6
Output: 11.582
Input: l = 8, b = 10
Output: 30.6341
Approach: From the figure, we see, the radius of inscribed circle is also a height h=OH of the right triangle AOB. To find it, we use equations for triangle’s area :
Area AOB = 1/2 * (a/2) * (b/2) = ab/8 = 12ch
where c = AB i.e. a hypotenuse. So,
r = h = ab/4c = ab/4?(a^2/4 + b^2/4) = ab/2?(a^2+b^2)
and therefore area of the circle is
A = ? * r^2 = ? a^2 b^2 /4(a2 + b2)
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
float
circlearea(
float
a,
float
b)
{
if
(a < 0 || b < 0)
return
-1;
float
A = (3.14 *
pow
(a, 2) *
pow
(b, 2))
/ (4 * (
pow
(a, 2) +
pow
(b, 2)));
return
A;
}
int
main()
{
float
a = 8, b = 10;
cout << circlearea(a, b) << endl;
return
0;
}
|
Java
public
class
GFG {
public
static
float
circlearea(
double
a,
double
b)
{
if
(a <
0
|| b <
0
)
return
-
1
;
float
A = (
float
) ((
3.14
* Math.pow(a,
2
) * Math.pow(b,
2
))
/ (
4
* (Math.pow(a,
2
) + Math.pow(b,
2
)))) ;
return
A ;
}
public
static
void
main(String[] args) {
float
a =
8
, b =
10
;
System.out.println(circlearea(a, b));
}
}
|
Python 3
def
circlearea(a, b):
if
(a <
0
or
b <
0
):
return
-
1
A
=
((
3.14
*
pow
(a,
2
)
*
pow
(b,
2
))
/
(
4
*
(
pow
(a,
2
)
+
pow
(b,
2
))))
return
A
if
__name__
=
=
"__main__"
:
a
=
8
b
=
10
print
( circlearea(a, b))
|
C#
using
System;
public
class
GFG {
public
static
float
circlearea(
double
a,
double
b)
{
if
(a < 0 || b < 0)
return
-1 ;
float
A = (
float
) ((3.14 * Math.Pow(a, 2) * Math.Pow(b, 2))
/ (4 * (Math.Pow(a, 2) + Math.Pow(b, 2)))) ;
return
A ;
}
public
static
void
Main() {
float
a = 8, b = 10 ;
Console.WriteLine(circlearea(a, b));
}
}
|
PHP
<?php
function
circlearea(
$a
,
$b
)
{
if
(
$a
< 0 ||
$b
< 0)
return
-1;
$A
= (3.14 * pow(
$a
, 2) * pow(
$b
, 2)) /
(4 * (pow(
$a
, 2) + pow(
$b
, 2)));
return
$A
;
}
$a
= 8;
$b
= 10;
echo
circlearea(
$a
,
$b
);
?>
|
Javascript
<script>
function
circlearea(a , b)
{
if
(a < 0 || b < 0)
return
-1 ;
var
A = ((3.14 * Math.pow(a, 2) * Math.pow(b, 2))
/ (4 * (Math.pow(a, 2) + Math.pow(b, 2)))) ;
return
A ;
}
var
a = 8, b = 10 ;
document.write(circlearea(a, b).toFixed(4));
</script>
|
Time Complexity: O(1), as calculating squares using pow function is a constant time operation.
Auxiliary Space: O(1), as no extra space is required