Exemplo n.º 1
0
 def crash(self, other):
     seg1_1 = Point(self.x_position, self.y_position)
     seg1_2 = Point(self.new_x_position, self.new_y_position)
     seg2_1 = Point(other.x_position, other.y_position)
     seg2_2 = Point(other.new_x_position, other.new_y_position)
     seg1 = Segment(seg1_1, seg1_2)
     seg2 = Segment(seg2_1, seg2_2)
     return len(seg1.intersection(seg2))
 def crash(
     self, other
 ):  # Функция для определения столкновения двух самолётов. Мы создаём для каждого самолёта по отрезку от старого положения до нового. Функция возвращает список точек пересечения двух отрезков. Если длина списка нулевая, пересечения нет. Если больше 0, значит пересечение есть и самолёты столкнулись.
     seg1_1 = Point(self.old_x, self.old_y)
     seg1_2 = Point(self.x_position, self.y_position)
     seg2_1 = Point(other.old_x, other.old_y)
     seg2_2 = Point(other.x_position, other.y_position)
     seg1 = Segment(seg1_1, seg1_2)
     seg2 = Segment(seg2_1, seg2_2)
     return len(seg1.intersection(seg2))
Exemplo n.º 3
0
def intersection_of_segments(p1_x, p1_y, p2_x, p2_y):
    """Координаты точки пересечения двух отрезков"""
    s1 = Segment(p1_y, p1_x)
    s2 = Segment(p2_y, p2_x)
    intersection = s1.intersection(s2)
    if len(intersection) != 0:
        intersection = intersection[0]
        return float(intersection.y), float(intersection.x)
    else:
        return 0, 0
Exemplo n.º 4
0
def intersection_of_segments(p1_x: Point, p1_y: Point, p2_x: Point,
                             p2_y: Point) -> tuple:
    """
    Координаты точки пересечения двух отрезков
    :param p1_x: Первая точка первого отрезка
    :param p1_y: Вторая точка первого отрезка
    :param p2_x: Первая точка второго отрезка
    :param p2_y: Вторая точка второго отрезка
    :return: Кортеж координат точки пересечения отрезков
    """
    s1 = Segment(p1_x, p1_y)
    s2 = Segment(p2_x, p2_y)
    intersection = s1.intersection(s2)
    if len(intersection) != 0:
        intersection = intersection[0]
        return float(intersection.x), float(intersection.y)
    else:
        return None, None
Exemplo n.º 5
0
    def random_segments(self, num_segments): # Generates random disjoint segments. Potentially horrible execution time due to randomness.
        p1 = Point(randint(1, self.plane_x-1), randint(1, self.plane_y-1))
        p2 = Point(randint(1, self.plane_x-1), randint(1, self.plane_y-1))
        self.segments = [Segment(p1, p2)]

        while len(self.segments) < num_segments:
            p1 = Point(randint(1, self.plane_x-1), randint(1, self.plane_y-1))
            p2 = Point(randint(1, self.plane_x-1), randint(1, self.plane_y-1))
            new_segment = Segment(p1, p2)

            # Verify that generated segment does not intersect with another
            intersection = False
            for s in self.segments:
                if new_segment.intersection(s) != []:
                    intersection = True
                    break
            if not intersection:
                self.segments.append(new_segment)
        return self.segments
def createallwaypoint(rboundary):
    #convert to rotated frame

    #define the width,generate cut position

    cutpos = np.arange(bounds[0] + width, bounds[2] + width, width)
    pointpos = np.arange(bounds[0] + width / 2, bounds[2] + width / 2, width)
    cutlines = formperpenicularlines(cutpos)
    #form sub-polygons cut by cutlines
    subpolygons = []
    remain = rboundary
    for line in cutlines:
        subpolygon, remain = splitpolygon(remain, line)
        subpolygons.append(subpolygon)
        if not (remain):
            break
    #design waypoints in subpolygons
    i = 0
    waypoints = []
    for polygon in subpolygons:
        sides = polygon.sides
        vertices = polygon.vertices
        lines = []
        for side in sides:
            if (side.is_parallel(yaxis)):
                lines.append(side)
        if (len(lines) == 2):  #formed by 2 cuts
            if (lines[0].p1.x < lines[1].p1.x):
                leftline = lines[0]
                rightline = lines[1]
            else:
                leftline = lines[1]
                rightline = lines[0]
            if (leftline.p1.y < leftline.p2.y):
                leftline = Line(leftline.p2, leftline.p1)
            if (rightline.p1.y < rightline.p2.y):
                rightline = Line(rightline.p2, rightline.p1)
            #seprate vertices into upper part and lower part
            leftupindex = findpoint(vertices, leftline.p1)
            leftdownindex = findpoint(vertices, leftline.p2)
            rightupindex = findpoint(vertices, rightline.p1)
            rightdownindex = findpoint(vertices, rightline.p2)
            if (rightupindex < leftupindex):
                uppoints = list(vertices[rightupindex:leftupindex + 1])
            else:
                uppoints = list(vertices[rightupindex:len(vertices)])
                uppoints.extend(vertices[0:leftupindex + 1])
            if (leftdownindex < rightdownindex):
                downpoints = list(vertices[leftdownindex:rightdownindex + 1])
            else:
                downpoints = list(vertices[leftdownindex:len(vertices)])
                downpoints.extend(vertices[0:leftupindex + 1])
            index = findbound(uppoints)
            buttomindex = index[1]  #upper min y
            if (index[1] == index[3]):  #upper part is segment
                up1 = None
            else:
                cut = xaxis.parallel_line(uppoints[buttomindex])
                up1, temp = splitpolygon(polygon, cut)
            index = findbound(downpoints)
            topindex = index[3]  #lower max y
            if (index[1] == index[3]):  #lower part is segment
                down1 = None
            else:
                cut = xaxis.parallel_line(downpoints[topindex])
                temp, down1 = splitpolygon(polygon, cut)
            waypoint = []
            if (up1 and down1):
                waypoint = createwaypoint(up1, down1, pointpos[i])
            elif (up1):  #lower part is segment
                upbound = up1.bounds
                upmin = upbound[1]  #ymin
                upmax = upbound[3]  #ymax
                buttom = downpoints[topindex].y  #y of lower part
                if (upmax - upmin <= width / 2):
                    waypoint = [Point(pointpos[i], upmax - width / 2)]
                else:
                    cutpt = Point(pointpos[i], upmax - width / 2)
                    cut = yaxis.perpendicular_line(cutpt)
                    crosspoint = polygon.intersection(cut)
                    if (crosspoint[0].x < crosspoint[1].x):
                        leftcross = crosspoint[0]
                        rightcross = crosspoint[1]
                    else:
                        leftcross = crosspoint[1]
                        rightcross = crosspoint[0]
                    if (leftcross.x <= pointpos[i]
                            and rightcross.x >= pointpos[i]):
                        waypoint = [Point(pointpos[i], upmax - width / 2)]
                    elif (leftcross.x > pointpos[i]):
                        waypoint = [leftcross]
                    else:
                        waypoint = [rightcross]
                if (upmin - buttom > width):
                    waypoint.append(Point(pointpos[i], upmin - width / 2))
                waypoint.append(Point(pointpos[i], buttom + width / 2))
            elif (down1):
                downbound = down1.bounds
                downmin = downbound[1]  #ymin
                downmax = downbound[3]  #ymax
                top = uppoints[buttomindex].y  #upper segment y
                if (downmax - downmin <= width / 2):
                    waypoint = [Point(pointpos[i], downmin + width / 2)]
                else:
                    cutpt = Point(pointpos[i], downmin + width / 2)
                    cut = yaxis.perpendicular_line(cutpt)
                    crosspoint = polygon.intersection(cut)
                    if (crosspoint[0].x < crosspoint[1].x):
                        leftcross = crosspoint[0]
                        rightcross = crosspoint[1]
                    else:
                        leftcross = crosspoint[1]
                        rightcross = crosspoint[0]
                    if (leftcross.x <= pointpos[i]
                            and rightcross.x >= pointpos[i]):
                        waypoint = [Point(pointpos[i], downmin + width / 2)]
                    elif (leftcross.x > pointpos[i]):
                        waypoint = [leftcross]
                    else:
                        waypoint = [rightcross]
                if (top - downmax >= width):
                    waypoint.append(Point(pointpos[i], downmax + width / 2))
                waypoint.append(Point(pointpos[i], top - width / 2))
                waypoint.reverse()
            else:
                buttom = downpoints[topindex].y
                top = uppoints[buttomindex].y
                if (top - buttom <= width):
                    waypoint = [Point(pointpos[i], (top + buttom) / 2)]
                else:
                    waypoint = [
                        Point(pointpos[i], top - width / 2),
                        Point(pointpos[i], buttom + width / 2)
                    ]
        elif (len(lines) == 1):
            index = findbound(vertices)
            leftindex = index[0]
            rightindex = index[2]
            line = lines[0]
            if (line.p1.y < line.p2.y):
                line = Line(line.p2, line.p1)
            bound = polygon.bounds
            ymax = bound[3]
            ymin = bound[1]
            xmin = bound[0]
            xmax = bound[2]
            if (line.contains(vertices[leftindex])):  #cut line at left
                if (ymax - ymin <= width):
                    waypoint = [Point(xmax - width / 2, (ymin + ymax) / 2)]
                else:
                    cutpt = Point(pointpos[i], ymax - width / 2)
                    cut = yaxis.perpendicular_line(cutpt)
                    crosspoint = polygon.intersection(cut)
                    if (crosspoint[0].x < crosspoint[1].x):
                        crosspoint = crosspoint[1]
                    else:
                        crosspoint = crosspoint[0]
                    if (crosspoint.x > pointpos[i]):
                        waypoint = [Point(pointpos[i], ymax - width / 2)]
                    else:
                        waypoint = [crosspoint]
                    cutpt = Point(pointpos[i], ymin + width / 2)
                    cut = yaxis.perpendicular_line(cutpt)
                    crosspoint = polygon.intersection(cut)
                    if (crosspoint[0].x < crosspoint[1].x):
                        crosspoint = crosspoint[1]
                    else:
                        crosspoint = crosspoint[0]
                    if (crosspoint.x > pointpos[i]):
                        waypoint.append(Point(pointpos[i], ymin + width / 2))
                    else:
                        waypoint.append(crosspoint)
                    #see if segment consist of 2 waypoints can cover rightmost part
                    way = Segment(*waypoint)
                    wp = Point(vertices[rightindex].x - width / 2,
                               vertices[rightindex].y)
                    limit = xaxis.perpendicular_line(wp)
                    if not (way.intersection(limit)):
                        waypoint.insert(1, wp)
            else:  #cut line at right
                if (ymax - ymin <= width):
                    waypoint = [Point(xmin + width / 2, (ymax + ymin) / 2)]
                else:
                    cutpt = Point(pointpos[i], ymax - width / 2)
                    cut = yaxis.perpendicular_line(cutpt)
                    crosspoint = polygon.intersection(cut)
                    if (crosspoint[0].x < crosspoint[1].x):
                        crosspoint = crosspoint[0]
                    else:
                        crosspoint = crosspoint[1]
                    if (crosspoint.x < pointpos[i]):
                        waypoint = [Point(pointpos[i], ymax - width / 2)]
                    else:
                        waypoint = [crosspoint]
                    cutpt = Point(pointpos[i], ymin + width / 2)
                    cut = yaxis.perpendicular_line(cutpt)
                    crosspoint = polygon.intersection(cut)
                    if (crosspoint[0].x < crosspoint[1].x):
                        crosspoint = crosspoint[0]
                    else:
                        crosspoint = crosspoint[1]
                    if (crosspoint.x < pointpos[i]):
                        waypoint.append(Point(pointpos[i], ymin + width / 2))
                    else:
                        waypoint.append(crosspoint)
                    way = Segment(*waypoint)
                    wp = Point(vertices[leftindex].x + width / 2,
                               vertices[leftindex].y)
                    limit = xaxis.perpendicular_line(wp)
                    if not (way.intersection(limit)):
                        waypoint.insert(1, wp)
        else:  #width of the area less than sensor width
            bound = polygon.bounds
            if (bound[3] - bound[1] < width):
                waypoint = [Point(pointpos[i], (bound[1] + bound[3]) / 2)]
            else:
                ypos = np.array(bound[3] - width / 2, bound[1] + width / 2,
                                width)
                ypos = ypos.reshape(len(ypos), 1)
                waypoint = ypos.insert(0, pointpos[i], axis=1)
        i = i + 1
        if (i % 2 == 1):
            waypoint.reverse()
        waypoints.extend(waypoint)
    return waypoints
Exemplo n.º 7
0
def check_segment_crossing(p1,p2,q1,q2) :
    p = Segment(p1, p2)
    q = Segment(q1, q2)
    return len(p.intersection(q)) > 0
Exemplo n.º 8
0
         cut = yaxis.perpendicular_line(cutpt)
         crosspoint = polygon.intersection(cut)
         if (crosspoint[0].x < crosspoint[1].x):
             crosspoint = crosspoint[1]
         else:
             crosspoint = crosspoint[0]
         if (crosspoint.x > pointpos[i]):
             waypoint.append(Point(pointpos[i], ymin + width / 2))
         else:
             waypoint.append(crosspoint)
         #see if segment consist of 2 waypoints can cover rightmost part
         way = Segment(*waypoint)
         wp = Point(vertices[rightindex].x - width / 2,
                    vertices[rightindex].y)
         limit = xaxis.perpendicular_line(wp)
         if not (way.intersection(limit)):
             waypoint.insert(1, wp)
 else:  #cut line at right
     if (ymax - ymin <= width):
         waypoint = [Point(xmin + width / 2, (ymax + ymin) / 2)]
     else:
         cutpt = Point(pointpos[i], ymax - width / 2)
         cut = yaxis.perpendicular_line(cutpt)
         crosspoint = polygon.intersection(cut)
         if (crosspoint[0].x < crosspoint[1].x):
             crosspoint = crosspoint[0]
         else:
             crosspoint = crosspoint[1]
         if (crosspoint.x < pointpos[i]):
             waypoint = [Point(pointpos[i], ymax - width / 2)]
         else: