给三个点的坐标,求三角形面积,然后在求两个坐标的程序上基础g写了代码,进行不下...
mport mathclass point():
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def getx(self):
return self.x
def gety(self):
return self.y
class line1():
def __init__(self,p1,p2):
self.x=p1.getx()-p2.getx()
self.y=p1.gety()-p2.gety()
self.len1=math.sqrt(self.x*self.x+self.y*self.y)
def getlen1(self):
return self.len
class p3(point):
def __init__(self,p3):
self.x=(p1.getx()-p2.getx())/2
self.y=(p1.gety()-p2.gety())/2
return self.p3
class line2():
def __init__(self,p4,p3):
self.x=p3.getx()-p4.getx()
self.y=p3.gety()-p4.gety()
self.len2=math.sqrt(self.x*self.x+self.y*self.y)
def getlen2(self):
return self.len
class dg():
def __init__(self,bb=0,gao=0):
self.bb=bb
self.gao=gao
def getdj(self): 本帖最后由 ZhKQYu 于 2022-7-28 15:52 编辑
import math
# 定义一个求三角形面积的类
class Triangle:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def getArea(self):
p = (self.a + self.b + self.c) / 2
return (p * (p - self.a) * (p - self.b) * (p - self.c)) ** 0.5 # 海伦公式
# 定义一个获取坐标的类
class GetAxis:
def __init__(self):
self.axis = []
self.count = 0
while self.count < 3:
axis= eval(input("请输入第" + str(self.count + 1) + "个坐标(用元组表示坐标(1, 2)) : "))
self.axis.append(axis)
self.count += 1
# 获取每个坐标之间的距离
def getDistance(self):
distance = []
for i in range(len(self.axis)-1):
for j in range(i+1, len(self.axis)):
distance.append(math.sqrt((self.axis - self.axis) ** 2 + (self.axis - self.axis) ** 2))
return distance
axis = GetAxis()
area = Triangle(*axis.getDistance())
print("三角形面积为 : ", area.getArea()) class Point: # 点的坐标
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self): # 打印点
return f"({self.x}, {self.y})"
class Area: # 三角形面积
def __init__(self, a: Point, b: Point, c: Point):
self.__a = a
self.__b = b
self.__c = c
self.area = .5 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y))
class Length: # 两点之间长度
def __init__(self, a: Point, b: Point):
self.__a = a
self.__b = b
self.length = pow(pow(b.x - a.x, 2) + pow(b.y - a.y, 2), .5)
if __name__ == "__main__":
a = Point(-2, 1)
b = Point(3, 2)
c = Point(1, 5)
print(f"三个点分别是 a:{a}, b:{b}, c:{c}")
print(f"三个点的面积:{Area(a, b, c).area}")
print(f"ab 长度:{Length(a, b).length}")
print(f"bc 长度:{Length(b, c).length}")
print(f"ac 长度:{Length(a, c).length}")三个点分别是 a:(-2, 1), b:(3, 2), c:(1, 5)
三个点的面积:8.5
ab 长度:5.0990195135927845
bc 长度:3.605551275463989
ac 长度:5.0 建议通过向量来算, 获取ab向量, ac向量, 然后算sin, 就可以算面积了
页:
[1]