0

suppose to have two polygons p1 and p2, where p2 is completely inside p1

p1 = [(0, 10), (10, 10), (10, 0), (0, 0)]
p2 = [(2, 6), (6, 6), (6, 2), (2, 2)]

degree_of_contact = 0

xyarrays = [p1,p2]
p1_degree_of_contact = 0
for x,y in xyarrays[0]:
        if point_inside_polygon(x,y,xyarrays[1]):
            p1_degree_of_contact += 1

p2_degree_of_contact = 0
for x,y in xyarrays[1]:
        if point_inside_polygon(x,y,xyarrays[0]):
            p2_degree_of_contact += 1

degree_of_contact = p1_degree_of_contact + p2_degree_of_contact

where point_inside_polygon is to deciding if a point is inside (True, False otherwise) a polygon, where poly is a list of pairs (x,y) containing the coordinates of the polygon's vertices. The algorithm is called the "Ray Casting Method

i wish to combine in an elegant way (line coding save) both loops in one.

1
  • Since I think your work is relevant, take a look at numpy which has built in support for vectors and matrices. There are nice tricks you can do there (like vectorized loops) that are purely written in C and are very fast. And its not only for speed: there are tons of built-in math methods which should make your life easier for such problems. scipy.org/Tentative_NumPy_Tutorial Commented Mar 6, 2013 at 18:13

2 Answers 2

3

The following should work:

degree_of_contact = 0
for tmp1, tmp2 in [(p1, p2), (p2, p1)]:
    for x,y in tmp1:
        if point_inside_polygon(x, y, tmp2):
            degree_of_contact += 1
Sign up to request clarification or add additional context in comments.

Comments

1
degree_of_contact = sum(point_inside_polygon(x, y, i) for i, j in ((p1, p2), (p2, p1)) for x, y in j)

1 Comment

I wonder if I can use this approach for my problem: stackoverflow.com/questions/68482842/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.