• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vishnu gb · Nov 22, 2014 at 05:25 AM · positiongridcheckinsideprogrammatically

Programatically check whether a grid is placed inside or outside a closed figure

Hi everyone, I want to build an application with the functionality

1-Draw a closed figure eg:alt text

2-Place a grid inside or outside eg:alt text

where a-Grid inside the closed figure and b-Grid outside the closed figure

3-programatically check whether the grid a is inside or outside the closed figure

u2.jpg (37.9 kB)
u1.jpg (29.0 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image koray1396 · Nov 22, 2014 at 07:52 AM 0
Share

you may want check out PolygonCollider2D and OverlapPoint.

avatar image robertbu · Nov 22, 2014 at 08:36 AM 0
Share

They common solution for checking if a point is inside or outside a closed figure is to walk each segment of the polyline that encloses the figure. For each segment, check to see what side (left or right) of the line formed by the segment the point is on. If the point is on the same side for all segments, then it is inside the polygon. For a rectangle, you'd have to check all four points and there are some degenerate cases with figure like #6 where all four points could be considered inside but part of the figure would be outside.

avatar image NoseKills · Nov 22, 2014 at 09:00 AM 0
Share

Like @robertbu points out, case 6 is the hard one here. You can do something like this to check if a point (corners of the rectangle) is inside a convex shape like 6, but even that doesn't work if one of the convex corners of shape 6 would go in to the checked rectangle through its side and out the same side: all corners would be inside but a side breaks the shape.

avatar image tanoshimi · Nov 22, 2014 at 09:36 AM 0
Share

Raycasting or winding algorithms should still work with concave polygons, but you'll need to test all four corners of the rectangle from all segments that define the containing polyline, which might be slow depending on how complex the shapes are.

You might also be able to take some shortcuts by defining minimum bounding rectangles of each shape and doing an initial test against those - we'd need to know more about the potential bounds of the grid and shape though.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

transform.position error 1 Answer

Looping through array to find a specific class? 0 Answers

How to make a Vector3 A = Vector3 B EXACTLY 1 Answer

How to instantiate the prefabs on grid in Unity? 1 Answer

2D Board game grid placement 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges