import math

from point2d import Point2D


def to_point(rads, dist):
    x = math.cos(rads) * dist
    y = math.sin(rads) * dist
    return Point2D(x, y)


class Slice(object):
    def __init__(self, begin, end):
        self.__begin = begin
        self.__end = end

        self.__begin_rad = math.radians(self.__begin)
        self.__end_rad = math.radians(self.__end)

        self.__begin_point = None
        self.__end_point = None

        # Calculate the angle halfway between the begin and end
        self.__mid_rad = math.radians(self.__begin + ((self.__end - self.__begin) / 2.0))
        self.__nearest_point = None

    @property
    def nearest(self):
        return self.__nearest_point

    def __contains__(self, point):
        return self.__begin <= point.angle <= self.__end

    def begin_point(self, max_dist):
        return to_point(self.__begin_rad, max_dist)

    def end_point(self, max_dist):
        return to_point(self.__end_rad, max_dist)

    def add_point(self, point):
        # See if point is closer than the previously closest point
        if point.origin_dist < self.__nearest_point.origin_dist:
            self.__nearest_point = point

    def reset(self, max_dist):
        self.__nearest_point = to_point(self.__mid_rad, max_dist)

    def __str__(self):
        return "Begin: {} End: {} Nearest: {}".format(self.__begin, self.__end, self.__nearest_point)

[((9, 11, 9, 24), 'point2d.Point2D', 'Point2D', ({(9, 19, 9, 20): 'x', (9, 22, 9, 23): 'y'}, {}), '(x, y)', False, 'from point2d import Point2D\n'), ((7, 8, 7, 22), 'math.cos', 'math.cos', ({(7, 17, 7, 21): 'rads'}, {}), '(rads)', False, 'import math\n'), ((8, 8, 8, 22), 'math.sin', 'math.sin', ({(8, 17, 8, 21): 'rads'}, {}), '(rads)', False, 'import math\n'), ((17, 27, 17, 53), 'math.radians', 'math.radians', ({(17, 40, 17, 52): 'self.__begin'}, {}), '(self.__begin)', False, 'import math\n'), ((18, 25, 18, 49), 'math.radians', 'math.radians', ({(18, 38, 18, 48): 'self.__end'}, {}), '(self.__end)', False, 'import math\n'), ((24, 25, 24, 89), 'math.radians', 'math.radians', ({(24, 38, 24, 88): 'self.__begin + (self.__end - self.__begin) / 2.0'}, {}), '(self.__begin + (self.__end - self.__begin) / 2.0)', False, 'import math\n')]