java 用可变参数绘制星形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2710065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:27:37  来源:igfitidea点击:

Drawing star shapes with variable parameters

javaswingshapes

提问by sasklacz

I have task to write program allowing users to draw stars, which can differ in size and amount of arms. When I was dealing with basic stars I was doing it with GeneralPath and tables of points :

我的任务是编写允许用户绘制星星的程序,星星的大小和数量可能不同。当我处理基本的星星时,我使用的是 GeneralPath 和点表:

     int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
     int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
     Graphics2D g2d = ( Graphics2D ) g;
     GeneralPath star = new GeneralPath();
     star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
     for ( int k = 1; k < xPoints.length; k++ )
     star.lineTo( xPoints[ k ], yPoints[ k ] );
     star.closePath();
     g2d.fill( star );

What method should I choose for drawing stars with variable inner and outer radius, as well as different amount of arms ? This is what I should obtain :

我应该选择什么方法来绘制具有可变内半径和外半径以及不同数量的臂的星星?这是我应该获得的:

alt text http://img228.imageshack.us/img228/6427/lab6c.jpg

替代文字 http://img228.imageshack.us/img228/6427/lab6c.jpg

回答by Peter Walser

Having n arms means you end up with 2n vertices, the even ones are on the outer circle, and the odd ones on the inner circle. Viewed from the center, the vertices are at evenly spaced angles (the angle is 2*PI/2*n = Pi/n). On an unit circle (r=1), the x,y coordinates of the points i=0..n is cos(x),sin(x). Multiply those coordinates with the respective radius (rOuter or rInner, depending of whether i is odd or even), and add that vector to the center of the star to get the coordinates for each vertex in the star path.

有 n 个手臂意味着你最终有 2n 个顶点,偶数个在外圈,奇数个在内圈。从中心看,顶点的角度均匀分布(角度为 2*PI/2*n = Pi/n)。在单位圆 (r=1) 上,点 i=0..n 的 x,y 坐标为 cos(x),sin(x)。将这些坐标与相应的半径(rOuter 或 rInner,取决于 i 是奇数还是偶数)相乘,然后将该向量添加到星形的中心以获得星形路径中每个顶点的坐标。

Here's the function to create a star shape with given number of arms, center coordinate and outer, inner radius:

这是创建具有给定臂数、中心坐标和外、内半径的星形的函数:

public static Shape createStar(int arms, Point center, double rOuter, double rInner) {
    double angle = Math.PI / arms;

    GeneralPath path = new GeneralPath();

    for (int i = 0; i < 2 * arms; i++) {
        double r = (i & 1) == 0 ? rOuter : rInner;
        Point2D.Double p = new Point2D.Double(
            center.x + Math.cos(i * angle) * r, 
            center.y + Math.sin(i * angle) * r);
        if (i == 0) {
            path.moveTo(p.getX(), p.getY());
        }
        else {
            path.lineTo(p.getX(), p.getY());
        }
    }
    path.closePath();
    return path;
}

回答by Eyal Schneider

I think you should use the same classes (GeneralPath), but here you should focus on how to compute the vertex coordinates.

我认为您应该使用相同的类(GeneralPath),但在这里您应该关注如何计算顶点坐标。

The first thing that comes to my mind is positioning 2N points on a circle of radius R1, centered at (0,0). Then, "strech" every odd vertex by multiplying its vector by c. The constant c should be equal to R2/R1 (i.e. the proportion of inner and outer radiuses).

我想到的第一件事是在以 (0,0) 为中心、半径为 R1 的圆上定位 2N 个点。然后,通过将其向量乘以 c 来“拉伸”每个奇数顶点。常数 c 应等于 R2/R1(即内外半径的比例)。

But maybe there is a simpler solution...

但也许有一个更简单的解决方案......

回答by trashgod

Here's an exampleof finding equally spaced points on a circlethat may help. Just make the number of points, n, a parameter in the constructor.

这是在上找到等距点的示例,可能会有所帮助。只需将点数n 设为构造函数中的参数即可。

private int n;
...
public CircleTest(int n) {
    ...
    this.n = n;
}
...
for (int i = 0; i < n; i++) {
    double t = 2 * Math.PI * i / n;
    ...
}