2012-04-24 1 views
3

여기 내 자바 문제가 Shape 인터페이스의 (의 Rectangle2D r에) 메소드가 포함되어 있습니다. boolean contains (Rectangle2D r)의 "Shape의 내부가 지정된 Rectangle2D를 완전하게 포함하고 있을지 어떨지를 테스트합니다."라는 문제가 있습니다. 이제 Rectangle2D는 사각형의 모서리 좌표를 가져 오는 메서드를 제공하지 않는 추상 클래스입니다. 보다 정확하게는, "Rectangle2D 클래스는 위치 (x, y)와 크기 (wxh)로 정의되는 사각형을 기술합니다.이 클래스는 2D 사각형을 저장하는 모든 객체의 추상 수퍼 클래스입니다. 하위 클래스에 남겨 둡니다. "구현 부울

어떻게 해결할 수 있습니까?

내 코드의 일부를 검색 :

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???? 
} 
} 
+0

이 숙제입니까? 그러나 태그 슬롯이 남아 있지 않습니다 ... –

+0

어떤 개발 환경을 사용하고 있습니까? 특정 클래스에 대한 지식 부족이 문제가되지 않아야하기 때문에 'r'을 입력 할 때'r '의 사용 가능한 메소드를 보여 주어야합니다. – weston

답변

4

Rectangle2DRectangularShape에서 getMaxX, getMaxY, getMinX, getMinY 상속합니다. 그래서 모서리의 코도를 얻을 수 있습니다.

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

페이지의 "클래스 java.awt.geom.RectangularShape로부터 상속 방법".

+0

@HovercraftFullOfEels yes,하지만 새 사각형 객체를 만들지 않습니까? 내부 경계 사각형이 노출되지 않는다고 가정합니다. 그리고 당신은 여전히 ​​그 객체에 대해 getX()'getY()''getWidth()'와'getHeight()'를 호출하여 4 개의 좌표를 테스트해야합니다. – weston

+1

네, 맞습니다. 도움이되지 않는 의견을 삭제하겠습니다. –

1

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 
} 
0

을 위해 작동합니다

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) 

는 그러면 (그래서 모서리를 계산하는 사소한 경우는 좌측 상단 코너 X 및 Y와 같은 폭과 높이를 갖는) 그에게 Rectangle2D 모서리를 통과 그 contains 방법을 사용할 수있다. truecontains 모든 직사각형 모서리에 적용됩니다. 원에 직사각형이 포함되어 있음을 나타냅니다.