The boolean implementation contains the (Rectangle2D r) Shape interface method

here is my problem with Java:

The My Circle class implements the Shape interface and, therefore, it must implement all the necessary methods. I have a problem with the boolean method contains (Rectangle2D r), which "Checks if the inside of the Form contains the fully specified Rectangle2D". Now Rectangle2D is an abstract class that provides (as far as I know) no method for obtaining the coordinates of the corners of a rectangle. More precisely: "The Rectangle2D class describes a rectangle defined by location (x, y) and dimension (wxh). This class is only an abstract superclass for all objects that store a 2D rectangle. Remains in a subclass."

So how can I solve this?

The following is part of my code:

public class Circle implements Shape
{
private double x, y, radius;

public Circle(double x, double y, double radius)
{
    this.x = x;
    this.y = y;
    this.radius = radius;
}

// Tests if the specified coordinates are inside the boundary of the Shape
public boolean contains(double x, double y)
{
    if (Math.pow(this.x-x, 2)+Math.pow(this.y-y, 2) < Math.pow(radius, 2))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Tests if the interior of the Shape entirely contains the specified rectangular area
public boolean contains(double x, double y, double w, double h)
{
    if (this.contains(x, y) && this.contains(x+w, y) && this.contains(x+w, y+h) && this.contains(x, y+h))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Tests if a specified Point2D is inside the boundary of the Shape
public boolean contains(Point2D p)
{
    if (this.contains(p.getX(), p.getY()))
    {
        return true;
    }
    else
    {
        return false;
    }
}

// Tests if the interior of the Shape entirely contains the specified Rectangle2D
public boolean contains(Rectangle2D r)
{
    // WHAT DO I DO HERE????
}
}
+3
4

Rectangle2D getMaxX, getMaxY, getMinX, getMinY RectangularShape. , .

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Rectangle2D.html

. ", java.awt.geom.RectangularShape".

+4

PathIterator.

PathIterator it = rectangle.getPathIterator(null);
while(!it.isDone()) {
    double[] coords = new double[2];
    it.currentSegment(coords);
    // At this point, coords contains the coordinates of one of the vertices. This is where you should check to make sure the vertex is inside your circle
    it.next(); // go to the next point
}
+1

:

    public boolean contains(Rectangle2D r)
{
    return this.contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
0

Ellipse2D.Double, .

Ellipse2D.Double(double x, double y, double w, double h)

contains, Rectangle2D ( X Y, , ). true, contains, , ,

0

All Articles