如何计算等边三角形的坐标?

时间:2023-02-09 16:19:07

The goal is to tesselate a plane using either equilateral triangles, squares, or hexagons, determined by an integer read from a file. The side length is also given by reading an integer from a file. I've done it for squares, but the triangle has me stumped. I've been trying to use drawPolygon. I assume the side length will correlate to the distance from point a to point b to point c, but i really have no clue as to how to find the coordinates.

目标是使用等边三角形、正方形或六边形,用一个从文件中读取的整数来确定一个平面。通过从一个文件中读取一个整数,也可以给出边长。我已经做过方块了,但是三角形把我难住了。我一直在尝试使用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;
                }
            }
        }
    }
}

3 个解决方案

#1


1  

This sounds like a math question. Assuming you are using equilateral triangles, the height of an equilateral triangle is equal to sqrt(3) * base. Also, to make equilateral triangles tesselate, they alternate up/down, etc.

这听起来像是个数学问题。假设你使用的是等边三角形,等边三角形的高度等于√(3)*底。此外,为了使等边三角形镶嵌,他们交替上下,等等。

So if the side length is sidelength, and the first coordinate is (0,0), to draw the first triangle, do:

所以如果边长是边长,第一个坐标是(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

To draw the upside down triangle, you only need to draw two more sides:

为了画出倒过来的三角形,你只需要再画两条边:

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));

HOWEVER, this is not the best way to do it (draw each triangle by hand). It's better to just draw big lines as the lines are all attached to each other.

然而,这并不是最好的方法(手工绘制每个三角形)。最好是画一条大线,因为这些线都是相连的。

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
}

Note: you may have to do a lot of size bounds checking to get this to work correctly. But it would be faster than drawing each triangle by hand.

注意:您可能需要做大量的大小边界检查才能正确地工作。但是它比手工绘制每个三角形要快。

#2


1  

For the bottom left corner of the Triangle to be 0,0 the 3 coordinates need to be:

三角形的左下角为0 0,3坐标为:

0, 0 (bottom left)

0,0(左下)

sidelength, 0 (bottom right)

sidelength,0(右下角)

sidelength/2, sin(60)*sidelength (tip)

sidelength / 2,罪(60)* sidelength(提示)

To tesselate, the next triangle in the row will have

对于镶嵌,下一个三角形将会有。

sidelength/2, sin(60)*sidelength (top left)

sidelength / 2,罪(60)* sidelength(左上)

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

3*副业/ 2,sin(60)*副业(右上)

sidelength, 0 (tip, which is now on the bottom)

副业,0(小费,现在在底部)

Then it's really just a matter of adding an x, y to each point to move it along.

这实际上只是在每一点上加一个x, y,来移动它。

#3


0  

Create an, upright, equilateral triangle with a side of 10 and an origin, centered, of (50, 50).

创建一个,直立的,等边三角形,边为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);
}

#1


1  

This sounds like a math question. Assuming you are using equilateral triangles, the height of an equilateral triangle is equal to sqrt(3) * base. Also, to make equilateral triangles tesselate, they alternate up/down, etc.

这听起来像是个数学问题。假设你使用的是等边三角形,等边三角形的高度等于√(3)*底。此外,为了使等边三角形镶嵌,他们交替上下,等等。

So if the side length is sidelength, and the first coordinate is (0,0), to draw the first triangle, do:

所以如果边长是边长,第一个坐标是(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

To draw the upside down triangle, you only need to draw two more sides:

为了画出倒过来的三角形,你只需要再画两条边:

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));

HOWEVER, this is not the best way to do it (draw each triangle by hand). It's better to just draw big lines as the lines are all attached to each other.

然而,这并不是最好的方法(手工绘制每个三角形)。最好是画一条大线,因为这些线都是相连的。

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
}

Note: you may have to do a lot of size bounds checking to get this to work correctly. But it would be faster than drawing each triangle by hand.

注意:您可能需要做大量的大小边界检查才能正确地工作。但是它比手工绘制每个三角形要快。

#2


1  

For the bottom left corner of the Triangle to be 0,0 the 3 coordinates need to be:

三角形的左下角为0 0,3坐标为:

0, 0 (bottom left)

0,0(左下)

sidelength, 0 (bottom right)

sidelength,0(右下角)

sidelength/2, sin(60)*sidelength (tip)

sidelength / 2,罪(60)* sidelength(提示)

To tesselate, the next triangle in the row will have

对于镶嵌,下一个三角形将会有。

sidelength/2, sin(60)*sidelength (top left)

sidelength / 2,罪(60)* sidelength(左上)

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

3*副业/ 2,sin(60)*副业(右上)

sidelength, 0 (tip, which is now on the bottom)

副业,0(小费,现在在底部)

Then it's really just a matter of adding an x, y to each point to move it along.

这实际上只是在每一点上加一个x, y,来移动它。

#3


0  

Create an, upright, equilateral triangle with a side of 10 and an origin, centered, of (50, 50).

创建一个,直立的,等边三角形,边为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);
}