【发布时间】:2021-02-23 00:28:00
【问题描述】:
所以我尝试使用点 rsin(theta) 和 rcos(theta) 公式制作六边形,但公式中似乎有一些错误。当把它写在纸上时,公式有效,但感觉好像我忽略了一些东西......
public static void drawHexagon(double len, double xc, double yc, int angle)
{
if(angle <= -360)
angle = 0;
double rotAngX = (len*Math.cos(angle));
double rotAngY = (len*Math.sin(angle));
//System.out.println(rotAngX + " " + rotAngY + " " + Math.cos(-30));
/*double [] x = {(xc - len*(Math.sqrt(3)/2.0)), xc,
xc + len*(Math.sqrt(3)/2.0), xc + len*(Math.sqrt(3)/2.0),
xc, xc - len*(Math.sqrt(3)/2.0)};
double [] y = {yc + len/2.0, yc + len, yc + len/2.0,
yc - len/2.0, yc - len, yc - len/2.0};*/
double[] x = new double[6];
double[] y = new double[6];
int[] angles = { 150, 90, 30, 330, 270, 210 };
for(int i = 0; i < 6; i++) {
x[i] = xc + (len*Math.cos(angles[i]));
y[i] = yc + (len*Math.sin(angles[i]));
}
printPoints(x, y);
StdDraw.setPenColor(Color.CYAN);
StdDraw.filledPolygon(x,y);
StdDraw.setPenColor(Color.PINK);
StdDraw.polygon(x,y);
}
注释掉的部分有效,但我正在尝试旋转它们,因此在 for 循环中会将角度参数添加到角度 [i]。我在 for 循环中做错了什么?
这是代码的结果。六边形但不是真的:
【问题讨论】:
-
您不需要对角度进行硬编码。
for (int i = 0; i < 6; i++) { angle += 360.0 / 6; x=len*cos(angle); y=len*sin(angle); /* draw line to xc+x, yc+y */ } -
试过了,但还是没有显示为六边形
-
Math.cos 和 sin 使用弧度
-
好的,应该是
angle += 2.0 * Math.PI / 6;
标签: java user-interface geometry draw