Проблема заключается в том, как представить отношения инциденции (например, точку, лежащую как на луче, так и на линии) без дублирования данных или создания неудобной иерархии владения.
На данный момент объекты вложены: Line содержит Point и Ray, и Ray также содержит Point. Это приводит к дублированию, когда точка принадлежит и линии, и лучу.
Вот минимальный пример:
Код: Выделить всё
"""
Title: Geometric incidence question
Author: Jasper
Date: 2026 - 03-25
Description: This script was written to demonstrate a roadblock
that I am running into with regard to where to store
geometric objects which belong to multiple parent objects
(i.e., they are coincident).
This is a very bare-bones script, and a lot of functionality
is deliberately removed for minimality.
The purpose of this question is to learn what kind of data
structure I could develop which could achieve this task.
Specifically, my goal is to not have to have - for instance -
a point belonging to a ray, as well as to a line to which that
ray belongs. I want the setup to be minimal, and non-repetitive.
Current Output:
Line: L
Line > Point: P
Line > Ray: R
Line > Ray > Point: P
"""
class Line:
"""
A Euclidean line.
"""
def __init__(self, name):
"""
Initializes a line; see comments.
Parameter - name (str): The name of the line.
"""
# the name of the line
self.name = name
# our line may contain any of the following objects:
self.points = dict()
self.rays = dict()
def __str__(self):
"""
Prints the line and its objects.
"""
# name of the line
string = f"Line: {self.name}\n"
# name of the line's points
for point in self.points:
string += f"Line > Point: {point}\n"
# name of the line's rays
for ray in self.rays:
string += f"Line > Ray: {ray}\n"
for point in self.rays[ray].points:
string += f"Line > Ray > Point: {point}\n"
# returns the final string
return string
class Point:
"""
A synthetic point. It has a name, but since it is synthetic,
is has NO COORDINATES(!).
"""
def __init__(self, name):
"""
Initializes a point.
Parameter - name (str): The name of the point.
"""
# the name of the point
self.name = name
class Ray:
"""
A ray (on a line).
"""
def __init__(self, name):
"""
Initializes a ray.
Parameter - name (str): The name of the ray.
"""
# the name of the ray
self.name = name
# obviously, a ray can contain points
self.points = dict()
# a ray can also contain other rays
self.rays = dict()
# Suppose we have a line, L
L = Line("L")
# We also have a point, P
P = Point("P")
# And a ray, R
R = Ray("R")
# Suppose that P coincides with both L and R
L.points[P.name] = P
R.points[P.name] = P
# And suppose that R coincides with L
L.rays[R.name] = R
if __name__ == "__main__":
print(L)
В более общем плане:
- Точка может принадлежать нескольким объектам (линиям, лучам и т. д.)
- Луч принадлежит линии, но также имеет свою собственную структуру
- Я хочу избежать избыточного хранения
Какая структура данных или шаблон проектирования здесь подойдет?
Будем очень благодарны за любые рекомендации и примеры.
Мобильная версия