例1
Problem 769. Calculate the area of a triangle between three points
alculate 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.
(计算三个点构成的三角形的面积: P1(X1,Y1) P2(X2,Y2) P3(X3,Y3) 这三个点是三角形的顶点。)
可以将这个函数保存到一个名为calculate_triangle_area.m
的文件中,并在MATLAB中调用它来计算三角形的面积。
例2
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?
Image courtesy of Wikipedia.
(如果一个等边三角形的面积为A,那么它的每条边的长度是多少,x?
图片由维基百科提供)
function side_length = equilateral_triangle_side_length(A)
% 计算等边三角形每条边的长度
% A: 三角形的面积
% 计算等边三角形的边长公式
side_length = sqrt(4 * A / sqrt(3));
end
% 给定面积A
A = 10; % 可以替换为任何正数
% 调用函数计算边长
x = equilateral_triangle_side_length(A);
% 打印结果
disp(['等边三角形每条边的长度为: ', num2str(x)]);
例3
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.
(给定任意三个正数 a、b、c,如果以这三个数为边长的三角形是直角三角形,则返回 true。否则,返回 false)
以下是相应的 MATLAB 代码,它可以判断给定任意三个正数 a、b、c 是否组成一个直角三角形,并返回 true 或 false:
function is_right_angled = check_right_angled_triangle(a, b, c)
% 给定任意三个正数 a、b、c,判断是否为直角三角形
% 将 a、b、c 排序,确保 a^2 + b^2 是否等于 c^2
sides = sort([a, b, c]);
% 计算 a^2 + b^2
sum_of_squares = sides(1)^2 + sides(2)^2;
% 计算 c^2
square_of_c = sides(3)^2;
% 比较两个值,如果相等,则为直角三角形
if abs(sum_of_squares - square_of_c) < 1e-10 % 考虑浮点误差
is_right_angled = true;
else
is_right_angled = false;
end
end
% 示例用法
a = 3;
b = 4;
c = 5;
% 调用函数判断是否为直角三角形
result = check_right_angled_triangle(a, b, c);
% 显示结果
disp(['Given sides (a = ', num2str(a), ', b = ', num2str(b), ', c = ', num2str(c), '), the triangle is right-angled: ', num2str(result)]);
在这个代码中,我们首先将三个边长排序,以确保最小的两个数的平方和与最大的数的平方比较。然后,我们比较这两个值,如果它们相等,则返回 true,否则返回 false。
例4
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.
(给定两腿之比(长腿 / 短腿)和斜边长度,找出较长腿的值。)
以下是相应的 MATLAB 代码,用于根据两腿之比和斜边长度找出较长腿的值:
function longer_leg = find_longer_leg(ratio, hypotenuse)
% 给定两腿之比和斜边长度,找出较长腿的值
% 计算较短腿的长度
shorter_leg = hypotenuse / sqrt(ratio^2 + 1);
% 计算较长腿的长度
longer_leg = shorter_leg * ratio;
end
% 示例用法
ratio = 3; % 两腿之比(长腿 / 短腿)
hypotenuse = 10; % 斜边长度
% 调用函数计算较长腿的值
longer_leg = find_longer_leg(ratio, hypotenuse);
% 显示结果
disp(['The length of the longer leg is: ', num2str(longer_leg)]);
在这个代码中,我们利用勾股定理根据给定的两腿之比和斜边长度,先计算出较短腿的长度,然后再根据两腿之比得出较长腿的长度。
例5
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.
(如果 p 是一个拥有整数长度边的直角三角形的周长,{ a, b, c },那么对于 p = 120,恰好有三个解。{[20,48,52], [24,45,51], [30,40,50]}。 给定任何值 p,返回一个单元数组,其元素是周长为 p 的可能的直角三角形的边长度,而且输出的元素应该按照它们的最短边长度排序。)
下面是用 MATLAB 编写的函数,可以根据给定的 p 返回可能的直角三角形的边长度,且按照最短边长度排序:
function sides = rightTriangleSides(p)
sides = {}; % 初始化存储边长度的单元数组
% 遍历可能的边长度组合
for a = 1:p
for b = a:p
c = p - a - b; % 根据周长计算第三边长度
if c > 0 && a^2 + b^2 == c^2 % 检查是否构成直角三角形
% 将边长度组成三元组并按照最短边长度排序后加入单元数组
sides{end+1} = sort([a, b, c]);
end
end
end
% 按照最短边长度排序
sides = unique(sides, 'rows'); % 去重
sides = sortrows(sides); % 按照最短边长度排序
end
% 示例用法:
p = 120;
triangleSides = rightTriangleSides(p);
disp(triangleSides);
这个函数 rightTriangleSides
接受一个参数 p
,表示直角三角形的周长。它通过遍历可能的边长度组合来找到满足条件的直角三角形,然后将边长度组成三元组,并按照最短边长度排序后加入单元数组中。最后,去重并按照最短边长度排序。
C = unique(A):返回的是和A中一样的值,但是没有重复元素。产生的结果向量按升序排序。
示例:
1.筛除向量中的重复值,产生的结果按升序排列
Define a vector with a repeated value.
A = [9 2 9 5];
Find the unique values of A.
C = unique(A)
C =
2 5 9
————————————————
原文链接:https://blog.csdn.net/sinat_40282753/article/details/78373532
例6
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;(检查一个或多个点是否在具有三个角的三角形内。
点 = [x, y]; 三角形 = [x1, y1; x2, y2; x3, y3] 对于每个测试点返回true或false。
例如, 输入:点 = [0, 0.5]; 三角形 = [0, 0; 1, 0; 1, 1] 输出:y = 0;)
以下是相应的 MATLAB 代码,用于检查一个或多个点是否在具有三个角的三角形内:
function in_triangle = point_in_triangle(point, triangle)
% 检查一个或多个点是否在具有三个角的三角形内
% 提取点的坐标
x = point(:, 1);
y = point(:, 2);
% 提取三角形的顶点坐标
x1 = triangle(1, 1);
y1 = triangle(1, 2);
x2 = triangle(2, 1);
y2 = triangle(2, 2);
x3 = triangle(3, 1);
y3 = triangle(3, 2);
% 计算三角形的面积
triangle_area = 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
% 计算三个子三角形的面积
sub_triangle1_area = 0.5 * abs(x * (y1 - y3) + x1 * (y3 - y) + x3 * (y - y1));
sub_triangle2_area = 0.5 * abs(x * (y2 - y1) + x2 * (y - y1) + x * (y1 - y2));
sub_triangle3_area = 0.5 * abs(x1 * (y - y3) + x * (y3 - y2) + x3 * (y2 - y));
% 判断点是否在三角形内部
in_triangle = sub_triangle1_area + sub_triangle2_area + sub_triangle3_area <= triangle_area;
end
% 示例用法
point = [0, 0.5]; % 测试点坐标
triangle = [0, 0; 1, 0; 1, 1]; % 三角形的顶点坐标
% 调用函数检查点是否在三角形内
in_triangle = point_in_triangle(point, triangle);
% 显示结果
disp(['Point is inside triangle: ', num2str(in_triangle)]);
在这个代码中,我们利用三角形的顶点坐标和给定点的坐标,通过计算三个子三角形的面积之和来判断点是否在三角形内部。
例7
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).
(给出MATLAB“给定数字 a、b 和 c,找到以 c 为底边的直角三角形的高,其中直角边分别为 a 和 b,斜边为 c。如果不存在以 a 和 b 为直角边,c 为斜边的直角三角形,则返回 NaN(非数字))
function height = find_triangle_height(a, b, c)
% 检查是否存在以 a 和 b 为直角边,c 为斜边的直角三角形
if ~(a^2 + b^2 == c^2)
height = NaN;
return;
end
% 计算直角三角形的高
height = 2 * (a * b) / c;
end
例7
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].
(当给定等腰三角形的面积和从底边到顶点的高度时,找出其边长。 例如,当 A=12 且 h=4 时,结果将是 [5 5 6])
function sides = find_isosceles_triangle_sides(A, h)
% 使用等腰三角形的面积公式计算底边长度
base = 2 * A / h;
% 使用勾股定理计算腰长
leg = sqrt((base / 2)^2 + h^2);
% 输出结果
sides = [leg, leg, base];
end
可以将这个函数保存在一个名为 find_isosceles_triangle_sides.m
的文件中,并在 MATLAB 中调用它来找到给定面积和高度的等腰三角形的边长。例如,使用 find_isosceles_triangle_sides(12, 4)
将会返回 [5, 5, 6]
。
例8
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
(给定三个正数,请检查是否可以用这些边长构成一个三角形。请记住,在三角形中,任意两边之和必须大于第三边。因此,对于长度为2、3和6的边长,我们无法构成一个三角形。)
function can_form_triangle = check_triangle(a, b, c)
% 检查三角形的边长是否满足三角不等式
if a + b > c && a + c > b && b + c > a
can_form_triangle = true;
else
can_form_triangle = false;
end
end
你可以调用这个函数,并传入三个正数作为参数,以检查是否可以构成一个三角形。