Dados dos vectores en forma de (xi+yj+zk) de dos lados adyacentes de un triángulo. La tarea es encontrar el área de un triángulo.
Ejemplos:
Entrada:
x1 = -2, y1 = 0, z1 = -5
x2 = 1, y2 = -2, z2 = -1
Salida: Área = 6,422616289332565
Entrada:
x1 = -2, y1 = 1, z1 = 5
x2 = 1 , y2 = 3, z2 = -1
Salida: Área = 8,860022573334675
Enfoque: supongamos que tenemos dos vectores a(x1*i+y1*j+z1*k) y b(x2*i+y2*j+z2*k) y sabemos que el área del triángulo está dada por:
Área del triángulo = (magnitud del producto vectorial de los vectores a y b) / 2, es decir, |axb| / 2
Y sabemos a X b = (y1*z2 – y2*z1)*i – (x1*z2 – x2*z1)*j + (x1*y2 – x2*y1)*k Entonces área
=
C++
// C++ program to calculate area of // triangle if vectors of // 2 adjacent sides are given #include<bits/stdc++.h> using namespace std ; // function to calculate area of triangle float area(int x1, int y1, int z1, int x2, int y2, int z2) { float area = sqrt(pow((y1 * z2 - y2 * z1),2) + pow((x1 * z2 - x2 * z1),2) + pow((x1 * y2 - x2 * y1),2)) ; area = area / 2; return area ; } // Driver Code int main() { int x1 = -2 ; int y1 = 0 ; int z1 = -5 ; int x2 = 1 ; int y2 = -2 ; int z2 = -1 ; float a = area(x1, y1, z1, x2, y2, z2) ; cout << "Area = " << a << endl; return 0; // This code is contributed by ANKITRAI1 }
Java
// Java program to calculate area of // triangle if vectors of // 2 adjacent sides are given import java.util.*; class solution { // function to calculate area of triangle static float area(int x1, int y1, int z1, int x2, int y2, int z2) { double a =Math.pow((y1 * z2 - y2 * z1),2) + Math.pow((x1 * z2 - x2 * z1),2) + Math.pow((x1 * y2 - x2 * y1),2); float area = (float)Math.sqrt(a) ; area = area / 2; return area ; } //Driver program public static void main(String arr[]) { int x1 = -2 ; int y1 = 0 ; int z1 = -5 ; int x2 = 1 ; int y2 = -2 ; int z2 = -1 ; float a = area(x1, y1, z1, x2, y2, z2) ; System.out.println("Area= "+a); } } //This code is contributed by //Surendra_Gangwar
Python 3
# Python code to calculate area of # triangle if vectors of # 2 adjacent sides are given import math # to calculate area of triangle def area(x1, y1, z1, x2, y2, z2): area = math.sqrt((y1 * z2 - y2 * z1) ** 2 + (x1 * z2 - x2 * z1) ** 2 + (x1 * y2 - x2 * y1) ** 2) area = area / 2 return area # main function def main(): x1 = -2 y1 = 0 z1 = -5 x2 = 1 y2 = -2 z2 = -1 a = area(x1, y1, z1, x2, y2, z2) print("Area = ", a) # driver code if __name__=="__main__": main()
C#
// C# program to calculate area // of triangle if vectors of // 2 adjacent sides are given using System; class GFG { // function to calculate area of triangle static float area(int x1, int y1, int z1, int x2, int y2, int z2) { double a = Math.Pow((y1 * z2 - y2 * z1), 2) + Math.Pow((x1 * z2 - x2 * z1), 2) + Math.Pow((x1 * y2 - x2 * y1), 2); float area = (float)Math.Sqrt(a) ; area = area / 2; return area ; } // Driver Code public static void Main() { int x1 = -2; int y1 = 0; int z1 = -5; int x2 = 1; int y2 = -2; int z2 = -1; float a = area(x1, y1, z1, x2, y2, z2); Console.WriteLine("Area = " + a); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to calculate area // of triangle if vectors of // 2 adjacent sides are given // function to calculate area of triangle function area($x1, $y1, $z1, $x2, $y2, $z2) { $area = sqrt(pow(($y1 * $z2 - $y2 * $z1), 2) + pow(($x1 * $z2 - $x2 * $z1), 2) + pow(($x1 * $y2 - $x2 * $y1), 2)); $area = $area / 2; return $area ; } // Driver Code $x1 = -2 ; $y1 = 0 ; $z1 = -5 ; $x2 = 1 ; $y2 = -2 ; $z2 = -1 ; $a = area($x1, $y1, $z1, $x2, $y2, $z2); echo "Area = ".$a ."\n"; // This code is contributed by ChitraNayal ?>
Javascript
<script> // Javascript program to calculate area of // triangle if vectors of // 2 adjacent sides are given // function to calculate area of triangle function area( x1, y1, z1, x2, y2, z2) { let area = Math.sqrt(Math.pow((y1 * z2 - y2 * z1),2) + Math.pow((x1 * z2 - x2 * z1),2) + Math.pow((x1 * y2 - x2 * y1),2)) ; area = area / 2; return area ; } // Driver Code let x1 = -2 ; let y1 = 0 ; let z1 = -5 ; let x2 = 1 ; let y2 = -2 ; let z2 = -1 ; let a = area(x1, y1, z1, x2, y2, z2) ; document.write( "Area= " +a); // This code contributed by aashish1995 </script>
Area = 6.422616289332565
Complejidad de tiempo: O(1), el código se ejecutará en un tiempo constante.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Publicación traducida automáticamente
Artículo escrito por AashutoshChauhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA