A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: Detect if square is drawn around

  1. #1
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194

    Detect if square is drawn around

    Hi,

    I need to detect is someone has drawn around a square, I'm hoping not to have to use path finding (to find a route out of square), is there a simple of detecting that?
    FlashGameMaker.com - er..where I say stuff.

  2. #2
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Some pictures of problem instances would help - perhaps one example "yes" instance and one example "no" instance?

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    maybe store some points of the drawn shape (guessing a squiggly line) in an array. Then loop over those points and check if those points lie within the square's shape.

    this page explains http://ozviz.wasp.uwa.edu.au/~pbourk...ry/insidepoly/

    Another method may be to fill the square with a color, draw box into a bitmapData object, then draw the customShape as well with a "difference" blendMode, then run a getColorBoundsRect on the bitmapData, if the returned rectangle has width then the shape is intersecting some or all of the square.
    Last edited by mr_malee; 11-25-2008 at 08:01 PM.
    lather yourself up with soap - soap arcade

  4. #4
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    I've attached a pic of what I mean. The line will always be straight, not squiggly , but it might be jagged, i'e up a bit, down a bit, but basically still surrounding the red square., the line will never intersect the square, it always has to be outside of the square.
    Attached Images Attached Images
    FlashGameMaker.com - er..where I say stuff.

  5. #5
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    How is the users input (the black line) represented? Do we have it an ordered sequence of points that are sampled from the users continuous gesture? Is it literally four points like that or could it be a funny lasso shape?

    Given a segmented & ordered polyline like that, you can robustly compute its total winding angle around a point using a bunch of cross products. (A suitable point to check against could be the centroid of the square, for example). But if you're wanting to detect if their input curve closely resembles a square shape I'd have to think a little more.

    Problems like this are so cool

  6. #6
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by rachil0
    How is the users input (the black line) represented? Do we have it an ordered sequence of points that are sampled from the users continuous gesture? Is it literally four points like that or could it be a funny lasso shape?

    Given a segmented & ordered polyline like that, you can robustly compute its total winding angle around a point using a bunch of cross products. (A suitable point to check against could be the centroid of the square, for example). But if you're wanting to detect if their input curve closely resembles a square shape I'd have to think a little more.

    Problems like this are so cool
    Is there no non super complicated maths way of doing it? , The screen is actually made up of a grid, where each cell is 10x10 pixels, so on that image I posted the square fits into that grid (lets say in total its 5x5 cells in size), and the "line" would also be using those 10x10 pixels.

    I thought a way to detect if theres a gap would be to use pathfinding out from the square to see if a path can escape say more then 4 cells from the outside of the edge of the square, but again I thought there must be a simpler way of doing it.

    I also thought about just checking all those cells in a band or frame so to speak around the square in some way, but I can see that getting complicated because the line that can be drawn around the square does not have to be made up for 4 lines, but can be jagged (no diagonals though)
    FlashGameMaker.com - er..where I say stuff.

  7. #7
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    Is the square going to be moving?

    Couldn't you just get the angle between the squares center and the mouse, and then check to see how far around the square it has rotated? (although this would also require a separate check to make sure that it doesn't go inside the square)

    If the square is moving you'll have to loop trough every point of the line once the line is complete, but if it's not I think you'd only have to do it once for every new point in the line when the new point is drawn.

  8. #8
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by Captain_404
    Is the square going to be moving?

    Couldn't you just get the angle between the squares center and the mouse, and then check to see how far around the square it has rotated? (although this would also require a separate check to make sure that it doesn't go inside the square)

    If the square is moving you'll have to loop trough every point of the line once the line is complete, but if it's not I think you'd only have to do it once for every new point in the line when the new point is drawn.
    what if the line around the square was like the one in the attached image?
    Attached Images Attached Images
    FlashGameMaker.com - er..where I say stuff.

  9. #9
    Senior Member random10122's Avatar
    Join Date
    Mar 2002
    Location
    Sheffield, UK
    Posts
    1,747
    I like these kind of logical problems, in my opinion this is what fk is good for even if it takes a while to reach the answer! I tried some playing around in photoshop and couldn't find an obvious solution involving measuring the angles..

    Mr_malee's blendmode solution sounds like an ingenius one to me that wouldn't take much work providing you know your way around the BitmapData methods :-)

    fracture2 - the sequel
    fracture - retro shooter
    blog - games, design and the rest

    "2D is a format, not a limitation" -Luis Barriga

  10. #10
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by random10122
    I like these kind of logical problems, in my opinion this is what fk is good for even if it takes a while to reach the answer! I tried some playing around in photoshop and couldn't find an obvious solution involving measuring the angles..

    Mr_malee's blendmode solution sounds like an ingenius one to me that wouldn't take much work providing you know your way around the BitmapData methods :-)
    but the thing is, i don't need to know if the line is intersecting the square, I need to know if the line has fully enveloped the square, i'e completely been drawn around the square, without there being any gaps.
    FlashGameMaker.com - er..where I say stuff.

  11. #11
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    Here's the solution I had in mind. There was one requirement that I wasn't sure about - does the users gesture actually have to self intersect, or can it spiral around without intersecting and still count as a successful lasso?

    The current code doesn't verify that the lasso is self-intersecting but I can add it if desired. The important function is test_lasso() - the rest is boilerplate to capture mouse input and draw the problem.

    EDIT: There's actually a small detail I failed to comment: on line 86, the "twist" variable should accumulate the arcsin of that cross product, not the cross product itself. With digitized mouse motion, the angles between consecutive points are so small that theta and sin(theta) are basically equal, but if your curve is much more coarsely sampled (like the pictures you provided) you should correct that approximation.
    Attached Files Attached Files
    Last edited by rachil0; 11-27-2008 at 10:21 AM.

  12. #12
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I thought this would work simply by casting one ray from center of square in any direction and counting number of times it intersects with line drawn by player:

    http://en.wikipedia.org/wiki/Point_in_polygon
    (known as crossing number algorithm or the even-odd rule algorithm)

  13. #13
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by rachil0
    Here's the solution I had in mind. There was one requirement that I wasn't sure about - does the users gesture actually have to self intersect, or can it spiral around without intersecting and still count as a successful lasso?

    The current code doesn't verify that the lasso is self-intersecting but I can add it if desired. The important function is test_lasso() - the rest is boilerplate to capture mouse input and draw the problem.

    EDIT: There's actually a small detail I failed to comment: on line 86, the "twist" variable should accumulate the arcsin of that cross product, not the cross product itself. With digitized mouse motion, the angles between consecutive points are so small that theta and sin(theta) are basically equal, but if your curve is much more coarsely sampled (like the pictures you provided) you should correct that approximation.
    That's helpful thanks. The lasso will be coarse like you suggested, there won't be any true curves, it will be squares of 10x10 pixels on a grid making up the "lines", so how would I do the correction?
    FlashGameMaker.com - er..where I say stuff.

  14. #14
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    A cheap workaround would just be to run a flood fill on the area enclosed by the lasso (make sure it's enclosed by attaching the end of the lasso to the start of it) and checking that 100% of the surface area of the shape gets covered (maybe just run a getPixel() in each corner of the rectangle).
    http://www.birchlabs.co.uk/
    You know you want to.

  15. #15
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    Quote Originally Posted by VENGEANCE MX
    A cheap workaround would just be to run a flood fill on the area enclosed by the lasso (make sure it's enclosed by attaching the end of the lasso to the start of it) and checking that 100% of the surface area of the shape gets covered (maybe just run a getPixel() in each corner of the rectangle).
    a flood fill is an interesting approach, i'm just not sure it will work it will work with spirals, where you would it to register as complete surrounded, but yet there would be gaps, gives me something to think about though thanks.
    FlashGameMaker.com - er..where I say stuff.

  16. #16
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    Break the problem down and check for four lines, each one traverses the height, width of the square on the appropriate side, they all intersect with each other, and none of them intersect with the square.

    edit.. and none of them are broken between their vital points of intersection with each other.

  17. #17
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    I'm not totally sure what you mean sometimes...Do you want a complete closed gap, or is spirals sufficient to count as complete surrounded?

    If you do want a complete closed gap, then the floodFill way is the best way I see, except for the expensive BitmapData.draw() in real-time...
    If no, then measuring the starting angle, and each one after that, and then seeing if it's more than 360 or not would work.

    (Sorry for not contributing my own ideas...I had one, but I saw a few problems with it...I'll see if I think of anything)

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center