Search Your Question...!

Area of Triangle using Javascript

You are given 3 sets of coordinates that form a triangle. Write code to find the area of the triangle enclosed by those coordinates.

For example, the 3 points have coordinates given as x = [0, 3, 6] and y = [0, 3, 0], aligned by index, so the coordinates are [0,0], [3,3], [6,0]. The height of the triangle is 3, and the width is 6, so the area of the triangle is 6 * 3 / 2 = 9. All resulting areas will be whole numbers. Not all triangles will contain a right angle.

 


 

Function Description

 Complete the function getTriangleArea in the editor below. The function must return an integer which indicates the area of the triangle.

 getTriangleArea has the following parameter(s):

    x:  An integer array, that denotes the x coordinates.

    y:  An integer array, that denotes the y coordinates.

 Constraints

  • 1 ≤ x[i] < 100000
  • 1 ≤ y[i] ;< 100000

 

Input Format For Custom Testing

Input from stdin will be processed as follows and passed to the function:

 The first line gives the number of x coordinates (always 3).



   View Solution :-   

   






function getTriangleArea(x, y) { area = 0.5*( (x[0]*(y[1]-y[2])) + (x[1]*(y[2]-y[0])) + (x[2]*(y[0]-y[1])) ); return parseInt(Math.abs(area)); }

No comments:

Post a Comment