将Ellipse2D转换为多边形

时间:2013-06-24 10:09:26

标签: java swing geometry shapes ellipse

我有一个Java swing应用程序,我可以在其中绘制热点。我允许用户绘制矩形,多边形和圆形。

对于Circle我正在使用Ellipse2D

Ellipse2D.Double ellipseDouble = new Ellipse2D.Double(x,y,width,height);
        g.draw(ellipseDouble);

以上工作正常,它会画一个椭圆/圆圈。

现在我希望在HTML Image map中使用该区域的问题。

Html图像映射不支持Ellipse,所以我想为Ellipse2D使用多边形,但我真的不知道如何转换它。

有谁知道如何将Ellipse2D转换为Polygon ponits?

2 个答案:

答案 0 :(得分:3)

使用FlatteningPathIterator。 参见例如http://java-sl.com/tip_flatteningpathiterator_moving_shape.html点在自定义Shape之后移动。

您可以获取Points列表并创建Polygon

答案 1 :(得分:0)

也许有人会发现这个有用:这是pdfbox椭圆或圆(宽度=高度)在矩形内绘制函数,它最初将椭圆作为多边形绘制。

基于点[0,0]的椭圆数学函数的代码:x ^ 2 / a ^ 2 + y ^ 2 / b ^ 2 = 1

private  PdfBoxPoligon draw_Ellipse_or_Circle_as_poligon_with_PDFBOX (
        PDPageContentStream content, float bottomLeftX, float bottomLeftY,
        float width, float height, boolean draw) throws IOException {
    PdfBoxPoligon result = new PdfBoxPoligon();


    float a = width/2;
    float b = height/2;

    int  points =    (int) (a*b/20);


    if (DEBUG) {
        System.out.println("points=" + points);
    }

    //top arc
    for (float x = -a; x < a; x = x + a / points) {
        result.x.add(bottomLeftX + a + x);
        float y = (float) Math.sqrt((1-(x*x)/(a*a))*(b*b));
        result.y.add(bottomLeftY+b+y);
    }

    //bottom arc
    for (float x = a; x >= -a; x = x - a / points) {
        result.x.add(bottomLeftX + a + x);
        float y = -(float) Math.sqrt((1-(x*x)/(a*a))*(b*b));
        result.y.add(bottomLeftY+b+y);
    }

    result.x.add(result.x.get(0));
    result.y.add(result.y.get(0));

    if (draw) {
        for (int i=1; i < result.x.size(); i++) {
            content.addLine(result.x.get(i-1), result.y.get(i-1), result.x.get(i), result.y.get(i));
        }
    }


    return result;
}