File: is_point_in_polygon.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (69 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#  Copyright (c) 2024, Manfred Moitzi
#  License: MIT License
import time

from ezdxf.math import Vec2
from ezdxf.math._construct import is_point_in_polygon_2d
from ezdxf.acc.construct import is_point_in_polygon_2d as is_point_in_polygon_cy

VERTICES = [(1, 1), (5, 1), (5, 3), (3, 3), (3, 5), (5, 5), (5, 7), (1, 7)]
SHAPE_PY = Vec2.list(VERTICES)

POINTS_INSIDE = Vec2.list(
    [(2, 2), (3, 2), (4, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 6), (4, 6)]
)

POINTS_OUTSIDE = Vec2.list(
    [
        (0, 0),  # 0
        (2, 0),  # 1
        (4, 0),  # 2
        (6, 0),  # 3
        (0, 2),  # 4
        (6, 2),  # 5
        (0, 4),  # 6
        (4, 4),  # 7
        (6, 2),  # 8
        (0, 6),  # 9
        (6, 6),  # a
        (0, 8),  # b
        (2, 8),  # c
        (4, 8),  # d
        (6, 8),  # e
    ]
)


ROUNDS = 2000


def python_version():
    for point in POINTS_INSIDE + POINTS_OUTSIDE:
        is_point_in_polygon_2d(point, SHAPE_PY)


def cython_version():
    for point in POINTS_INSIDE + POINTS_OUTSIDE:
        is_point_in_polygon_cy(point, SHAPE_PY)


def profile(func) -> float:
    t0 = time.perf_counter()
    for _ in range(ROUNDS):
        func()
    t1 = time.perf_counter()
    return t1 - t0


def main():
    py_t = profile(python_version)
    print(f"python version: {py_t:.3f}s")

    # Numpy/Cython version was just 10x faster, this Cython version is 23x faster!
    cy_t = profile(cython_version)
    print(f"cython version: {cy_t:.3f}s")
    print(f"ratio python/cython: {py_t/cy_t:.3f}")


if __name__ == "__main__":
    main()