首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ellipse2D绘图精度不高

Ellipse2D绘图精度不高
EN

Stack Overflow用户
提问于 2015-10-11 23:26:24
回答 1查看 315关注 0票数 2

我在做空间物理学的应用,所以我做了很多关于轨道的研究。当然,我会遇到Ellipse2D.Double来在屏幕上画出我的轨道。

每当我的JPanel刷新时,我就使用Ellipse2D绘制物体的轨道,以及用不同的方法绘制身体本身。

本质上,我发现当数字变得很大时(不管是轨道的大小变大,或者可视化被放大到很远的地方),身体和Ellipse2D的位置就不会排列在一起。

我使用从极坐标到直角坐标的转换来计算物体的位置,我把Ellipse2D的数学问题留给geom包。

看看这个代码示例。这是我能解决的问题中最自成一体的一个版本,因为这个圈子的规模必须非常大:

代码语言:javascript
复制
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.math.BigDecimal;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class EllipseDemo extends JPanel {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.add(new EllipseDemo());
        frame.setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // These values allow for a very zoomed in view of a piece of the circle
        BigDecimal[] circleCenter = { new BigDecimal(-262842.5), new BigDecimal(-93212.8) };
        BigDecimal circleRadius = new BigDecimal(279081.3);

        // Draw the circle at the given center, with the given width and height
        // x = centerx - radius, y = centery - radius, w = h = radius * 2
        g2d.draw(new Ellipse2D.Double(circleCenter[0].subtract(circleRadius).doubleValue(),
                circleCenter[1].subtract(circleRadius).doubleValue(), circleRadius.multiply(new BigDecimal(2)).doubleValue(),
                circleRadius.multiply(new BigDecimal(2)).doubleValue()));

        // Get a rectangular conversion of a point on the circle at this angle
        BigDecimal angle = new BigDecimal(0.34117696217);
        BigDecimal[] rectangular = convertPolarToRectangular(new BigDecimal[] {
                circleRadius, angle });

        // Draw a line from the center of the circle to the point
        g2d.draw(new Line2D.Double(circleCenter[0].doubleValue(), circleCenter[1].doubleValue(),
                circleCenter[0].add(rectangular[0]).doubleValue(), circleCenter[1]
                        .add(rectangular[1]).doubleValue()));
    }

    public BigDecimal[] convertPolarToRectangular(BigDecimal[] polar) {
        BigDecimal radius = polar[0];
        BigDecimal angle = polar[1];
        BigDecimal x = radius.multiply(new BigDecimal(Math.cos(angle.doubleValue())));
        BigDecimal y = radius.multiply(new BigDecimal(Math.sin(angle.doubleValue())));
        return new BigDecimal[] { x, y };
    }
}

上面的代码实际上在屏幕上画了一个圆圈,非常远,半径很大。我选择了这个尺寸,这样在小窗口中就可以看到圆圈的一部分。

然后画一条线,从圆心到在窗口可见的圆上的一个点:我选择了一个在窗口上可见的角度,用几何把这个角度和圆圈半径转换成直角坐标。

这是程序显示的内容:

注意,这条线实际上并没有接触到椭圆。现在,我决定我必须找出是我计算的点还是椭圆是不正确的。我在计算器上算了一下,发现这条线是正确的,椭圆是不正确的:

考虑到计算器可能没有错,我不得不相信Ellipse2D绘制得不正确。然而,我尝试了许多其他的角度,这就是我发现的模式:

这让我相信计算是错误的。

所以这就是我的问题。我应该使用Ellipse2D以外的其他东西吗?也许Ellipse2D不够精确?我在代码示例中使用了BigDecimals,因为我认为它会给我更高的精度--这是错误的方法吗?我的最终目标是能够在特定的角度上计算椭圆上一个点的矩形位置。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-12 01:04:26

您会看到这个错误,因为Ellipse2D是由四条三次曲线近似的。要确保只需查看其定义形状边框的路径迭代器:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/geom/EllipseIterator.java#187

为了提高质量,我们应该用更多的三次曲线来逼近椭圆。下面是标准java实现的扩展,其中有可变的段数:

代码语言:javascript
复制
class BetterEllipse extends Ellipse2D.Double {
    private int segments;

    public BetterEllipse(int segments, double x, double y, double w, double h) {
        super(x, y, w, h);
        this.segments = segments;
    }

    public int getSegments() {
        return segments;
    }

    @Override
    public PathIterator getPathIterator(final AffineTransform affine) {
        return new PathIterator() {
            private int index = 0;

            @Override
            public void next() {
                index++;
            }

            @Override
            public int getWindingRule() {
                return WIND_NON_ZERO;
            }

            @Override
            public boolean isDone() {
                return index > getSegments() + 1;
            }

            @Override
            public int currentSegment(double[] coords) {
                int count = getSegments();
                if (index > count)
                    return SEG_CLOSE;
                BetterEllipse ellipse = BetterEllipse.this;
                double x = ellipse.getCenterX() + Math.sin(2 * Math.PI * index / count) * ellipse.getWidth() / 2;
                double y = ellipse.getCenterY() + Math.cos(2 * Math.PI * index / count) * ellipse.getHeight() / 2;
                if (index == 0) {
                    coords[0] = x;
                    coords[1] = y;
                    if (affine != null)
                        affine.transform(coords, 0, coords, 0, 1);
                    return SEG_MOVETO;
                }
                double x0 = ellipse.getCenterX() + Math.sin(2 * Math.PI * (index - 2) / count) * ellipse.getWidth() / 2;
                double y0 = ellipse.getCenterY() + Math.cos(2 * Math.PI * (index - 2) / count) * ellipse.getHeight() / 2;
                double x1 = ellipse.getCenterX() + Math.sin(2 * Math.PI * (index - 1) / count) * ellipse.getWidth() / 2;
                double y1 = ellipse.getCenterY() + Math.cos(2 * Math.PI * (index - 1) / count) * ellipse.getHeight() / 2;
                double x2 = x;
                double y2 = y;
                double x3 = ellipse.getCenterX() + Math.sin(2 * Math.PI * (index + 1) / count) * ellipse.getWidth() / 2;
                double y3 = ellipse.getCenterY() + Math.cos(2 * Math.PI * (index + 1) / count) * ellipse.getHeight() / 2;
                double x1ctrl = x1 + (x2 - x0) / 6;
                double y1ctrl = y1 + (y2 - y0) / 6;
                double x2ctrl = x2 + (x1 - x3) / 6;
                double y2ctrl = y2 + (y1 - y3) / 6;
                coords[0] = x1ctrl;
                coords[1] = y1ctrl;
                coords[2] = x2ctrl;
                coords[3] = y2ctrl;
                coords[4] = x2;
                coords[5] = y2;
                if (affine != null)
                    affine.transform(coords, 0, coords, 0, 3);
                return SEG_CUBICTO;
            }

            @Override
            public int currentSegment(float[] coords) {
                double[] temp = new double[6];
                int ret = currentSegment(temp);
                for (int i = 0; i < coords.length; i++)
                    coords[i] = (float)temp[i];
                return ret;
            }
        };
    }
}

下面是如何在代码中使用它而不是标准代码(我在这里使用了100个段):

代码语言:javascript
复制
    g2d.draw(new BetterEllipse(100, circleCenter[0].subtract(circleRadius).doubleValue(),
            circleCenter[1].subtract(circleRadius).doubleValue(), circleRadius.multiply(new BigDecimal(2)).doubleValue(),
            circleRadius.multiply(new BigDecimal(2)).doubleValue()));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33071206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档