Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a rectangular canvas and I need to verify whether a shape is totally inside that canvas or not. The shape can be lines, ellipses, arcs, rectangles and arbitrary polygons.

Does anyone know a lib with this kind of algorithm, preferably in java?

I searched and found just Slick2D, but it has a bug exactly on the method that verify if shape contains another shape.

Just to exemplifly, in the figure only shapes 2, 4, 5, and 7 are inside the canvas.

enter image description here

share|improve this question
    
Maybe worth looking at: stackoverflow.com/a/13217508/402022 - there I provide a general to test if a point is inside a polygon. –  Theraot 7 hours ago

2 Answers 2

I have good news and bad news for you:

The Bad News: I don't know or remember any Java library that does what you want

The Good News: It's really easy to implement this type of algorithm yourself! Here's a couple, you can mix them to optimize your collision detection depending on the type of shape.


BB Collision Detection

You can imagine a box around your canvas. This algorithm is perfect if the two colliding shapes are rectangles. Now your two boxes have a x-pos, y-pos, width and a height, right? Let's say the x and y positions are the bottom-left point of your bounding box (BB). In pseudocode,

let x1, y1, w1, h1
let x2, y2, w2, h2

if (x1 + w1) - x2 >= 0 and (x2 + w2) - x1 >= 0
    shapesCollide()

Bounding Sphere Collision Detection

This one's easy. If your two shapes are circles, you have two radii, r1 and r2. To detect the collision, you must calculate the distance between the shapes and substract to it the two radii. If this gives you a negative result, then your two shapes are colliding.


SAT (Separating Axis Theorem)

This one is good if you have any shape that is convex. It works very well, albeit a little bit more complex to implement than the two others. Here's a very good tutorial about this algorithm: here


Going Further

If you have concave shapes or want a more optimized algorithm (let's say you have more shapes than a thousand), well there's nothing a quick google search will not fix ;) You can also try to implement these on your own:

  • Separate your concave shape into sub-shapes that are convex
  • Make non-collision zones where you are sure that there won't be any collisions so you don't need to test for it
  • etc.

Good luck!

EDIT: Just saw that I'm not answering your question directly... For your problem you could implement the SAT algorithm and detect if the shapes are not colliding with the inverse of your global canevas... anyways, sorry!

share|improve this answer
    
fisrtly, thanks for your comments. by the way, I'm not a game developer or graphics specialist.I know it sounds like collision problem but it is'nt. I just wanna validate the arguments of a function to draw, lines, rectangles, ellipses, arcs and arbitrary polygons in order to verify whether the drawn shape is totally inside or not the canvas. If it isn't I must throw an exception. it seems that there's no java library that do this, so I will try your SAT algorithm recommendation. –  Bruno 9 hours ago
    
Do you know a open source library in any language that implement this? I'm asking because almost all languages has compilers for java bytecode. –  Bruno 9 hours ago
    
It's too bad you went to all this trouble providing a solution to a completely different question. –  FreeAsInBeer 6 hours ago

As already pointed out in the comments and answer: This can be arbitrarily complex.

Particularly, depending on the exact use case and performance requirements, you can employ some rather sophisticated data structures in order to make these tests fast. The bounding box test is the simplest one that should be done in any case (and in fact, could already be sufficient for many application cases). But beyond that, you could more tight-fitting bounding volumes like oriented bounding boxes, possibly in a hierarchy to accelerate the tests for complex shapes (see quad tree, KD Tree and related structures), or employ some sort of spatial hashing.

The "best" choice here also heavily depends on whether the objects are animated or static, or whether objects may be added or removed at runtime.

However, there is a very simple solution with plain Java. Note that this is comparatively inefficient, but it does an exact test for any possible Shape: You can define an Area that defines the region "outside of the screen" (assuming that the coordinates of the actual shapes are not unreasonably (infinitely) large). Then you can intersect the "outside" area with the area of the shape, and see whether the result is empty.

The code could roughly look like this:

Rectangle screenRectangle = ... // The screen/canvas rectangle
int s = Short.MAX_VALUE;
Area a = new Area(new Rectangle(-s, -s, s+s, s+s));
a.subtract(screenRectangle);

Shape toCheck = ... // The shape to check
Area b = new Area(toCheck);
b.intersect(a);
if (b.isEmpty()) {
    // The shape toCheck was completely contained in the screenRectangle
}

Again, this will not be very efficient when you have to check many (complex) shapes, but may still be an option here.

Additionally: A Line2D object would have to be treated differently, because it has no area at all. But fortunately, for a line, you can simply check whether both endpoints of the line are contained in the screen rectangle by calling screenRectangle.contains(lineEndpoint);.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.