MATLAB: Find the intersection of a square pixel with a closed 2D curve

curveintersectionMATLAB

Dear MatLab Experts,
I would appreciate your help with the following problem.
I have a set of closed concave 2D curves of arbitrary shape. The coordinates of the polygonal curve are known.
I cover the areas inclosed by the curves with square pixels whose side is a parameter. Therefore the pixels may be smaller or larger.
The rim of the area (curve) must be covered by the square pixels as well. Please, see the attached pictures.
Question:
How can I find the pixels that intesect the curve as distinguished from the pixels totally contained inside the curve?
Thank you in advance for any suggestion and help.
Kind regards,
Maura E. M.

Best Answer

Dear All,
I have attached
  • the main script "Pixelize_Collimator_Aperture_v4.m"
  • the funcions called by the script "CollimatorGrid_v3.m", "curveintersect.m"
  • the coordinates of the closed curve that causes the problem "26-Jul-2018_A170258" in text format
  • the picture showing the problem on the attached curve "A170258.jpg"
  • the function I downloaded from FEX that find curves intersections "curveintersect.m"
To run the code, please do the following:
  1. run "Pixelize_Collimator_Aperture_v4.m"
  2. upon promp select the folder containing the curve "26-Jul-2018_A170258.txt"
  3. upon prompt click on the curve file name "26-Jul-2018_A170257.txt"
  4. upon prompt enter 1 for the number of nodes (of the Grid) per unit square area. This data can be any positive number (not necessaily integer). If there are too many points per unit square area then it becomes hard to see what is going on.
  5. the program will stop on line 155 of script "Pixelize_Collimator_Aprture_v4.m"
Given a closed 2D arbitrary curve, the program builds a grid over the curve bounding box. The goal is to approximate the area inclosed by the curve with square pixels whose side is sqrt(#nodes per square area). The goal is to keep the pixels that
  1. are totally inside the closed area
  2. intersect the curve on more than one point
  3. double the resolution only of the pixels that intersect the curve in more than one
  4. repeat steps 1. and 2. on the new smaller pixels in order to better approximate the area
The logic of my script is wrong as it would keep a pixel (yellow in the attached picture) that is totally outside the closed curve but only has the upper left corner on the curve. This corner coordinates are printed on the picture and coincide with one of the 4 points making up the pixel as printed by the script.
I wrongly toughts that MatLab function "inpolygon" would separate the points inside a curve from the points on the curve. Instead this is returns as "inside" a point which is on the curve.
I use the contributed function "curveintersect" as a double-check. Luckily, as it helped me detect my error.
********* Question *********
In order to find the pixels that intersect the curve on more than one pont may I totally rely on function "curveintersect"?
Actually, I still have to use fynction "inpolygon" to keep the pixels totally inside the closed area. Whereas I will generate 4 smaller pixels from each original pixel that intesect the curve.
Thank you in advance for any help and/or suggestion.
Kind regards,
maura
Related Question