# Basics – Triangles Cody Solutions

Learn Matlab and earn a badge and skill. This section contains the Cody solution of Basics – Triangles Cody Solution. Total 9/9 solved problems.

Visit the Matlab official Problems page of Basics – Triangles

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

Get Notified Faster

## Problem 769. Calculate the area of a triangle between three points

Calculate the area of a triangle between three points:
P1(X1,Y1)
P2(X2,Y2)
P3(X3,Y3)
these three points are the vertices of the triangle.
``````function y = your_fcn_name(X,Y)
y = abs(det([X(2)-X(1),X(3)-X(1);...
Y(2)-Y(1),Y(3)-Y(1)]))/2;
end``````

## Problem 2017. Side of an equilateral triangle

If an equilateral triangle has area A, then what is the length of each of its sides, x?
``````function x = side_length(A)
x = sqrt(A*4/sqrt(3))
end``````

## Problem 2023. Is this triangle right-angled?

Given any three positive numbers a, b, c, return true if the triangle with sides a, b and c is right-angled. Otherwise, return false.
``````function flag = isRightAngled(a,b,c)
v=[a b c];
v=v.*v
flag = 2*max(v)==sum(v);
end``````

## Problem 43236. Find my daddy long leg (No 's')

Given the ratio of the two legs (longer / shorter), and the hypotenuse length, find the value of the bigger leg.
``````function ans = myDaddyLongLeg(x,ratio)
x/1.1473
end``````

## Problem 1103. Right Triangle Side Lengths (Inspired by Project Euler Problem 39)

If p is the perimeter of a right angle triangle with integral length sides, { a, b, c }, there are exactly three solutions for p = 120.
{[20,48,52], [24,45,51], [30,40,50]}
Given any value of p, return a cell array whose elements are the sorted side lengths of a possible right triangle whose perimeter is p. Furthermore, the elements of the output should be sorted by their shortest side length.
``````function c = right_triangle_sides(p)
array = [];
for a = 1:p
for b = 1:a
h = sqrt(a*a + b*b);
if (a+b+h)==p
array = [array ; sort([a b h])];
end
end
end
[rows cols] = size(array);
for i = rows:-1:1
c(rows-i+1) = {array(i,:)};
end
end``````

## Problem 558. Is the Point in a Triangle?

Check whether a point or multiple points is/are in a triangle with three corners
Points = [x, y];
Triangle = [x1, y1; x2, y2; x3, y3]
Return true or false for each point tested.
For example,
input: Points = [0, 0.5]; Triangle = [0, 0; 1, 0; 1, 1]
output: y = 0;
``````function y = your_fcn_name(Points, Triangle)
y=[];
for i=1:numel(Points)/2
X=Triangle(1,:)-Points(i,:);
Y=Triangle(2,:)-Points(i,:);
Z=Triangle(3,:)-Points(i,:);
XY=acos(dot(X,Y)/sqrt(dot(X,X))/sqrt(dot(Y,Y)));
YZ=acos(dot(Z,Y)/sqrt(dot(Z,Z))/sqrt(dot(Y,Y)));
ZX=acos(dot(X,Z)/sqrt(dot(X,X))/sqrt(dot(Z,Z)));
if XY+YZ+ZX==2*pi
y=[y,true];
else
y=[y,false];
end
end
end``````

## Problem 42855. Height of a right-angled triangle

Given numbers a, b and c, find the height of the right angled triangle with sides a and b and hypotenuse c, for the base c. If a right angled triangle with sides a and b and hypotenuse c does not exist, return NaN (not-a-number).
``````function y = triangle_height(a, b, c)
if  sqrt(a*a + b*b)==c && a>0 && b>0 && c>0
sides = [a b];
small = min(sides);
long = max(sides);
hyp = c;
y = (0.5000*((hyp + long - small)*(hyp - long + small)*(long - hyp + small)*(hyp + long + small))^(1/2))/hyp;
else
y = NaN;
end``````

## Problem 43599. Find the sides of an isosceles triangle when given its area and height from its base to apex

Find the sides of an isosceles triangle when given its area and the height from its base to apex.
For example, with A=12 and h=4, the result will be [5 5 6].
``````function y = sidesOfTheTriangle(A,h)
a = sqrt(h*h+A*A/(h*h));
y = [a a 2*A/h];
end``````

## Problem 43294. Can we make a triangle?

Given three positive number, check whether a triangle can be made with these sides length or not. remember that in a triangle sum of two sides should be greater than the third one. So with the lengths of 2,3 and 6 we can not make a triangle
``````function flag = Is_Triangle(a, b, c)
v=[a b c];
flag=(2*max(v)<sum(v));
end``````

Basics – Triangles 9/9 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.

0