2014-10-30 5 views
0

Ellipse2D를 사용하면 왼쪽 위 모퉁이의 좌표와 a 및 b 축을 알고 있다고 가정하여 타원을 그릴 수 있습니다. 대신, 나는 두 초점의 좌표와 두 초점으로부터 각 점의 거리를가집니다. 어떻게 두 foci의 좌표에서 시작하는 해당 Ellipse2D를 만들 수 있습니까?Java : 두 초점의 좌표에서 시작하는 Ellipse2D 그리기

+0

1. 오라클 자습서 ??? - 기하학적 인 도형 그리기, 2. 여기에서 검색 java + Ellipse2D – mKorbel

+1

@mKorbel 나는 이것이 좀 더 복잡하다고 생각한다. 그러나 정보가 부족합니다. 초점을 기반으로 Ellipse2D를 작성하는 방법을 잘 모르겠습니다. 2. 포커스가 동일한 y 좌표를 갖지 않으면 "불가능합니다"(Ellipse2D는 저장할 수 없습니다. a * rotate * 타원 자체 - Ellipse2D와 회전 각이 필요하거나 일반적인 'Shape'으로 표현) – Marco13

+0

실제로 Oracle 문서에서는 이것이 고려되지 않았습니다. 실제로 Ellipse2D 클래스는 줄임표를 가정하고 지원합니다. 두 축은 ​​X와 Y에 평행합니다. 그래서 나는이 질문을했습니다. – marcorossi

답변

2

여기 내 코드입니다. 원점 (0,0)을 중심으로 올바른 크기의 타원을 만듭니다. 그런 다음 AffineTransform을 사용하여 타원을 번역하고 적절한 위치로 회전시킵니다. 필요한 타원을 정의하는 Shape를 리턴합니다. 변수 이름은 다음에서 제공됩니다. http://math.tutorvista.com/geometry/ellipse-equation.html 희망 사항.

#import java.awt.geom.*; 

    /* Create an ellipse given foci and distance. 
    (x1,y1) and (x2,y2) are foci. semimajor axis (the sum of distances 
    that define the ellipse) is *dist*. 
    See 
    for meaning of vars 
    */ 
    public Shape makeEllipse (
     float x1, float y1, float x2, float y2, float dist 
    ) 
    { 
     // Create foci points 
     Point2D f1 = new Point2D.Float (x1, y1); 
     Point2D f2 = new Point2D.Float (x2, y2); 

     // Calculate ellipse characteristics 
     double a = dist/2.0; 
     double c = f1.distance (f2)/2.0; 
     // If 'dist' is smaller than the distance between foci, 
     // the ellipse is invalid 
     if (a < c) 
      die ("Invalid semimajor axis length"); 
     double b = Math.sqrt (a * a - c * c); 

     Point2D centre = 
      new Point2D.Float ((x1 + x2)/2.0, (y1 + y2)/2.0); 

     // Create a transform to rotate and translate the ellipse 
     double theta = Math.atan2 (y2 - y1, x2 - x1); 
     AffineTransform trans = new AffineTransform(); 
     trans.translate (centre.getX(), centre.getY()); 
     trans.rotate(theta); 

     // Create an ellipse with correct size but origin at centre 
     Ellipse2D tmpEllipse = new Ellipse2D.Double (-a, -b, 2 * a, 2 * b); 

     // Translate and rotate it to where it should be 
     Shape ellipse = trans.createTransformedShape (tmpEllipse); 

     return ellipse; 
    }