Beispiel #1
0
 def __init__(self, width: float, height: float):
     super(Rectangle, self).__init__()
     self.__position = Point2D(0, 0)
     self.__rotation_in_radians = 0
     self.__center = Point2D(0, 0)
     self.width = width
     self.height = height
Beispiel #2
0
    def points(self) -> List[Point2D]:
        p1 = self.__position + Point2D(-self.width / 2, -self.height / 2)
        p2 = self.__position + Point2D(self.width / 2, -self.height / 2)
        p3 = self.__position + Point2D(self.width / 2, self.height / 2)
        p4 = self.__position + Point2D(-self.width / 2, self.height / 2)

        return [
            p1.rotate(self.__position, self.__rotation_in_radians),
            p2.rotate(self.__position, self.__rotation_in_radians),
            p3.rotate(self.__position, self.__rotation_in_radians),
            p4.rotate(self.__position, self.__rotation_in_radians)
        ]
Beispiel #3
0
    def __init__(self, track, width: float, length: float, center: Point2D,
                 tipo: int, n, x, ponderacio):
        self.__ponderacio = ponderacio
        self.__track = track
        self.__bounds = Rectangle(width, length)
        self.__bounds.center = center
        self.speed_in_meters_per_sec = 20
        #self.speed_in_meters_per_sec = 0.0001

        self.__tipo = tipo  # 0 si el duc jo, 1 si el du sa xarxa

        self.__collision_time = 0
        self.steer = 0
        self.__collision = False
        angles = [45, 65, 75, 85, 90, 95, 105, 115, 135]
        angle_ini = 10
        num_angles = self.get_num_angles()
        # se generan los angulos para hallar las colisiones, angulo de orientacion de los sensores
        # 90 es dirección que circula el coche
        angles = [
            angle_ini + (x * (180 - angle_ini * 2) / (num_angles - 1))
            for x in range(0, num_angles)
        ]
        self.__collision_distance = 150
        self.__vectors = [
            Point2D(
                math.sin(angle / 180 * math.pi) * self.__collision_distance,
                math.cos(angle / 180 * math.pi) * self.__collision_distance)
            for angle in angles
        ]
        self.__collision_points = [Point2D(0, 0) for v in self.__vectors]
        self.__collision_distances = [0] * len(self.__vectors)
        self.__current_segment = 0
        self.__distance = 0
        self.__total_segment_distance = 0
        #self._net = Network([Car.get_num_angles(), 25, 2])
        self._net = x
        self.__laps = 0
        self.__current_speed = 3
        self.number = 0

        self.__carTemps = 0

        #self.__guardarAngles=[]
        #self.__recorregut=[]
        #self.__angle=[]
        self.__teclat = 0
        self.__guardarTeclat = []

        self.__vel = 0
        self.__SensorCentral = [8] * 10
        self.__memoriaSensorCentral = []
Beispiel #4
0
def line_intersection(p1: Point2D, p2: Point2D, p3: Point2D, p4: Point2D):
    xdiff = Point2D(p1.x - p2.x, p3.x - p4.x)
    ydiff = Point2D(p1.y - p2.y, p3.y - p4.y)  #Typo was here

    def det(a, b):
        return a.x * b.y - a.y * b.x

    div = det(xdiff, ydiff)
    if div == 0:
        return None

    d = Point2D(det(p1, p2), det(p3, p4))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return Point2D(x, y)
