简体   繁体   English

给定三个点的坐标,如何确定所定义的三角形是等边,等腰还是斜角?

[英]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!')
  • Since you square the coordinate distance, you don't need abs . 由于你平方坐标距离,你不需要abs
  • Your second y coordinate for side3 is wrong: should be y1 )). side3的第二个y坐标是错误的:应该是y1 ))。
  • Your check for legal sides should include equality: sides of 2, 2, 4 give you straight line, but you classify it as an isosceles triangle. 您对合法双方的检查应该包括相等:2,2,4的边给你直线,但你把它归类为等腰三角形。
  • You can use the math package for a more readable square root. 您可以使用数学包获得更易读的平方根。
  • You can save a step or two by sorting the side lengths; 您可以通过对边长进行排序来节省一两步; this simplifies your comparisons. 这简化了您的比较。

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.

相关问题 给定等边三角形的 2D 角坐标的 3D 法向量 - 3D normal vector given 2D corner coordinates of an equilateral triangle 如何在python中找到等腰三角形的第三个顶点 - How to find the third vertex of an isosceles triangle in python 给定一些约束,对等腰三角形的两个剩余顶点进行采样 - Sample two remaining vertices of isosceles triangle, given some constraints 找到三个最近的点,三角形包含球体上的给定点 - Find three closest points which triangle contains a given point on a sphere 给定(象限 i)中的三个(x,y)点组成一个直角三角形,找到三角形的方向 - Given three (x,y) points in (quadrant i) that make a right angle triangle, find triangle oriantation 多边形在 python 中划分为等腰三角形 - Polygon divided into isosceles triangle in python 打印直角和等腰三角形 - printing a right angle and isosceles triangle 在较大的等边三角形内创建等边三角形网格/网格 - Creating an equilateral triangle grid / mesh inside a larger equilateral triangle 如何检查笛卡尔坐标中的三个点在 python 中是否共线? - How to check three points in cartesian coordinates are collinear in python? 找到包含最多点的三角形的坐标 - Find the coordinates of the triangle containing the largest number of points
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM