Find area of parallelogram if vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a parallelogram. The task is to find out the area of a parallelogram.
Example:
Input:
x1 = 3, y1 = 1, z1 = -2
x2 = 1, y2 = -3, z2 = 4
Output: Area = 17.3205081
Input:
x1 = 1, y1 = 3, z1 = 2
x2 = 1, y2 = -3, z2 = 4
Output: Area = 19.078784028338912
Approach: Suppose we have two vectors a(x1*i+y1*j+z1*k) and b(x2*i+y2*j+z2*k) and we know that area of parallelogram is given by :
Area of parallelogram = magnitude of cross product of vectors a and b i.e |axb|
And we know a X b = (y1*z2 – y2*z1)*i – (x1*z2 – x2*z1)*j + (x1*y2 – x2*y1)*k
Then area =
C++
#include<bits/stdc++.h>
using namespace std ;
float area( float x1, float y1, float z1, float x2,
float y2, float z2)
{
float area = sqrt ( pow ((y1 * z2 - y2 * z1),2)
+ pow ((x1 * z2 - x2 * z1),2) +
pow ((x1 * y2 - x2 * y1),2));
return area;
}
int main()
{
float x1 = 3;
float y1 = 1;
float z1 = -2;
float x2 = 1;
float y2 = -3;
float z2 = 4;
float a = area(x1, y1, z1, x2, y2, z2);
cout << "Area = " << a;
return 0;
}
|
Java
public class GFG {
static float area( float x1, float y1, float z1, float x2,
float y2, float z2)
{
float area =( float ) Math.sqrt(Math.pow((y1 * z2 - y2 * z1), 2 )
+ Math.pow((x1 * z2 - x2 * z1), 2 ) +
Math.pow((x1 * y2 - x2 * y1), 2 ));
return area;
}
public static void main (String args[]){
float x1 = 3 ;
float y1 = 1 ;
float z1 = - 2 ;
float x2 = 1 ;
float y2 = - 3 ;
float z2 = 4 ;
float a = area(x1, y1, z1, x2, y2, z2);
System.out.println( "Area = " + a) ;
}
}
|
Python
import math
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 )
return area
def main():
x1 = 3
y1 = 1
z1 = - 2
x2 = 1
y2 = - 3
z2 = 4
a = area(x1, y1, z1, x2, y2, z2)
print ( "Area = " , a)
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static float area( float x1, float y1,
float z1, float x2,
float y2, float z2)
{
float area = ( float ) Math.Sqrt(Math.Pow((y1 * z2 - y2 * z1), 2) +
Math.Pow((x1 * z2 - x2 * z1), 2) +
Math.Pow((x1 * y2 - x2 * y1), 2));
return area;
}
public static void Main ()
{
float x1 = 3;
float y1 = 1;
float z1 = -2;
float x2 = 1;
float y2 = -3;
float z2 = 4;
float a = area(x1, y1, z1, x2, y2, z2);
Console.Write( "Area = " + a) ;
}
}
|
PHP
<?php
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));
return $area ;
}
$x1 = 3; $y1 = 1; $z1 = -2;
$x2 = 1; $y2 = -3; $z2 = 4;
$a = area( $x1 , $y1 , $z1 ,
$x2 , $y2 , $z2 );
echo ( "Area = " );
echo ( $a );
?>
|
Javascript
<script>
function area(x1, y1, z1,
x2, y2, z2)
{
area = Math.sqrt(Math.pow((y1 * z2 - y2 * z1), 2) +
Math.pow((x1 * z2 - x2 * z1), 2) +
Math.pow((x1 * y2 - x2 * y1), 2));
return area;
}
let x1 = 3;
let y1 = 1;
let z1 = -2;
let x2 = 1;
let y2 = -3;
let z2 = 4;
a = area(x1, y1, z1,
x2, y2, z2);
document.write ( "Area = " );
document.write (a);
</script>
|
Output: Area = 17.320508075688775
Time Complexity: O(logn) because using inbuilt sqrt function
Auxiliary Space: O(1)
Last Updated :
27 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...