intersect function with concave or rounded graphics

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderator: Klaus

Post Reply
Eric_Taquet
Posts: 29
Joined: Wed Feb 14, 2007 3:26 pm

intersect function with concave or rounded graphics

Post by Eric_Taquet » Thu Jan 07, 2010 12:29 am

I'd like to know if an object intersects another one, yes
but say these objects are ovals or concave polygons ...

the intersect function returns true if their rects intersect
even if their shapes does not intersect and if they are not inside each other !!!

a new feature like this one could be useful :

intersect (object, object)

if the object is an oval or rounded rect, its actual shape is considered
if it is another grc (polygon ...), its shape is "the points"
for any other object, its rect is considered

------------

And what about the difference between inside and border

an object can be within another one (say inside) but their borders (I say their shapes) does not intersect !!! it could be useful for some games (labytinth...) and other

for compatibility issues, we could keep "is within" (or not) for objects and points
as usual

but introduce a variant in the within function :

within (object1, object2 | point)

that returns true, when second param is an object,
if and only if object2 is entirely inside object1, considering their shapes / borders

and we could have the following 'intersect' syntax, with an optional third param :

intersect (object, object [, border])

that consider only objects borders if the third param is true
interesting for detection of "contact" / "touch" between objects borders

and consider objects rect insides if this third param is false or absent,
as usual

your feeling ... ?

Eric
FRANCE
Attachments
Image 1.png
Image 1.png (12.91 KiB) Viewed 2108 times
The Erikoded Frenchy
La logique est le dernier refuge des gens sans imagination
Oscar Wilde :: Logic is the last refuge of the unimaginative

massung
Posts: 93
Joined: Thu Mar 19, 2009 5:34 pm

Re: intersect function with concave or rounded graphics

Post by massung » Mon Jan 11, 2010 4:16 pm

This is a fairly easy problem in computer graphics. I highly doubt Rev would be interested in adding such a specific function for everyone, though, as making it generic (and fast) requires a few "tricks" that likely aren't worth implementing. That said, here's a few helpful hints to get you started...

http://local.wasp.uwa.edu.au/~pbourke/g ... ineline2d/

The above link will come in handy. :D

First, let's solve the trivial case of whether or not a point is somewhere within a concave polygon:

1. Pick any random direction away from the point (straight up, down, left/right is easiest, but any direction will work).
2. Create a vector from the point moving in that direction.
3. Test the number of times your vector intersects the line segments of the polygon.
4. If the line intersects an odd number of times, the point is within the polygon. Even (or zero) indicates it is not within the polygon.

Note: there is an edge case where your vector happens to be co-planar with a line segment, so doing the above test twice is usually a good idea to prevent false negatives.

Now, circle (or oval, it's the same thing) is just an extension of the above. I'll leave that as an exercise for the reader, but it's important to know whether you care about overlapping or only want to know about 100% enclosure.

Hint: don't try and solve whether or not the circle intersects the polygon... instead try solving whether or not the polygon intersects the circle. :)

Hope this helps,

Jeff M.

Post Reply