Instrumented version - to look at timings
Including: - replacing guassian with simpler triangle distribution - Timing calculations -resulting in using only 50 points. - Changes (which may need to be reconsidered) for optimization. These are likely to be simplified back.
This commit is contained in:
@@ -30,14 +30,14 @@ def point_is_inside_arena(x, y):
|
||||
return True
|
||||
|
||||
## intention - we can use a distance squared function to avoid the square root, and just square the distance sensor readings too.
|
||||
def get_ray_distance_to_segment_squared(ray, segment):
|
||||
def get_ray_distance_to_segment_squared(ray_x, ray_y, ray_tan, ray_heading, segment):
|
||||
"""Return the distance squared from the ray origin to the intersection point along the given ray heading.
|
||||
The segments are boundary lines, which will be horizontal or vertical, and have known lengths.
|
||||
The ray can have any heading, and will be infinite in length.
|
||||
Ray -> (x, y, heading)
|
||||
ray_tan -> tangent of the heading (optimization)
|
||||
Segment -> ((x1, y1), (x2, y2))
|
||||
"""
|
||||
ray_x, ray_y, ray_heading = ray
|
||||
segment_x1, segment_y1 = segment[0]
|
||||
segment_x2, segment_y2 = segment[1]
|
||||
# if the segment is horizontal, the ray will intersect it at a known y value
|
||||
@@ -46,7 +46,7 @@ def get_ray_distance_to_segment_squared(ray, segment):
|
||||
if ray_heading == 0:
|
||||
return None
|
||||
# calculate the x value of the intersection point
|
||||
intersection_x = ray_x + (segment_y1 - ray_y) / math.tan(ray_heading)
|
||||
intersection_x = ray_x + (segment_y1 - ray_y) / ray_tan
|
||||
# is the intersection point on the segment?
|
||||
if intersection_x > max(segment_x1, segment_x2) or intersection_x < min(segment_x1, segment_x2):
|
||||
return None
|
||||
@@ -58,7 +58,7 @@ def get_ray_distance_to_segment_squared(ray, segment):
|
||||
if ray_heading == math.pi / 2:
|
||||
return None
|
||||
# calculate the y value of the intersection point
|
||||
intersection_y = ray_y + (segment_x1 - ray_x) * math.tan(ray_heading)
|
||||
intersection_y = ray_y + (segment_x1 - ray_x) * ray_tan
|
||||
# is the intersection point on the segment?
|
||||
if intersection_y > max(segment_y1, segment_y2) or intersection_y < min(segment_y1, segment_y2):
|
||||
return None
|
||||
@@ -75,8 +75,10 @@ def get_ray_distance_squared_to_nearest_boundary_segment(ray):
|
||||
"""
|
||||
# find the distance to each segment
|
||||
distances = []
|
||||
ray_x, ray_y, ray_heading = ray
|
||||
ray_tan = math.tan(ray_heading)
|
||||
for segment in boundary_lines:
|
||||
distance_squared = get_ray_distance_to_segment_squared(ray, segment)
|
||||
distance_squared = get_ray_distance_to_segment_squared(ray_x, ray_y, ray_tan, ray_heading, segment)
|
||||
if distance_squared is not None:
|
||||
distances.append(distance_squared)
|
||||
# return the minimum distance
|
||||
|
||||
Reference in New Issue
Block a user