Beispiel #5
0
    def __init__(self, n):

        self.__points = CarregarCircuit(n).points()
        widths = CarregarCircuit(n).widths()

        self._segments = []
        parray = []
        for i in range(0, len(self.__points)):
            p0 = self.__points[i]
            j = (i + 1) % len(self.__points)
            p1 = self.__points[j]
            k = (i + 2) % len(self.__points)
            p2 = self.__points[k]
            v0 = (p1 - p0).unitary()
            v1 = (p2 - p1).unitary()
            vf = (v0 + v1).unitary()
            vn = Point2D(vf.y, -vf.x)
            width = widths[i]
            pn1 = p1 + vn.mul_by_value(width)
            pn2 = p1 - vn.mul_by_value(width)
            parray.append((pn1, pn2))

        wall_width = 0.5
        for i in range(0, len(parray)):
            j = (i + 1) % len(parray)
            p0 = parray[i][0]
            p1 = parray[i][1]
            p2 = parray[j][0]
            p3 = parray[j][1]
            self._segments.append(Segment(p0, p1, p2, p3, wall_width))

        obstacles = CarregarObstacle(n).lista()
        for i in obstacles:

            self._segments[0].collisionRegions.append(Polygon(i))
Beispiel #6
0
def _furthest_point(direction: Point2D, points: List[Point2D]) -> Point2D:
    max_dot = 0.0
    furthest = None
    for point in points:
        current_dot = direction.dot_product(point)
        if current_dot > max_dot or furthest is None:
            max_dot = current_dot
            furthest = point
    return furthest
Beispiel #7
0
    def __init__(self, track, width: float, length: float, center: Point2D,
                 tipo: int, n):
        self.__track = track
        self.__bounds = Rectangle(width, length)
        self.__bounds.center = center
        self.speed_in_meters_per_sec = 20

        self.__tipo = tipo  # 0 si el duc jo, 1 si el du sa xarxa

        self.steer = 0
        self.__collision = False
        angles = [45, 65, 75, 85, 90, 95, 105, 115, 135]
        angle_ini = 10
        num_angles = self.get_num_angles()

        angles = [
            angle_ini + (x * (180 - angle_ini * 2) / (num_angles - 1))
            for x in range(0, num_angles)
        ]
        self.__collision_distance = 150
        self.__vectors = [
            Point2D(
                math.sin(angle / 180 * math.pi) * self.__collision_distance,
                math.cos(angle / 180 * math.pi) * self.__collision_distance)
            for angle in angles
        ]
        self.__collision_points = [Point2D(0, 0) for v in self.__vectors]
        self.__collision_distances = [0] * len(self.__vectors)
        self.__current_segment = 0
        self.__distance = 0
        self.__total_segment_distance = 0
        #self._net = Network([CarUsuari.get_num_angles(), 25, 2])
        #self._net = NetworkAmbSensors([CarUsuari.get_num_angles(), 25, 2])
        self._net = n
        self.__laps = 0
        self.__current_speed = 5
        self.number = 0

        self.__teclat = 0
        self.__guardarTeclat = []

        self.__tempsCar = 0
        self.__SensorCentral = [8] * 10
        self.__memoriaSensorCentral = []
Beispiel #8
0
def are_colliding(shape1: Shape, shape2: Shape) -> bool:
    if not shape1.overlaps(shape2):
        return False
    direction = Point2D(1, 1)
    c = _support(direction, shape1, shape2)
    simplex = [c]
    direction = -c
    origin = Point2D(0, 0)
    step = 1
    while True:
        b = _support(direction, shape1, shape2)
        if b.dot_product(direction) < 0:
            return False
        simplex.append(b)
        if len(simplex) == 3:
            if Triangle(simplex[0], simplex[1], simplex[2]).contains(origin):
                return True
        simplex, direction = _do_simplex(simplex)
        step += 1
        if step > 1000:
            return False
Beispiel #9
0
    def colocar(self, x, y, angle, vel):

        self.__bounds.position = Point2D(x, y)
        self.__bounds.rotation_in_radians = angle
        #print(self.__current_speed)
        if vel == 1:
            self.__current_speed = self.__current_speed + 5 * 0.03
        else:
            if vel == 0:
                self.__current_speed = self.__current_speed - 5 * 0.03

        self.compute_collision_points()
