Java:从两个焦点的坐标开始绘制一个Ellipse2D

时间:2014-10-30 14:12:40

标签: java awt ellipse drawellipse

Ellipse2D允许我画一个椭圆,假设我知道左上角和a和b轴的坐标。相反,我有两个焦点的坐标,以及两个焦点的每个点的距离。如何从两个焦点的坐标开始创建相应的Ellipse2D?

1 个答案:

答案 0 :(得分: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;
    }