0% found this document useful (0 votes)
13 views4 pages

Prac 4

The document contains Python code that calculates the area and perimeter of a triangle defined by given coordinates. It also includes functions to determine the midpoint of a line segment and to check if a triangle is a right-angled triangle. Additionally, it uses matplotlib to visualize the triangle and its properties.

Uploaded by

itzsahil18005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Prac 4

The document contains Python code that calculates the area and perimeter of a triangle defined by given coordinates. It also includes functions to determine the midpoint of a line segment and to check if a triangle is a right-angled triangle. Additionally, it uses matplotlib to visualize the triangle and its properties.

Uploaded by

itzsahil18005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

In [1]: 1 from sympy import*

In [3]: 1 x=[2,3,7]
2 y=[4,-6,8]
3 def get_area(X,y):
4 area=0.5*(x[0]*(y[1]-y[2])+x[1]*(y[2]-y[0])+x[2]*(y[0]-y[1]))
5 return int(area)
6 coordinates=zip(x,y)
7 print(f"area of triangle ABC is {get_area(x,y)}sq.unit")

area of triangle ABC is 27sq.unit

In [4]: 1 import math


2 a=(2,4)
3 b=(3,-6)
4 c=(7,8)
5 def distance(p1,p2):
6 return math.sqrt((p2[0]-p1[0])**2+(p2[1]-p1[1])**2)
7 ab=distance(a,b)
8 bc=distance(b,c)
9 ca=distance(c,a)
10 perimeter=ab+bc+ca
11 print(f"the perimeter of triangle is :{perimeter}")

the perimeter of triangle is :31.013219637114773

In [6]: 1 p1,p2=Point(0,0),Point(10,10)
2 s1=Segment(p1,p2)
3 s1.midpoint

Out[6]: 𝑃𝑜𝑖𝑛𝑡2𝐷(5,5)
In [11]: 1 import matplotlib.pyplot as plt
2 x1,y1=0,0
3 x2,y2=10,10
4 midpoint=((x1+x2)/2,(y1+y2)/2)
5 plt.figure(figsize=(6,6))
6 plt.plot([x1,x2],[y1,y2],label='Line segment',color='blue')
7 plt.scatter(midpoint[0],midpoint[1],color='red',label='midpoint(5,5)')
8 plt.text(midpoint[0]+0.2,midpoint[1]+0.2,f'Midpoint{midpoint}',color='red'
9 plt.axis('equal')
10 plt.grid(True)
11 plt.legend()
12 plt.show()
In [15]: 1 import math
2 import matplotlib.pyplot as plt
3 def distance (p1,p2):
4 return math.sqrt((p2[0]-p1[0])**2 + (p2[1]-p1[1])**2)
5 def is_right_angle_triangle(a,b,c):
6 sides=sorted([a,b,c])
7 return math.isclose(sides[0]**2+sides[1]**2,sides[2]**2)
8 A=(0,0)
9 B=(4,0)
10 C=(4,3)
11 AB=distance(A,B)
12 BC=distance(B,C)
13 CA=distance(C,A)
14 if is_right_angle_triangle(AB,BC,CA):
15 print("the triangle is right angled triangle")
16 else:
17 print("the tringle is not right angled triangle")
18 plt.figure(figsize=(6,6))
19 plt.plot([A[0],B[0]],[A[1],B[1]],label=f"AB:{AB:.2f}",color="blue")
20 plt.plot([B[0],C[0]],[B[1],C[1]],label=f"BC:{BC:.2f}",color="red")
21 plt.plot([C[0],A[0]],[C[1],A[1]],label=f"CA:{CA:.2f}",color="green")
22 plt.scatter([A[0],B[0],C[0]],[A[1],B[1],c[1]],color="black")
23 plt.text(A[0],A[1],'A(0,0)',fontsize=12,verticalalignment='bottom',horizon
24 plt.text(B[0],B[1],'B(3,4)',fontsize=12,verticalalignment='bottom',horizon
25 plt.text(C[0],C[1],'C(0,0)',fontsize=12,verticalalignment='top',horizontal
26 plt.xlim(-1,7)
27 plt.ylim(-1,5)
28 plt.title("Triangle and Right-Angle check")
29 plt.xlabel("X-axis")
30 plt.ylabel("Y-axis")
31 plt.legend()
32 plt.show()

the triangle is right angled triangle


In [ ]: 1 ​

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy