Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:37:57+00:00 2026-06-10T20:37:57+00:00

I have a closed non-self-intersecting polygon. Its vertices are saved in two vectors X,

  • 0

I have a closed non-self-intersecting polygon. Its vertices are saved in two vectors X, and Y. Finally the values of X and Y are bound between 0 and 22.

I’d like to construct a matrix of size 22×22 and set the value of each bin equal to true if part of the polygon overlaps with that bin, otherwise false.

My initial thought was to generate a grid of points defined with [a, b] = meshgrid(1:22) and then to use inpolygon to determine which points of the grid were in the polygon.

[a b] = meshgrid(1:22);
inPoly1 = inpolygon(a,b,X,Y);

However this only returns true if if the center of the bin is contained in the polygon, ie it returns the red shape in the image below. However what need is more along the lines of the green shape (although its still an incomplete solution).

To get the green blob I performed four calls to inpolygon. For each comparison I shifted the grid of points either NE, NW, SE, or SW by 1/2. This is equivalent to testing if the corners of a bin are in the polygon.

inPoly2 = inpolygon(a-.5,b-.5,X,Y) | inpolygon(a+.5,b-.5,X,Y) | inpolygon(a-.5,b+5,X,Y) | inpolygon(a+.5,b+.5,X,Y);

While this does provide me with a partial solution it fails in the case when a vertex is contain in a bin but none of the bin corners are.

Is there a more direct way of attacking this problem, with preferably a solution that produces more readable code?

enter image description here

This plot was drawn with:

imagesc(inPoly1 + inPoly2); hold on;
line(a, b, 'w.');
line(X, Y, 'y); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:37:59+00:00Added an answer on June 10, 2026 at 8:37 pm

    One suggestion is to use the polybool function (not available in 2008b or earlier). It finds the intersection of two polygons and returns resulting vertices (or an empty vector if no vertices exist). To use it here, we iterate (using arrayfun) over all of the squares in your grid check to see whether the output argument to polybool is empty (e.g. no overlap).

    N=22;
    sqX = repmat([1:N]',1,N);
    sqX = sqX(:);
    sqY = repmat(1:N,N,1);
    sqY = sqY(:);
    
    intersects = arrayfun((@(xs,ys) ...
          (~isempty(polybool('intersection',X,Y,[xs-1 xs-1 xs xs],[ys-1 ys ys ys-1])))),...
          sqX,sqY);
    
    intersects = reshape(intersects,22,22);
    

    Here is the resulting image:

    enter image description here

    Code for plotting:

    imagesc(.5:1:N-.5,.5:1:N-.5,intersects');
    hold on;
    plot(X,Y,'w');
    for x = 1:N
        plot([0 N],[x x],'-k');
        plot([x x],[0 N],'-k');
    end
    hold off;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a binary image with non closed curves of 1-pixel width on it.
We have a Service Broker application where we're communicating between two SQL Server instances
I have a site at blog.foobar.com that I have closed down, and I want
I have a closed-source 3rd party program, and I want to be able to
I have a custom closed-hashset/open-addressing (i.e. no linked lists) class. It's very specific to
I have sheet1.xls which is closed. I am working in sheet2.xls VBA as follows.
I have a form that is closed unexpectedly immediately after showing it. I hooked
I have a field with enums: 'preview','active','closed' When I query like this: $query =
I have a number of lists that can be opened or closed, something like
I have a Python script that is using some closed-box Python functions (i.e. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.