1

代替テキスト配列内にいくつのアイテムがあるか保留中のピクセルの正方形を描画したいと思います。四角は配列の量を表すため、小さな四角は小さな配列を表し、大きな四角は大きな配列を表します。私がこれをどのように行うかを概念化するのは難しいと思いますか?

編集: Java 2D を使用しています。

らせんは 1 から始まり、正方形の外側に向かって反時計回りに進みます (つまり、2、3、4、5 など)。各正方形は、正方形が表すデータの量で表すことができます。

4

1 に答える 1

4
public class Test {

    enum Direction {
        Right,
        Up,
        Left,
        Down
    }

    public static void main(String... args) throws IOException {

        BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

        int rgb = Color.BLACK.getRGB();

        Point p = new Point(50, 50);
        Direction d = Direction.Right;
        int currentSegmentLength = 1;


        for (int i = 0; i < 100; i += 2) {

            paintSegment(image, rgb, p, d, currentSegmentLength);
            d = nextSegmentDirection(d);

            paintSegment(image, rgb, p, d, currentSegmentLength);
            d = nextSegmentDirection(d);

            currentSegmentLength++;
        }


        ImageIO.write(image, "png", new File("test.png"));
    }

    private static void paintSegment(BufferedImage image, int rgb, Point p,
            Direction d, int currentSegmentLength) {

        for (int s = 0; s < currentSegmentLength; s++) {
            image.setRGB(p.x, p.y, rgb);

            switch (d) {
            case Right: p.x++; break;
            case Up:    p.y--; break;
            case Left:  p.x--; break;
            case Down:  p.y++; break;
            }
        }
    }

    private static Direction nextSegmentDirection(Direction d) {
        switch (d) {
        case Right: return Direction.Up;
        case Up:    return Direction.Left;
        case Left:  return Direction.Down;
        case Down:  return Direction.Right;

        default: throw new RuntimeException("never here");
        }
    }
}
于 2010-12-01T20:39:40.687 に答える