Beispiel #10
0
    def simulate(self, elapsed_time: float):
        # calculamos la nueva posicion
        cos = math.cos(self.__bounds.rotation_in_radians)
        sin = math.sin(self.__bounds.rotation_in_radians)
        mov = Point2D(cos * elapsed_time * self.current_speed,
                      sin * elapsed_time * self.current_speed)

        self.__bounds.position = self.__bounds.position + mov
        #nou
        #self.__guardarAngles.append(self.__bounds.rotation_in_radians)
        #self.__recorregut.append(self.__bounds.position)

        # calculamos los puntos de colision
        self.compute_collision_points()
Beispiel #11
0
    def advanced(self, point: Point2D) -> Point2D:
        projection_point = line_intersection(self.p1, self.p2, self.p3, self.p4)

        if projection_point is None:

            width_ini = (self.p2 - self.p1).length()
            width_end = (self.p4 - self.p3).length()

            width_max = max(width_ini, width_end)

            v = self._direction.unitary()
            v = Point2D(v.y, -v.x).mul_by_value(width_max * 2)
            point_a = point + v
            point_b = point - v

            intersection = segment_intersection(self._point_ini, self._point_end, point_a, point_b)
            return intersection
        else:
            # no son paraleles
            return line_intersection(projection_point, point, self._point_ini, self._point_end)
Beispiel #12
0
    def __init__(self, n):
        self.__lista = []

        if n == 8:
            self.__lista.append([
                Point2D(36, 23),
                Point2D(30, 23),
                Point2D(33, 19),
                Point2D(36, 19)
            ])
            self.__lista.append([
                Point2D(60, 20),
                Point2D(66, 20),
                Point2D(66, 26),
                Point2D(60, 26)
            ])
            self.__lista.append(
                [Point2D(127, 60),
                 Point2D(129, 53),
                 Point2D(131, 56)])
            self.__lista.append([
                Point2D(60, 100),
                Point2D(64, 100),
                Point2D(64, 103),
                Point2D(62, 103)
            ])
            self.__lista.append([
                Point2D(60, 43),
                Point2D(64, 44),
                Point2D(64, 45),
                Point2D(60, 51)
            ])
            self.__lista.append(
                [Point2D(82, 106),
                 Point2D(96, 104),
                 Point2D(93, 108)])

        if n == 10:
            self.__lista.append(
                [Point2D(38, 102),
                 Point2D(44, 106),
                 Point2D(64, 103)])
            self.__lista.append(
                [Point2D(72, 75),
                 Point2D(79, 75),
                 Point2D(76, 70)])
            self.__lista.append([
                Point2D(106, 20),
                Point2D(110, 18),
                Point2D(112, 23),
                Point2D(109, 27),
                Point2D(105, 26)
            ])

        if n == 3:
            self.__lista.append(
                [Point2D(70, 122),
                 Point2D(85, 120),
                 Point2D(70, 115)])
            self.__lista.append(
                [Point2D(70, 103),
                 Point2D(85, 103),
                 Point2D(70, 110)])

        if n == 12:
            self.__lista.append([
                Point2D(70, -9),
                Point2D(50, -1),
                Point2D(40, -7),
                Point2D(40, -9),
                Point2D(55, -12)
            ])

            self.__lista.append([
                Point2D(70, -18),
                Point2D(55, -27),
                Point2D(40, -18),
                Point2D(55, -16)
            ])
            self.__lista.append([
                Point2D(30, -23),
                Point2D(36, -30),
                Point2D(46, -28),
                Point2D(40, -24),
                Point2D(35, -20)
            ])
            self.__lista.append(
                [Point2D(29, 79),
                 Point2D(36, 83),
                 Point2D(50, 79)])
            self.__lista.append(
                [Point2D(32, 75),
                 Point2D(36, 71),
                 Point2D(50, 75)])
            self.__lista.append([
                Point2D(30, 95),
                Point2D(36, 91),
                Point2D(40, 95),
                Point2D(36, 100)
            ])
            self.__lista.append([
                Point2D(45, 105),
                Point2D(66, 115),
                Point2D(80, 116),
                Point2D(90, 105),
                Point2D(66, 101)
            ])

        if n == 15:
            self.__lista.append(
                [Point2D(56, 47),
                 Point2D(72, 44),
                 Point2D(55, 57)])
            self.__lista.append([
                Point2D(120, 85),
                Point2D(127, 90),
                Point2D(129, 77),
                Point2D(120, 78)
            ])
