1+ # area of trianlgle whose three coordinates are given by user
2+ x1 = float (input ("Enter x-coordinate of first point: " ))
3+ y1 = float (input ("Enter y-coordinate of first point: " ))
4+ x2 = float (input ("Enter x-coordinate of second point: " ))
5+ y2 = float (input ("Enter y-coordinate of second point: " ))
6+ x3 = float (input ("Enter x-coordinate of third point: " ))
7+ y3 = float (input ("Enter y-coordinate of third point: " ))
8+
9+ def area_of_triangle (x1 , y1 , x2 , y2 , x3 , y3 ):
10+ area = abs ((x1 * (y2 - y3 ) + x2 * (y3 - y1 ) + x3 * (y1 - y2 ))/ 2 )
11+ return area
12+ area = area_of_triangle (x1 , y1 , x2 , y2 , x3 , y3 )
13+ print (f"The area of the triangle formed by the points ({ x1 } , { y1 } ), ({ x2 } , { y2 } ), and ({ x3 } , { y3 } ) is: { area } square units." )
14+ # plotting the triangle
15+ import matplotlib .pyplot as plt
16+ plt .plot ([x1 , x2 , x3 , x1 ], [y1 , y2 , y3 , y1 ], 'b-' )
17+ plt .scatter ([x1 , x2 , x3 ], [y1 , y2 , y3 ], color = 'red' )
18+ plt .title ("Triangle formed by the given points" )
19+ plt .xlabel ("X-axis" )
20+ plt .ylabel ("Y-axis" )
21+ plt .grid (True )
22+ plt .show ()
23+ #saving the result to a text file
24+ with open ("triangle_area.txt" , "w" ) as file :
25+ file .write (f"The area of the triangle formed by the points ({ x1 } , { y1 } ), ({ x2 } , { y2 } ), and ({ x3 } , { y3 } ) is: { area } square units.\n " )
26+ file .write ("The triangle has been plotted and displayed.\n " )
27+ #saving the figure
28+ plt .savefig ('triangle_plot.png' )
0 commit comments