我为Ray、Line、Point和其他一些形状编写了类,但我被一个函数的需要所阻止,该函数将Line类对象和Ray类对象作为参数,并返回直线和射线相交的点。
点类:
class Point:
def __init__(self, x: float, y: float):
self.x = x
self.y = y
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
def __iter__(self):
yield self.x
yield self.y
def __str__(self):
return f"x = {self.x}; y = {self.y}"
射线等级:
class Ray:
def __init__(self, start: Point, end: Point):
self.start = start
self.end = end
def get_direction(self):
x_diff = self.end[0] - self.start[0]
y_diff = self.end[1] - self.start[1]
return (x_diff, y_diff)
线路等级:
class Line:
def __init__(self, point1: Point, point2: Point):
self.point1 = point1
self.point2 = point2
def get_direction(self) -> tuple:
x_diff = abs(self.point2[0] - self.point1[0])
y_diff = abs(self.point2[1] - self.point1[1])
return (x_diff, y_diff)
def get_params(self) -> tuple:
x1, y1 = self.point1
x2, y2 = self.point2
a = y2 - y1
b = x1 - x2
c = x2 * y1 - x1 * y2
return a, b, c
def get_normal(self):
a, b, c = self.get_params()
return -b, a, c
以下函数可能会有所帮助:点在线上吗?
def point_on_line(point: Point, line: Line):
x1, y1 = line.point1
x2, y2 = line.point2
x, y = point
return (y - y1) * (x2 - x1) == (y2 - y1) * (x - x1)
点在射线上吗?
def point_on_ray(point: Point, ray: Ray) -> bool:
start = ray.start
end = ray.end
direction = ray.get_direction()
x_diff = point[0] - start[0]
y_diff = point[1] - start[1]
if x_diff * direction[1] == y_diff * direction[0]:
if x_diff >= 0 and y_diff >= 0:
return True
return False
我数学不好,所以我请ChatGPT来帮我,但是他写了一些函数,会返回错误的结果
我试着给ChatGPT几个完全不同的提示,得到了几个完全不同的函数作为响应,但无论如何,它们都不起作用。其中一些函数返回了错误的点,一些函数认为射线和直线是平行的,尽管它们没有正确地工作
1条答案
按热度按时间gblwokeq1#
你要解方程组
对于未知数
t
和u
,则检查t >= 0
然后将
t
值代入左边部分,得到交点坐标。独立Python函数