Beispiel #13
0
def _triple_product_expansion(v1: Point2D, v2: Point2D,
                              v3: Point2D) -> Point2D:
    return v2.mul_by_value(v1.dot_product(v3)) - v3.mul_by_value(
        v1.dot_product(v2))
Beispiel #14
0
 def maxPoint(self) -> Point2D:
     if self._maxPoint is None:
         ps = self.points
         self._maxPoint = Point2D(max([p.x for p in ps]),
                                  max([p.y for p in ps]))
     return self._maxPoint
Beispiel #15
0
    def compute_collision_points(self):
        '''
        Compute collision points, like the car have distance sensors, and detect the points where will collide
        :return: no returns
        '''

        rot = self.bounds.rotation_in_radians
        self.__collision_points = []
        self.__collision_distances = []
        for vector in self.vectors:
            # para cada sensor calculamos un segmento (p0-p1) que representa el haz para detectar un obstáculo
            vector = vector.rotate(Point2D(0, 0), rot)
            p0 = Point2D(self.bounds.position.x, self.bounds.position.y)
            p1 = Point2D(self.bounds.position.x + vector.x,
                         self.bounds.position.y + vector.y)

            # inicialmente no ha detectado nada
            min_distance = self.__collision_distance + 1
            min_point = None

            # para cada segmento
            # num = 5
            # num_segments = len(self.__track.segments)
            # seg = (self.__current_segment - num + num_segments) % num_segments
            # seg_fin = (self.__current_segment + num + num_segments + 1) % num_segments
            for s in self.__track.segments:
                #while seg != seg_fin:
                # para cada region de colision
                # s = self.__track.segments[seg]
                # seg = (seg + 1) % num_segments

                for cr in s.collisionRegions:
                    points = cr.points
                    numPoints = len(points)
                    for x in range(0, numPoints):
                        cr1 = points[x]
                        cr2 = points[(x + 1) % numPoints]
                        intersection = segment_intersection(p0, p1, cr1, cr2)
                        if intersection is not None:
                            distance = (intersection -
                                        self.bounds.position).length()
                            if distance < min_distance:
                                min_distance = distance
                                min_point = intersection
                '''
                # de todas las colisiones nos quedamos con la más cercana
                #  buscamos si colisiona con el borde izquierdo
                intersection = segment_intersection(p0, p1, s.p1, s.p3)
                if intersection is not None:
                    distance = (intersection - self.bounds.position).length()
                    if distance < min_distance:
                        min_distance = distance
                        min_point = intersection
                # buscamos si colisiona con el borde derecho
                intersection = segment_intersection(p0, p1, s.p2, s.p4)
                if intersection is not None:
                    distance = (intersection - self.bounds.position).length()
                    if distance < min_distance:
                        min_distance = distance
                        min_point = intersection
                '''

            if min_point is not None:
                # Ha habido colision
                self.__collision_points.append(min_point)
                self.__collision_distances.append(min_distance)
            else:
                # No ha habido colision, nos quedamos con el punto final del segmento
                self.__collision_points.append(p1)
                self.__collision_distances.append(min_distance)
Beispiel #16
0
 def llistes(self, x, y, w):
     for i in range(len(x)):
         self.__points.append(Point2D(float(x[i]), float(y[i])))
         self.__widths.append(float(w[i]))
     return self.__points, self.__widths
Beispiel #17
0
 def __init__(self, points: List[Point2D]):
     super(Polygon, self).__init__()
     self._points = []
     for p in points:
         self._points.append(Point2D(round(p.x, 2), round(p.y, 2)))