Area of triangle using 3 sets of coordinates
Question:
I am doing one question where i need to find area of triangle with given 3 sets of coordinates
So what will be the logic to convert array to pair in (a1,b1) (a2,b2) (a3,b3) and how to find area of triangle using this vertices
Here is my code
def getTriangleArea(x, y):
///What will be the code
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
x_count = int(input().strip())
x = []
for _ in range(x_count):
x_item = int(input().strip())
x.append(x_item)
y_count = int(input().strip())
y = []
for _ in range(y_count):
y_item = int(input().strip())
y.append(y_item)
result = getTriangleArea(x, y)
fptr.write(str(result) + 'n')
fptr.close()
Answers:
Assuming that you have the coordinates of your 3 input points as:
x1, y1
x2, y2
x3, y3
You can use Pythagorean theorem to find the lengths of all sides:
l1 = sqrt((x1 - x2)**2 + (y1 - y2)**2)
l2 = sqrt((x2 - x3)**2 + (y2 - y3)**2)
l3 = sqrt((x3 - x1)**2 + (y3 - y1)**2)
and then use Heron’s Formula for the area of the triangle:
p = (l1 + l2 + l3)/2
area = sqrt(p * (p - l1) * (p - l2) * (p - l3))
Area of a triangle where (x1,y1)
is 1st co-ordinate, (x2,y2)
is 2nd co-ordinate, (x3,y3)
is 3rd co-ordinate.
Area = 1/2[x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)]
x = [2, 3, 7]
y = [4, -6, 8]
def get_area(x, y):
area = 0.5 * (x[0] * (y[1] - y[2]) + x[1] * (y[2] - y[0]) + x[2]
* (y[0] - y[1]))
return int(area)
coords = zip(x, y)
print('Area of points {}, {}, {} is {}'.format(*coords, get_area(x, y)))
output
Area of points (2, 4), (3, -6), (7, 8) is 27
I am doing one question where i need to find area of triangle with given 3 sets of coordinates
So what will be the logic to convert array to pair in (a1,b1) (a2,b2) (a3,b3) and how to find area of triangle using this vertices
Here is my code
def getTriangleArea(x, y):
///What will be the code
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
x_count = int(input().strip())
x = []
for _ in range(x_count):
x_item = int(input().strip())
x.append(x_item)
y_count = int(input().strip())
y = []
for _ in range(y_count):
y_item = int(input().strip())
y.append(y_item)
result = getTriangleArea(x, y)
fptr.write(str(result) + 'n')
fptr.close()
Assuming that you have the coordinates of your 3 input points as:
x1, y1
x2, y2
x3, y3
You can use Pythagorean theorem to find the lengths of all sides:
l1 = sqrt((x1 - x2)**2 + (y1 - y2)**2)
l2 = sqrt((x2 - x3)**2 + (y2 - y3)**2)
l3 = sqrt((x3 - x1)**2 + (y3 - y1)**2)
and then use Heron’s Formula for the area of the triangle:
p = (l1 + l2 + l3)/2
area = sqrt(p * (p - l1) * (p - l2) * (p - l3))
Area of a triangle where (x1,y1)
is 1st co-ordinate, (x2,y2)
is 2nd co-ordinate, (x3,y3)
is 3rd co-ordinate.
Area = 1/2[x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)]
x = [2, 3, 7]
y = [4, -6, 8]
def get_area(x, y):
area = 0.5 * (x[0] * (y[1] - y[2]) + x[1] * (y[2] - y[0]) + x[2]
* (y[0] - y[1]))
return int(area)
coords = zip(x, y)
print('Area of points {}, {}, {} is {}'.format(*coords, get_area(x, y)))
output
Area of points (2, 4), (3, -6), (7, 8) is 27