[英]Given the coordinates of three points, how to determine if the triangle defined is equilateral, isosceles or scalene?
I've written the following code, but I can't make sure if it recognizes an equilateral triangle because I can't input the coordinates I need to make it (square root of 3, for example): 我写了下面的代码,但是我无法确定它是否识别出等边三角形,因为我无法输入我需要的坐标(例如3的平方根):
x1 = eval(input('x1: '))
y1 = eval(input('y1: '))
x2 = eval(input('x2: '))
y2 = eval(input('y2: '))
x3 = eval(input('x3: '))
y3 = eval(input('y3: '))
side1 = (abs(x1 - x2) + abs(y1 - y2)) ** (1/2)
side2 = (abs(x2 - x3) + abs(y2 - y3)) ** (1/2)
side3 = (abs(x3 - x1) + abs(y3 - y2)) ** (1/2)
print('side1: ', side1, 'side2: ', side2,'side3:', side3)
if side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2 :
if side1 == side2 == side3:
print('This triangle is equilateral')
elif side1 == side2 or side2 == side3 or side1 == side3 :
print('This triangle is isosceles')
else:
print('This triangle is scalene')
else:
print('This is not a triangle!')
EDIT: I've rewritten the code as follows 编辑:我已经重写了如下代码
x1 = eval(input('x1: '))
y1 = eval(input('y1: '))
x2 = eval(input('x2: '))
y2 = eval(input('y2: '))
x3 = eval(input('x3: '))
y3 = eval(input('y3: '))
side1 = ((x1 - x2)**2 + (y1 - y2)**2) ** (1/2)
side2 = ((x2 - x3)**2 + (y2 - y3)**2) ** (1/2)
side3 = ((x3 - x1)**2 + (y3 - y1)**2) ** (1/2)
print('side1: ', side1, 'side2: ', side2,'side3:', side3)
if side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2 :
if side1 == side2 == side3:
print('This triangle is equilateral')
elif side1 == side2 or side2 == side3 or side1 == side3 :
print('This triangle is isosceles')
else:
print('This triangle is scalene')
else:
print('This is not a triangle!')
Updated code: 更新的代码:
from math import sqrt
x1 = float(raw_input('x1: '))
y1 = float(raw_input('y1: '))
x2 = float(raw_input('x2: '))
y2 = float(raw_input('y2: '))
x3 = float(raw_input('x3: '))
y3 = float(raw_input('y3: '))
side1 = sqrt((x1 - x2)**2 + (y1-y2)**2)
side2 = sqrt((x2 - x3)**2 + (y2-y3)**2)
side3 = sqrt((x3 - x1)**2 + (y3-y1)**2)
# Put the sides into a list; sort them.
tri= [side1, side2, side3]
tri.sort()
if tri[0] < tri[1]+tri[2]:
if tri[0] == tri[2]:
print('This triangle is equilateral')
elif tri[1] == tri[2] or tri[1] == tri[0]:
print('This triangle is isosceles')
else:
print('This triangle is scalene')
else:
print('This is not a triangle!')
It sounds like the question is really about how to enter the input. 这听起来像是关于如何输入输入的问题。 You need to wrap the input in quotes because
eval
expects string input, not number types. 您需要将输入包装在引号中,因为
eval
需要字符串输入,而不是数字类型。
The eval
function will parse that string as Python code. eval
函数将该字符串解析为Python代码。 Thus, you use the same square root syntax as you do in your code. 因此,您使用与代码中相同的平方根语法。
So for example, let's test an equilateral triangle. 例如,让我们测试一个等边三角形。 I run your (edited) script from the terminal, and enter the coordinates as strings.
我从终端运行您的(编辑过的)脚本,并输入坐标作为字符串。
x1: '0'
y1: '0'
x2: '2'
y2: '12**0.5'
x3: '4'
y3: '0'
('side1: ', 1.0, 'side2: ', 1.0, 'side3:', 1.0)
This triangle is equilateral
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.