我目前正在学习如何用Python编码,我遇到了这个任务,我正在try 解决它.但我想我在某个地方犯了一个错误,我想知道也许有人可以帮助我解决这个问题.任务是写一个代码,它将: 从文本文件导入四个2D坐标(A、B、C和X) 判断A、B、C是否可以是矩形的点 判断X是否在ABC矩形内 计算矩形的对角线.

到目前为止我有这个:

import math

def distance(point1, point2):
    return math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)

def is_rectangle(point1, point2, point3):
    distances = [
        distance(point1, point2),
        distance(point2, point3),
        distance(point3, point1)
    ]

    distances.sort()

    if distances[0] == distances[1] and distances[1] != distances[2]:
        return True
    else:
        return False
def is_inside_rectangle(rectangle, point):
    x_values = [vertex[0] for vertex in rectangle]
    y_values = [vertex[1] for vertex in rectangle]

    if (min(x_values) < point[0] < max(x_values)) and (min(y_values) < point[1] < max(y_values)):
        return True
    else:
        return False

with open('coordinates.txt', 'r') as file:
    coordinates = []
    for line in file:
        x, y = map(int, line.strip()[1:-1].split(','))
        coordinates.append((x, y))

rectangle = [coordinates[0], coordinates[1], coordinates[2]]
diagonal1 = distance(coordinates[0], coordinates[2])
diagonal2 = distance(coordinates[1], coordinates[3])
if is_rectangle(coordinates[0], coordinates[1], coordinates[2]) and is_inside_rectangle(rectangle, coordinates[3]):
    print("True")
    print(f"Diagonal of the rectangle is: {max(diagonal1, diagonal2)}")

else:
    print("False")

该代码有效,但我认为它计算对角线错误.例如,让我们将此点作为输入:A(0,0)、B(5,0)、C(0,5)和X(2,2).它说它们可以是矩形的点,对角线是5.当我把这些点写在纸上时,第四个点可以是D(5,5),然后对角线是7.07(50的平方根).或者它可以是D(-5,5),但它是一个对角线,一个对角线是5,但它不是最大值. 此外,我正在try 编写一个函数,该函数将判断文本文件中的所有数据是否都是integer.假设B是(m,k),那么它应该返回假,如果所有数据都是整数,那么继续执行代码.对此有什么 idea 吗?

推荐答案

让我们看看is_rectangle函数的逻辑.

def is_rectangle(point1, point2, point3):
    distances = [
        distance(point1, point2),
        distance(point2, point3),
        distance(point3, point1)
    ]

    distances.sort()

    if distances[0] == distances[1] and distances[1] != distances[2]:
        return True
    else:
        return False

您正在测试三角形ABC的两条最小边是否相等,并且与最大边不同.

有效地回答了这个问题:"ABC是obtuse isosceles三角形吗?"

但你应该问的问题是:"ABC是right三角形吗?"

有多种方法可以测试三角形ABC是否是直角三角形.

  • 一种方法是判断三对载体(AB,AC)、(BA,BC)、(CA,CB)中的一对是否是垂直的,即其点积是否为0.
  • 另一种方法是判断三个距离AB、AC、BC是否构成毕达哥拉斯三重组,即当x,y,z = sorted(map(distances,itertools.combinations(points,2)))z**2 == x**2 + y**2是否为真

PS:一般写if condition: return True else: return False的时候,还不如直接写return condition.

考虑到这一点,您用Python为此函数编写的代码相当于:

from itertools import combinations

def is_acute_isosceles_triangle(point1, point2, point3):
    distances = sorted(map(distance, combinations((point1,point2,point3), 2)))
    return (distances[0] == distances[1] and distances[1] != distances[2])

Python相关问答推荐

根据网格和相机参数渲染深度

如何通过多2多字段过滤查询集

将jit与numpy linSpace函数一起使用时出错

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

如何让剧作家等待Python中出现特定cookie(然后返回它)?

用合并列替换现有列并重命名

切片包括面具的第一个实例在内的眼镜的最佳方法是什么?

如何获取numpy数组的特定索引值?

我对我应该做什么以及我如何做感到困惑'

使用groupby方法移除公共子字符串

为什么if2/if3会提供两种不同的输出?

Geopandas未返回正确的缓冲区(单位:米)

基于多个数组的多个条件将值添加到numpy数组

统计numpy. ndarray中的项目列表出现次数的最快方法

BeautifulSoup-Screper有时运行得很好,很健壮--但有时它失败了::可能这里需要一些更多的异常处理?

仅使用预先计算的排序获取排序元素

如何在信号的FFT中获得正确的频率幅值

Django抛出重复的键值违反唯一约束错误

Matplotlib中的曲线箭头样式

根据过滤后的牛郎星图表中的数据计算新系列