Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square
Last Updated :
27 Aug, 2022
Given a positive integer N representing the side of a square, the task is to find the area of a triangle formed by connecting the midpoints of two adjacent sides and vertex opposite to the two sides.
Examples:
Input: N = 10
Output: 37.5
Input: N = 1
Output: 0.375
Approach: The given problem can be solved based on the following observations:
- The one side of the triangle will be the hypotenuse of the triangle formed with the vertices as two middle point and one vertex of the square at the intersection of the sides whose length of the side is given by .
- The length of the other two sides of the triangle is given by .
- Now, the sides of the triangle are known, therefore, the area of the triangle can be calculated using the Heron’s Formula.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double areaOftriangle( int side)
{
double a = sqrt ( pow (side / 2, 2)
+ pow (side / 2, 2));
double b = sqrt ( pow (side, 2)
+ pow (side / 2, 2));
double c = sqrt ( pow (side, 2)
+ pow (side / 2, 2));
double s = (a + b + c) / 2;
double area = sqrt (s * (s - a)
* (s - b) * (s - c));
return area;
}
int main()
{
int N = 10;
cout << areaOftriangle(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double areaOftriangle( int side)
{
double a = Math.sqrt(Math.pow(side / 2 , 2 ) +
Math.pow(side / 2 , 2 ));
double b = Math.sqrt(Math.pow(side, 2 ) +
Math.pow(side / 2 , 2 ));
double c = Math.sqrt(Math.pow(side, 2 ) +
Math.pow(side / 2 , 2 ));
double s = (a + b + c) / 2 ;
double area = Math.sqrt(s * (s - a) *
(s - b) * (s - c));
return area;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.print(areaOftriangle(N));
}
}
|
Python3
from math import sqrt
def areaOftriangle(side):
a = sqrt( pow (side / 2 , 2 ) + pow (side / 2 , 2 ))
b = sqrt( pow (side, 2 ) + pow (side / 2 , 2 ))
c = sqrt( pow (side, 2 ) + pow (side / 2 , 2 ))
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
return round (area, 1 )
if __name__ = = '__main__' :
N = 10
print (areaOftriangle(N))
|
C#
using System;
class GFG{
static double areaOftriangle( int side)
{
double a = Math.Sqrt(Math.Pow(side / 2, 2) +
Math.Pow(side / 2, 2));
double b = Math.Sqrt(Math.Pow(side, 2) +
Math.Pow(side / 2, 2));
double c = Math.Sqrt(Math.Pow(side, 2) +
Math.Pow(side / 2, 2));
double s = (a + b + c) / 2;
double area = Math.Sqrt(s * (s - a) *
(s - b) * (s - c));
return area;
}
public static void Main( string [] args)
{
int N = 10;
Console.WriteLine(areaOftriangle(N));
}}
|
Javascript
<script>
function areaOftriangle(side)
{
let a = Math.sqrt(Math.pow(side / 2, 2) +
Math.pow(side / 2, 2));
let b = Math.sqrt(Math.pow(side, 2) +
Math.pow(side / 2, 2));
let c = Math.sqrt(Math.pow(side, 2) +
Math.pow(side / 2, 2));
let s = (a + b + c) / 2;
let area = Math.sqrt(s * (s - a) *
(s - b) * (s - c));
return area.toFixed(1);
}
let N = 10;
document.write(areaOftriangle(N));
</script>
|
Time Complexity: O(logn) because using inbuilt sqrt function
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...