在这个问题中,我们将学习检查两个给定三角形的相似性,从程序员的角度来看,这有许多现实世界的用例。
为了构建和管理事物的 2d 和 3d 模型,需要使用 cad 系统,其中一个关键功能是比较两个三角形的能力。
例如,从事设计和施工的工程师可能需要使建筑物的基础测量与蓝图相匹配。工程师可以利用 cad 工具快速评估基础的角度和边是否符合布局,该工具具有检查两个三角形相似性的内置功能。这有助于确保建筑物的结构稳定性和安全性。
此外,物体的3d模型是通过3d打印技术使用cad软件制作的。为了确保在这种情况下精确打印模型并达到所需的比例,相似性检查会很有帮助。这对于复杂的模型至关重要,因为手动验证相似性可能很乏味且容易出错。
机器人领域的程序员可以通过使用相似性检查工具来确保机器人运动的精度。检查两个三角形的相似性有助于确保机械臂(通常具有多个关节)进行的复杂运动精确且恒定。
说明现在让我们了解一些计算三角形相似度所涉及的数学。
如果两个三角形具有以下特征,则它们相似 -
两个三角形的内角相等。
三角形的对应边具有相同的比例。
判断两个三角形是否相似有三种方法:sss、sas、aa。让我们简要讨论每个定理。
sss(边-边-边)标准在两个给定的三角形中,如果三对边的比例相同,则这两个三角形相似。
让我们考虑上面给出的两个三角形。如果三对边的比例相等,则上述两个三角形根据sss标准可以相似,即ac/pr = ab/pq = cb/rq
sas(边-角-边)标准在两个给定的三角形中,如果两对边的比例相同,并且两个三角形中两条边之间的角度相同,则这两个三角形相似。
以上面的三角形为例,如果 ab/pq = bc/qr 且 aa(角度-角度)标准
在两个给定的三角形中,如果两个三角形的任意两个角相等,则这两个三角形相似。
如果我们以上面的三角形为例,那么如果 通常情况下,我们会得到三角形三个点的坐标,然后我们需要检查相似度。在这种情况下,我们将使用这个公式来计算距离。
在提供坐标时检查给定两个三角形的相似性的程序。
方法让我们将整个程序解码为逐步算法
将两个三角形的三个点的坐标作为输入。
使用上面讨论的公式计算坐标之间的长度,即距离= math。 sqrt(math.pow(y2-y1,2)+math.pow(x2-x1,2))
计算出两个三角形所有边的长度后,计算所有对的比率。
接下来,检查三个比例是否相同,如果相同,则打印三角形相似,否则打印三角形不相似。
现在,我们将编写实现上述算法的代码
示例c++ 程序,用于在提供坐标时检查给定两个三角形的相似性。
#include <iostream>#include <cmath>using namespace std;int main() { double x1 = 0, y1 = 0, x2 = 3, y2 = 0, x3 = 0, y3 = 4; //coordinates of first triangle (x1, y1), (x2, y2), (x3, y3) double p1 = 0, q1 = 0, p2 = 6, q2 = 0, p3 = 0, q3 = 8; //coordinates of second triangle (p1, q1), (p2, q2), (p3, q3) // calculate the distance between the coordinates of the first triangle double dist1 = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2)); double dist2 = sqrt(pow((x3 - x2), 2) + pow((y3 - y2), 2)); double dist3 = sqrt(pow((x1 - x3), 2) + pow((y1 - y3), 2)); // calculate the distance between the coordinates of the second triangle double dist4 = sqrt(pow((p2 - p1), 2) + pow((q2 - q1), 2)); double dist5 = sqrt(pow((p3 - p2), 2) + pow((q3 - q2), 2)); double dist6 = sqrt(pow((p1 - p3), 2) + pow((q1 - q3), 2)); // calculate the ratio of the length of the triangle double ratio1 = dist1/dist4; double ratio2 = dist2/dist5; double ratio3 = dist3/dist6; // check if the ratio of all three pairs of sides of the triangle are same, we are using sss criteria if ((ratio1 == ratio2) && (ratio2 == ratio3)) { cout << the two triangles are similar. << endl; } else { cout << the two triangles are not similar. << endl; } return 0;}
输出the two triangles are similar.
复杂性时间复杂度:o(1),因为无论输入大小如何,此代码都会执行固定数量的计算。
空间复杂度:o(1),因为代码使用固定数量的变量来存储输入值和结果,而不管输入的大小。
在提供坐标时检查给定两个三角形的相似性的程序。
方法让我们将整个程序解码为逐步算法
将三角形的角度作为输入。
比较角度,检查三角形的任意两个角是否相同,这里我们使用 aa 准则。
如果任意两个角相同,则打印三角形相似,否则打印三角形不相似。
现在,我们将编写实现上述算法的代码。
示例c++ 程序,用于在提供角度时检查给定两个三角形的相似性。
#include <iostream>using namespace std;bool check_aa(int a1,int a2,int a3,int a4,int a5,int a6){ if((a1==a4 || a1==a5 || a1==a6) && (a2==a4 || a2==a5 || a2==a6)) return true; else return false;}int main(){ // input: the angles of the triangles double a1 = 30, a2 = 60, a3 = 90; //angles of triangle a double a4 = 60, a5 = 90, a6 = 30; //angles of triangle b bool similar= check_aa(a1,a2,a3,a4,a5,a6); if (similar) cout << the two triangles are similar. << endl; else cout << the two triangles are not similar. << endl;}
输出the two triangles are similar.
复杂性时间复杂度:o(1),因为无论输入大小如何,此代码都会执行固定数量的计算。
空间复杂度:o(1),因为代码使用固定数量的变量来存储输入值和结果,而不管输入的大小。
结论在本文中,我们尝试基于两种情况解释检查两个三角形相似性的方法,一种是提供边作为输入,另一种是提供角度作为输入。我希望这篇文章可以帮助您更好地学习这个概念。
以上就是检查给定的两个三角形的相似性的程序的详细内容。
