📌  相关文章
📜  程序从给定的坐标中找到三角形的类型

📅  最后修改于: 2021-10-23 08:06:04             🧑  作者: Mango

我们得到了一个三角形的坐标。任务是根据边和角对这个三角形进行分类。
例子:

Input: p1 = (3, 0), p2 = (0, 4), p3 = (4, 7)
Output: Right Angle triangle and Isosceles

Input: p1 = (0, 0), p2 = (1, 1), p3 = (1, 2);
Output: Triangle is obtuse and Scalene

方法:

  • 我们可以通过先计算边长,然后根据边长的比较进行分类来解决这个问题。按边分类很简单,如果所有边都相等,则三角形是等边的,如果任何两条边相等,则三角形为等腰三角形,否则为不等边
  • 现在角可以用毕达哥拉斯定理分类,如果两条边的平方和等于第三条边的平方,三角形就是直角,如果三角形少了就是锐角,否则就是钝角三角形。

下面是编写三角形分类的简单代码:

C++
// C/C++ program to classify a given triangle
 
#include 
using namespace std;
 
struct point {
    int x, y;
    point() {}
    point(int x, int y)
        : x(x), y(y)
    {
    }
};
 
// Utility method to return square of x
int square(int x)
{
    return x * x;
}
 
// Utility method to sort a, b, c; after this
// method a <= b <= c
void order(int& a, int& b, int& c)
{
    int copy[3];
    copy[0] = a;
    copy[1] = b;
    copy[2] = c;
    sort(copy, copy + 3);
    a = copy[0];
    b = copy[1];
    c = copy[2];
}
 
// Utility method to return Square of distance
// between two points
int euclidDistSquare(point p1, point p2)
{
    return square(p1.x - p2.x) + square(p1.y - p2.y);
}
 
// Method to classify side
string getSideClassification(int a, int b, int c)
{
    // if all sides are equal
    if (a == b && b == c)
        return "Equilateral";
 
    // if any two sides are equal
    else if (a == b || b == c)
        return "Isosceles";
 
    else
        return "Scalene";
}
 
// Method to classify angle
string getAngleClassification(int a, int b, int c)
{
    // If addition of sum of square of two side
    // is less, then acute
    if (a + b > c)
        return "acute";
 
    // by pythagoras theorem
    else if (a + b == c)
        return "right";
 
    else
        return "obtuse";
}
 
// Method to classify the triangle by sides and angles
void classifyTriangle(point p1, point p2, point p3)
{
    // Find squares of distances between points
    int a = euclidDistSquare(p1, p2);
    int b = euclidDistSquare(p1, p3);
    int c = euclidDistSquare(p2, p3);
 
    // Sort all squares of distances in increasing order
    order(a, b, c);
 
    cout << "Triangle is "
                + getAngleClassification(a, b, c)
                + " and "
                + getSideClassification(a, b, c)
         << endl;
}
 
// Driver code
int main()
{
    point p1, p2, p3;
    p1 = point(3, 0);
    p2 = point(0, 4);
    p3 = point(4, 7);
    classifyTriangle(p1, p2, p3);
 
    p1 = point(0, 0);
    p2 = point(1, 1);
    p3 = point(1, 2);
    classifyTriangle(p1, p2, p3);
    return 0;
}


Java
// Java program to classify a given triangle
import java.util.*;
class GFG
{
     
static class point
{
    int x, y;
    point() {}
 
    public point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
};
 
// Utility method to return square of x
static int square(int x)
{
    return x * x;
}
static int a, b, c;
 
// Utility method to sort a, b, c; after this
// method a <= b <= c
static void order()
{
    int []copy = new int[3];
    copy[0] = a;
    copy[1] = b;
    copy[2] = c;
    Arrays.sort(copy);
    a = copy[0];
    b = copy[1];
    c = copy[2];
}
 
// Utility method to return Square of distance
// between two points
static int euclidDistSquare(point p1, point p2)
{
    return square(p1.x - p2.x) + square(p1.y - p2.y);
}
 
// Method to classify side
static String getSideClassification(int a,
                                    int b, int c)
{
    // if all sides are equal
    if (a == b && b == c)
        return "Equilateral";
 
    // if any two sides are equal
    else if (a == b || b == c)
        return "Isosceles";
 
    else
        return "Scalene";
}
 
// Method to classify angle
static String getAngleClassification(int a,
                                     int b, int c)
{
    // If addition of sum of square of two side
    // is less, then acute
    if (a + b > c)
        return "acute";
 
    // by pythagoras theorem
    else if (a + b == c)
        return "right";
 
    else
        return "obtuse";
}
 
// Method to classify the triangle
// by sides and angles
static void classifyTriangle(point p1,
                             point p2, point p3)
{
    // Find squares of distances between points
    a = euclidDistSquare(p1, p2);
    b = euclidDistSquare(p1, p3);
    c = euclidDistSquare(p2, p3);
 
    // Sort all squares of distances in increasing order
    order();
 
    System.out.println( "Triangle is "
                + getAngleClassification(a, b, c)
                + " and "
                + getSideClassification(a, b, c));
}
 
// Driver code
public static void main(String[] args)
{
    point p1, p2, p3;
    p1 = new point(3, 0);
    p2 = new point(0, 4);
    p3 = new point(4, 7);
    classifyTriangle(p1, p2, p3);
 
    p1 = new point(0, 0);
    p2 = new point(1, 1);
    p3 = new point(1, 2);
    classifyTriangle(p1, p2, p3);
}
}
 
// This code is contributed by Rajput-Ji


C#
// C# program to classify a given triangle
using System;
     
class GFG
{
public class point
{
    public int x, y;
    public point() {}
 
    public point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
};
 
// Utility method to return square of x
static int square(int x)
{
    return x * x;
}
static int a, b, c;
 
// Utility method to sort a, b, c;
// after this method a <= b <= c
static void order()
{
    int []copy = new int[3];
    copy[0] = a;
    copy[1] = b;
    copy[2] = c;
    Array.Sort(copy);
    a = copy[0];
    b = copy[1];
    c = copy[2];
}
 
// Utility method to return
// Square of distance between two points
static int euclidDistSquare(point p1,
                            point p2)
{
    return square(p1.x - p2.x) +
           square(p1.y - p2.y);
}
 
// Method to classify side
static String getSideClassification(int a,
                                    int b, int c)
{
    // if all sides are equal
    if (a == b && b == c)
        return "Equilateral";
 
    // if any two sides are equal
    else if (a == b || b == c)
        return "Isosceles";
 
    else
        return "Scalene";
}
 
// Method to classify angle
static String getAngleClassification(int a,
                                     int b, int c)
{
    // If addition of sum of square of
    // two side is less, then acute
    if (a + b > c)
        return "acute";
 
    // by pythagoras theorem
    else if (a + b == c)
        return "right";
 
    else
        return "obtuse";
}
 
// Method to classify the triangle
// by sides and angles
static void classifyTriangle(point p1,
                              point p2,
                              point p3)
{
    // Find squares of distances between points
    a = euclidDistSquare(p1, p2);
    b = euclidDistSquare(p1, p3);
    c = euclidDistSquare(p2, p3);
 
    // Sort all squares of distances
    // in increasing order
    order();
 
    Console.WriteLine( "Triangle is "
                + getAngleClassification(a, b, c)
                + " and "
                + getSideClassification(a, b, c));
}
 
// Driver code
public static void Main(String[] args)
{
    point p1, p2, p3;
    p1 = new point(3, 0);
    p2 = new point(0, 4);
    p3 = new point(4, 7);
    classifyTriangle(p1, p2, p3);
 
    p1 = new point(0, 0);
    p2 = new point(1, 1);
    p3 = new point(1, 2);
    classifyTriangle(p1, p2, p3);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

Triangle is right and Isosceles
Triangle is obtuse and Scalene

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程