0

Is there a smart way to get the indices of all the entries within four points? In particular this is about the case where I have four points (x_1, y_1), (x_1, y_2), (x_2, y_3), (x_2, y_4) and I need all the indices "within", including the boundaries. One could obviously loop through all x values, but as the array is quite large, I would prefer a direct way. In the end I need an array with ones inside the quadrilateral and zeros outside.

For example this input:

np.arange(1, 13).reshape((4, 3)) #= [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
x_1 = 0
x_2 = 3
y_1 = 0
y_2 = 1
y_3 = 1
y_4 = 2

should give this output:

np.array([[1,1,0],[0,1,0],[0,1,0],[0,1,1]])#numbers 1, 2, 5, 8, 11, 12 have a 1
munichmath
  • 65
  • 6
  • 1
    Show us the obvious, with [mcve]. That will give us a test case and eliminate any ambiguities. – hpaulj May 06 '22 at 08:24
  • Does this answer your question? [What's the fastest way of checking if a point is inside a polygon in python](https://stackoverflow.com/questions/36399381/whats-the-fastest-way-of-checking-if-a-point-is-inside-a-polygon-in-python) – Jussi Nurminen May 06 '22 at 08:32
  • Not quite. This is a very genery example, while I have a specific case. Pretty much all of these examples contain either loops or special libraries, which I do not want to use. Like I said my example is trivial with a loop but I'm wondering if there is a more elegant way that creates the indices. THese methods check every point which would be detrimental since the array is quite large – munichmath May 06 '22 at 08:49
  • Why are `x3` and `x4` missing? – norok2 May 06 '22 at 09:24
  • [(x_1, y_1), (x_1, y_2)] and [(x_2, y_3), (x_2, y_4)] are parallel because of the problem at hand – munichmath May 06 '22 at 10:18
  • You could also say that y_1 and y_2, and y_3 and y_4 are on top of each other – munichmath May 06 '22 at 10:20
  • Does this answer your question? [How to accelerate my written python code: function containing nested functions for classification of points by polygons](https://stackoverflow.com/questions/70469480/how-to-accelerate-my-written-python-code-function-containing-nested-functions-f) – Ali_Sh May 06 '22 at 10:23
  • Your question is a little confusing for me. If finding the points in a region is the main goal, so the 2 aforementioned links will contain the solution (perhaps need little changes). But if not, putting the loop code in the body of the issue will help readers how the expected results could be achieved. Please, improve the question by adding the loop or explain more how the results will be determined. – Ali_Sh May 06 '22 at 10:31
  • The links contain a solution but since I am working on a special case I thought there might be a better one – munichmath May 11 '22 at 15:27

0 Answers0