2

目标是使用等边三角形、正方形或六边形对平面进行镶嵌,由从文件中读取的整数确定。边长也是通过从文件中读取一个整数来给出的。我已经为正方形做过,但三角形让我难过。我一直在尝试使用drawPolygon. 我假设边长将与从点 a 到点 b 到点 c 的距离相关,但我真的不知道如何找到坐标。

import java.awt.*;
import javax.swing.JPanel;

public class DrawShapes extends JPanel {

    private int shape;  //user choice of which shape to draw
    private int sideLength; //user choice of what length to draw the sides

    public DrawShapes(int shapeChoice, int sideChoice) {
        shape = shapeChoice;
        sideLength = sideChoice;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                switch(shape) {
                    case 3: //Draws triangles
                        //int[] x = 
                        //int[] y = 
                        //int[] n =
                        g.setColor(Color.green);
                        //g.drawPolygon(x,y,n);
                        break;

                    case 4: //Draws squares
                        g.setColor(Color.blue);
                        g.drawRect(sideLength*i, sideLength*j, sideLength, sideLength);
                        break;

                    case 6: //Draws hexagons
                        break;
                }
            }
        }
    }
}
4

3 回答 3

1

这听起来像是一道数学题。假设您使用等边三角形,等边三角形的高度等于sqrt(3) * base。此外,为了使等边三角形镶嵌,它们交替上/下等。

因此,如果边长为sidelength,第一个坐标为(0,0),要绘制第一个三角形,请执行以下操作:

g.drawLine(0, 0, sidelength, 0); // the top
g.drawLine(0, 0, sidelength/2, sidelength/2 * sqrt(3)); // left side
g.drawLine(sidelength, 0, sidelength/2, sidelength/2 * sqrt(3)); // right side

要绘制倒三角形,您只需要再画两条边:

g.drawLine(sidelength, sidelength/2 * sqrt(3), 3 * sidelength/2, sidelength/2 * sqrt(3));
g.drawLine(sidelength, 0, 3 * sidelength/2, sidelength/2 * sqrt(3));

但是,这不是最好的方法(手工绘制每个三角形)。最好只画大线,因为这些线都是相互连接的。

private static final double srqtThree = sqrt(3)/2d;
private Dimension d = new Dimension();
private int rootThree;
public DrawShapes(int sideLength) {
    rootThree = (int) sideLength * sqrt(3)/2d;
}
public void paintComponent(Graphics g){
    getSize(d);
    int y = 0;
    while(y < d.height) {
        // Draw the horizontal line
        g.drawLine(0, y, d.width, y);
        y = y + rootThree;
    }

    // Figure this out mostly yourself, but now you draw the angled sides.
    // Use this to get started. This is the down-to-right line
    g.drawLine(0, 0, d.width / sqrtThree, d.height);
    g.drawLine(sideLength, 0, sideLength + (d.width / sqrtThree), d.height);-

    // Now draw the down-to-left line
}

注意:您可能需要进行大量大小范围检查才能使其正常工作。但这比手工绘制每个三角形要快。

于 2013-09-06T16:33:03.397 回答
1

要使三角形的左下角为 0,0,则 3 个坐标必须为:

0, 0 (bottom left)

边长,0 (bottom right)

边长/2, sin(60)*边长(tip)

要进行镶嵌,该行中的下一个三角形将具有

边长/2, sin(60)*边长(top left)

3*sidelength / 2, sin(60)*sidelength(top right)

边长,0(tip, which is now on the bottom)

然后实际上只是在每个点上添加一个 x, y 以使其移动。

于 2013-09-06T16:25:26.983 回答
0

创建一个直立的等边三角形,其边为10,原点为 ,中心为(50, 50)

g.fillPolygon(createTriange(50, 50, 10, false));
private Polygon createPolygon(float x, float y, float side, boolean invert) {
    float xOff = side / 2f;
    float yOff = (float) (xOff * Math.sqrt(3));

    float r1 = 1f / 3f;
    float r2 = 2f / 3f;

    if (invert) { yOff *= -1; }

    Point2D.Float top   = new Point2D.Float(x,        y - (yOff * r2));
    Point2D.Float left  = new Point2D.Float(x - xOff, y + (yOff * r1));
    Point2D.Float right = new Point2D.Float(x + xOff, y + (yOff * r1));

    int xCoords[] = { (int) top.x, (int) left.x, (int) right.x, (int) top.x };
    int yCoords[] = { (int) top.y, (int) left.y, (int) right.y, (int) top.y };

    return new Polygon(xCoords, yCoords, xCoords.length);
}
于 2016-02-10T12:04:08.930 回答