Dadas las coordenadas de tres puntos en un plano P1 , P2 y P3 , la tarea es verificar si los tres puntos forman un triángulo o no
. Ejemplos:
Entrada: P1 = (1, 5), P2 = (2, 5), P3 = (4, 6)
Salida: Sí
Entrada: P1 = (1, 1), P2 = (1, 4), P3 = (1 , 5)
Salida: No
Enfoque: La observación clave en el problema es que tres puntos forman un triángulo solo cuando no están en línea recta, es decir, un área formada por el triángulo de estos tres puntos no es igual a cero.
La fórmula anterior se deriva de la fórmula del cordón .
Entonces comprobaremos si el área formada por el triángulo es cero o no.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check // if three points form a triangle #include <bits/stdc++.h> using namespace std; // Function to check if three // points make a triangle void checkTriangle(int x1, int y1, int x2, int y2, int x3, int y3) { // Calculation the area of // triangle. We have skipped // multiplication with 0.5 // to avoid floating point // computations int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); // Condition to check if // area is not equal to 0 if (a == 0) cout << "No"; else cout << "Yes"; } // Driver Code int main() { int x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 2, y3 = 3; checkTriangle(x1, y1, x2, y2, x3, y3); return 0; }
Java
// Java implementation to check // if three points form a triangle import java.io.*; import java.util.*; class GFG { // Function to check if three // points make a triangle static void checkTriangle(int x1, int y1, int x2, int y2, int x3, int y3) { // Calculation the area of // triangle. We have skipped // multiplication with 0.5 // to avoid floating point // computations int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); // Condition to check if // area is not equal to 0 if (a == 0) System.out.println("No"); else System.out.println("Yes"); } // Driver code public static void main(String[] args) { int x1 = 1, y1 = 1, x2 = 2, y2 = 2, x3 = 3, y3 = 3; checkTriangle(x1, y1, x2, y2, x3, y3); } } // This code is contributed by coder001
Python3
# Python3 implementation to check # if three points form a triangle # Function to check if three # points make a triangle def checkTriangle(x1, y1, x2, y2, x3, y3): # Calculation the area of # triangle. We have skipped # multiplication with 0.5 # to avoid floating point # computations a = (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) # Condition to check if # area is not equal to 0 if a == 0: print('No') else: print('Yes') # Driver code if __name__=='__main__': (x1, x2, x3) = (1, 2, 3) (y1, y2, y3) = (1, 2, 3) checkTriangle(x1, y1, x2, y2, x3, y3) # This code is contributed by rutvik_56
C#
// C# implementation to check // if three points form a triangle using System; class GFG { // Function to check if three // points make a triangle static void checkTriangle(int x1, int y1, int x2, int y2, int x3, int y3) { // Calculation the area of // triangle. We have skipped // multiplication with 0.5 // to avoid floating point // computations int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); // Condition to check if // area is not equal to 0 if (a == 0) Console.WriteLine("No"); else Console.WriteLine("Yes"); } // Driver code public static void Main() { int x1 = 1, y1 = 1, x2 = 2, y2 = 2, x3 = 3, y3 = 3; checkTriangle(x1, y1, x2, y2, x3, y3); } } //This code is contributed by AbhiThakur
Javascript
<script> // Javascript implementation to check // if three points form a triangle // Function to check if three // points make a triangle function checkTriangle(x1, y1, x2, y2, x3, y3) { // Calculation the area of // triangle. We have skipped // multiplication with 0.5 // to avoid floating point // computations let a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); // Condition to check if // area is not equal to 0 if (a == 0) document.write("No"); else document.write("Yes"); } // Driver Code let x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 2, y3 = 3; checkTriangle(x1, y1, x2, y2, x3, y3); // This code is contributed by Mayank Tyagi </script>
Producción:
No
Complejidad de tiempo: O(1)
Espacio Auxiliar : O(1)