Programa para encontrar el Tipo de Triángulo a partir de las Coordenadas dadas – Part 1

Nos dan las coordenadas de un triángulo. La tarea es clasificar este triángulo sobre la base de los lados y el ángulo.
Ejemplos: 
 

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

Acercarse: 
 

  • Podemos resolver este problema calculando primero la longitud de los lados y luego clasificando al comparar las longitudes de los lados. La clasificación por lados es simple, si todos los lados son iguales, el triángulo será equilátero , si dos lados son iguales, el triángulo será isósceles , de lo contrario, será escaleno .
  • Ahora el ángulo se puede clasificar por el teorema de Pitágoras, si la suma del cuadrado de dos lados es igual al cuadrado del tercer lado, el triángulo será un ángulo recto , si el triángulo menor será un ángulo agudo, de lo contrario será un triángulo obtuso .

A continuación se escribe un código simple para la clasificación del triángulo: 
 

C++

// C/C++ program to classify a given triangle
 
#include <bits/stdc++.h>
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

Python3

# Python program to classify a given triangle
class point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
# Utility method to return square of x
def square(x):
    return x * x
 
# Utility method to sort a, b, c; after this
# method a <= b <= c
def order(a, b, c):
    copy = [a, b, c]
    copy.sort()
    return copy[0], copy[1], copy[2]
 
# Utility method to return Square of distance
# between two points
def euclidDistSquare(p1, p2):
    return square(p1.x - p2.x) + square(p1.y - p2.y)
 
#  Method to classify side
def getSideClassification(a, b, c):
    # if all sides are equal
    if a == b and b == c:
        return "Equilateral"
 
    # if any two sides are equal
    elif a == b or b == c:
        return "Isosceles"
    else:
        return "Scalene"
 
#  Method to classify angle
def getAngleClassification(a, b, c):
   
    # If addition of sum of square of two side
    # is less, then acute
    if a + b > c:
        return "acute"
 
    # by pythagoras theorem
    elif a + b == c:
        return "right"
    else:
        return "obtuse"
 
# Method to classify the triangle by sides and angles
def classifyTriangle(p1, p2, 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
    a, b, c = order(a, b, c)
    print("Triangle is ", getAngleClassification(a, b, c),
          " and ",  getSideClassification(a, b, c))
 
# Driver code
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)
 
# The code is contributed by Gautam goel (gautamgoel962)

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

<script>
// Javascript program to classify a given triangle
 
class point
{
    constructor(x,y)
    {
        this.x = x;
        this.y = y;
    }
}
 
// Utility method to return square of x
function square(x)
{
    return x * x;
}
let a, b, c;
 
// Utility method to sort a, b, c; after this
// method a <= b <= c
function order()
{
    let copy = new Array(3);
    copy[0] = a;
    copy[1] = b;
    copy[2] = c;
    (copy).sort(function(a,b){return a-b;});
    a = copy[0];
    b = copy[1];
    c = copy[2];
}
 
// Utility method to return Square of distance
// between two points
function euclidDistSquare(p1,p2)
{
    return square(p1.x - p2.x) + square(p1.y - p2.y);
}
 
// Method to classify side
function getSideClassification(a,b,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
function getAngleClassification(a, b, 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
function classifyTriangle(p1, p2, 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();
   
    document.write( "Triangle is "
                + getAngleClassification(a, b, c)
                + " and "
                + getSideClassification(a, b, c)+"<br>");
}
 
// Driver code
let 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 rag2127
</script>
Producción: 

Triangle is right and Isosceles
Triangle is obtuse and Scalene

 

Complejidad del tiempo : O(1)

Espacio Auxiliar : O(1)

Este artículo es una contribución de Utkarsh Trivedi . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *