Пример #1
0
def generate_point_inside_polygon(filename, poly:Polygon)->point:

    l = return_extrems(filename)

    p = point(random.randint(l[0], l[1]), random.randint(l[2], l[3]))
    while not PointInsidePolygon(p, poly):
        p = point(random.randint(l[0], l[1]), random.randint(l[2], l[3]))
    print('generated point', p)
    return p
Пример #2
0
def PointInsidePolygon(p: point, poly: Polygon):

    extremePoint = point(inf, p.y)
    s = segment(p, extremePoint)

    i = 0  # number of intersections of extreme point and sides od Polygon
    counter = 0
    length = len(poly.points)

    while i < length:
        s1 = segment(poly.points[i], poly.points[(i + 1) % length])

        if intersection(s, s1):

            counter = counter + 1
            if orientation(poly.points[i], p, poly.points[(i + 1) % length]) == 0:
                # print('collinear')
                return False
                    #onSegment(poly.points[i], poly.points[(i + 1) % length], p)
        i += 1
    # print(counter)
    if counter % 2 == 0:
        return False
    else:
        return True
Пример #3
0
def is_guard_set(input_list: List[point],
                 poly: Polygon) -> Tuple[bool, List[segment]]:

    visible_vertex = []
    segments = []
    reflex_vertices_minus_one = []

    for p in input_list:

        for vertex in poly.points:
            if vertex not in visible_vertex:
                s = segment(vertex, p)
                # srediste segmenta
                p1 = point((vertex.x + p.x) / 2, (vertex.y + p.y) / 2)
                #potrebno je provjeriti da li je segment unutar poligona sa PointInsidePolygon(p1, poly):

                if not IntersectionP1(poly, s) and PointInsidePolygon(
                        p1, poly):
                    segments.append(s)
                    visible_vertex.append(vertex)

                # slucaj kada se segmenti preklapaju, tj kada cuvar sa refleksne ivice vidi susjedni vrh
                else:
                    for i in range(0, len(poly.points)):
                        seg = segment(poly.points[i],
                                      poly.points[(i + 1) % len(poly.points)])
                        if same_segments(seg, s):
                            visible_vertex.append(vertex)
                            segments.append(s)

    #print('visible vertex', len(visible_vertex))
    #print('poly points', len(poly.points))

    return len(visible_vertex) == len(poly.points), segments
Пример #4
0
def generate_polygon() -> Polygon:

    n = random.randint(3, 20)

    i = 0
    l = []
    while i < n:
        x = random.randint(-300, 300)
        y = random.randint(-300, 300)
        l.append(point(x, y))
        i += 1
    return Polygon(get_simple_polygon(l))
Пример #5
0
def convex_polygon() -> None:

    n = random.randint(3, 6)

    i = 0
    l = []
    while i < n:
        x = random.randint(-300, 300)
        y = random.randint(-300, 300)
        l.append(point(x, y))
        i += 1

    p = Polygon(l)
    p.drawTwo(canvas)
    print(p.convexPolygon())
    print(n)
Пример #6
0
def visible_points(p: point, poly: Polygon):

    counter = 0
    for vertex in poly.points:
        s = segment(vertex, p)
        # srediste segmenta
        p1 = point((vertex.x + p.x) / 2, (vertex.y + p.y) / 2)
        # potrebno je provjeriti da li je segment unutar poligona sa PointInsidePolygon(p1, poly):

        if not IntersectionP1(poly, s) and PointInsidePolygon(p1, poly):
            counter += 1

        # slucaj kada se segmenti preklapaju, tj kada cuvar sa refleksne ivice vidi susjedni vrh
        else:
            for i in range(0, len(poly.points)):
                seg = segment(poly.points[i],
                              poly.points[(i + 1) % len(poly.points)])
                if same_segments(seg, s):
                    counter += 1

    return counter
Пример #7
0
def run_naive_algorithm(filename)-> List[point]:

    input_list = create_coordinates(filename)
    guards = []
    visible_vertex = []

    polygon = Polygon(input_list)

    polygon_vertices = input_list

    while len(visible_vertex) < len(polygon_vertices):

        print("novi while")
        random_guard = generate_point_inside_polygon(filename, polygon)
        print("izabrao random tacku")

        for vertex in polygon_vertices:
            print("vertex", vertex)

            if vertex not in visible_vertex:
                s = segment(random_guard, vertex)
                p1 = point((vertex.x + random_guard.x)/2, (vertex.y+random_guard.y)/2)

                if not IntersectionP1(polygon, s) and PointInsidePolygon(p1, polygon):
                    visible_vertex.append(vertex)
                    if random_guard not in guards:
                        guards.append(random_guard)
                        print("dosao do guarda")

                    # slucaj kada se segmenti preklapaju, tj kada cuvar sa refleksne ivice vidi susjedni vrh
                else:
                    for i in range(0, len(polygon.points)):
                        seg = segment(polygon.points[i], polygon.points[(i + 1) % len(polygon.points)])
                        if same_segments(seg, s):
                            visible_vertex.append(vertex)

    return guards
Пример #8
0
        while orientation(s.next_to_top(), s.peek(), input_list1[i]) < 0:
            s.pop()
            # print(s.next_to_top(), s.peek(), input_list1[i])

        s.push(input_list1[i])

    l = []

    while not s.is_empty():
        l.append(s.peek())
        s.pop()
    return Polygon(l)


s = segment(point(0, 0), point(5, 5))

s1 = segment(point(5, 5), point(5, 10))

# print(intersection(s, s1))



#-----------------------------------

# generate polygon

#---------------------------------------


def generate_polygon() -> Polygon:
Пример #9
0
def create_coordinates(filename) -> List[point]:

    l = read_file_into_list(filename)

    points = []
    #print(l)

    l_for_extrems = []

    fizicke = []

    for i in range(0, len(l) - 3, 4):
        x = (l[i] / l[i + 1])
        y = (l[i + 2] / l[i + 3])

        point_p = point(x, y)

        l_for_extrems.append(x)
        l_for_extrems.append(y)
        points.append(point_p)

    x_coords = []

    for i in range(0, len(l_for_extrems), 2):
        x_coords.append(l_for_extrems[i])

    x_min = min(x_coords)

    print('xmin', x_min)

    for i in range(0, len(l_for_extrems), 2):
        x_coords.append(l_for_extrems[i])

    x_max = max(x_coords)

    print('xmax', x_max)

    y_coords = []

    for i in range(1, len(l_for_extrems), 2):
        y_coords.append(l_for_extrems[i])

    y_min = min(y_coords)

    print('ymin', y_min)

    for i in range(1, len(l_for_extrems), 2):
        y_coords.append(l_for_extrems[i])

    y_max = max(y_coords)

    print('ymax', y_max)

    for item in points:
        x_fiz = turtle.Screen().canvwidth - 0 - (turtle.Screen().canvwidth) * (
            item.x - x_min) / (x_max - x_min) * 2
        y_fiz = turtle.Screen().canvheight - 0 - (turtle.Screen(
        ).canvheight) * (item.y - y_min) / (y_max - y_min) * 2
        fiz_point = point(x_fiz, y_fiz)
        fizicke.append(fiz_point)

    return fizicke
Пример #10
0
def callback(event):
    canvas = event.widget
    x, y = canvas.canvasx(event.x), canvas.canvasx(event.y)
    p = point(x, y)

    p.draw(canvas)
Пример #11
0
def translate_coord_from_canvas(x: int, y: int) -> point:
    return point(-(x - CENTER), (y - CENTER))