• Matlab point in polygon help needed

    ShikhaTan Member

    Basically when a user clicks any of the polygon I want a function that returns the polygon clicked as well as the area of the polygon.

    Here is what I have so far, looking for suggestions to compute point in polygon and area. Thanks.

    clear
    clc
    Coord = [230 400; 270 420; 300 400;340 430;390 380; 230 300; 300 290;
             370 280;250 200;290 200 ;320 200 ;200 190 ;250 100 ;310 120; 410 110;
             390 180;];
    P = [1 6 7 3 2 1 NaN NaN;
         3 7 8 5 4 3 NaN NaN;
         6 9 10 7 6 NaN NaN NaN;
         7 10 11 8 7 NaN NaN NaN;
         6 12 13 14 10 9 6 NaN;
         8 11 10 14 15 16 8 NaN];
    
    patch('Faces',P,'Vertices',Coord,'FaceColor','w', 'Marker','o');
    
    %
    % Prompt user to click polygon
    %
    disp('Click any polygon')
    [s,t] = ginput(1);
    
    disp(sprintf('point(%8.6f ,%4.2f)',s,t))
    
    %
    % Function to determine polygon
    %
    
    %
    %Function to compute Area
    % 
  • Amit Member
    http://it.mathworks.com/help/matlab/ref/ginput.html
    http://it.mathworks.com/help/matlab/matlab_external/using-events.html

    Those came to my mind, I suggest you to stick with ginput.

  • ShikhaTan Member

    The quick and dirt solution I think is through calculating the area and using a function that draw a polygon shaped button. It doesn’t seem too optimized. I’m looking through the documentation searching something clean. In the meantime you can “square” your polygon to obtain an approximate polygon and you can add a specific event when you click it. A sort of an invisible square that acts like a button.

  • Adan Member

    I was thinking of writing a function that finds a point in polygon. The main challenge now is how this function will determine which polygon was clicked.

    Because if I have:
    Area = polyarea(polygon)
    The polygon parameter has to be retrieved first from the click event before parsing into the area function.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.