I have a function that takes a point, and given its velocity, and acceleration, calculates the time(s) for the point to collide with the line:
def ParabolaLineCollision(pos, vel, acc, line):
"""A pure function that returns all intersections."""
pass
I have another function that wants to calculate the smallest intersection time between two entities, where an entity has a velocity/acceleration, and a list of vertices.
The algorithm of entity_intersection creates lines for each objects' list of vertices, and then calls ParabolaLineCollision on each point of each object, with each line of the other object. My current algorithm runs slowly, and thus I would like to be able to parallelize the algorithm.
My problem is that entity_intersections
is a bit hard to read, and I think that there may be some code duplication, which might possibly be eliminated. Here's the algorithm, and
class Entity
# ...
@property
def lines(self):
"""
Get a list of lines that connect self.points.
If self.enclosed, then the last point will connect the first point.
"""
# type(self.enclosed) == bool
# # I included this, as this may be where there is logic duplication with entity_intersections.
# # It's also not very readable.
return [(self.points[i-1], self.points[i]) for i in range((1-int(self.enclosed)), len(self.points))]
# ...
def entity_intersections(entity, other):
"""
Find all intersection times between two entities.
type(entity) == type(other) == Entity
"""
# Find relative velocity/acceleration
a12 = entity.acceleration - other.acceleration
a21 = other.acceleration - entity.acceleration
v12 = entity.velocity - other.velocity
v21 = other.velocity - entity.velocity
entity_points, other_points = entity.points, other.points
entity_lines, other_lines = entity.lines, other.lines
results = []
# Get all intersections between each object's point and the other object's line
for point in entity_points:
for line in other_lines:
results.append((point, v12, a12, line))
for point in other_points:
for line in entity_lines:
results.append((point, v21, a21, line))
# Return results of ParabolaLineCollision with each list of arguments in results
# Pseudo-code to achieve this (I haven't done the parallizing yet):
# return min(map(ParabolaLineCollision, results))