尝试使用正弦和余弦制作星星
trying to make a star using sine and cosine
所以我想为我要画一面旗帜的节目做一个明星。我几乎只局限于 Java Applet class。由于每个具有不同中心的恒星的过程应该相同,我想我会将中心传递给具有 X 和 Y 值的方法,然后使用多边形根据与中心的角度绘制它。问题是出现的形状绝不是星星。
public void paint( Graphics g)
{
super.paint( g );
Polygon star= new Polygon();
int radius=20;
int roundX=(int)Math.round(radius*Math.cos(Math.toRadians(54)));
int roundY=(int)Math.round(radius*Math.sin(Math.toRadians(54)));
int originX=100,originY=100;
//int radius=20;
int topY=originY+radius;
int bottomLeftX=originX+roundX;
int bottomY=originY-roundY;
int middleY=originY+(int)Math.round(radius*Math.sin(Math.toRadians(18)));;
int midRightX=originX+(int)Math.round(radius*Math.cos(Math.toRadians(18)));;
int midLeftX=originX-(int)Math.round(radius*Math.cos(Math.toRadians(18)));
int bottomRightX=originX+roundX;
int bottomRightY=middleY-roundY;
star.addPoint(originX, originY);
star.addPoint(originX, topY);
star.addPoint(bottomLeftX, bottomY);
star.addPoint(midRightX, middleY);
//star.addPoint(midLeftX, middleY);
//star.addPoint(bottomRightX, bottomY);
//star.addPoint(originX, topY);
g.drawPolygon(star);
}
因此左下角的点应与中心成 234 度角或与 x 轴成 54 度角。当我对其中任何一个进行编码并更改操作(+ 或减号)时,它似乎朝着与我预期相反的方向射击。中间点应该在中心上方 18 度,尽管从外观上看它们几乎与 x 轴对齐。我一直在一个一个地禁用点,看看我是否可以调整它以更接近一颗星星,但此时我意识到这相当于在蒙着眼睛和醉酒时雕刻。
如果你能帮我找出问题所在,我将不胜感激。
也许你想要一个五角星。
private double sin(int degree) {
return Math.sin(Math.toRadians(degree));
}
private double cos(int degree) {
return Math.cos(Math.toRadians(degree));
}
@Override
public void paint(Graphics g) {
super.paint(g);
Polygon star = new Polygon();
int radius = 20;
int originX = 100;
int originY = 100;
int innerRadius = (int) Math.round(radius * sin(18) / sin(126));
Point[] points = new Point[11];
// top center
points[0] = new Point();
points[0].x = 0;
points[0].y = -radius;
// inner top left
points[1] = new Point();
points[1].x = -(int) Math.round(innerRadius * cos(54));
points[1].y = -(int) Math.round(innerRadius * sin(54));
// top left
points[2] = new Point();
points[2].x = -(int) Math.round(radius * cos(18));
points[2].y = -(int) Math.round(radius * sin(18));
// inner bottom left
points[3] = new Point();
points[3].x = -(int) Math.round(innerRadius * cos(18));
points[3].y = (int) Math.round(innerRadius * sin(18));
// bottom left
points[4] = new Point();
points[4].x = -(int) Math.round(radius * cos(54));
points[4].y = (int) Math.round(radius * sin(54));
// inner bottom center
points[5] = new Point();
points[5].x = 0;
points[5].y = innerRadius;
// bottom right
points[6] = new Point();
points[6].x = (int) Math.round(radius * cos(54));
points[6].y = (int) Math.round(radius * sin(54));
// inner bottom right
points[7] = new Point();
points[7].x = (int) Math.round(innerRadius * cos(18));
points[7].y = (int) Math.round(innerRadius * sin(18));
// top right
points[8] = new Point();
points[8].x = (int) Math.round(radius * cos(18));
points[8].y = -(int) Math.round(radius * sin(18));
// inner top right
points[9] = new Point();
points[9].x = (int) Math.round(innerRadius * cos(54));
points[9].y = -(int) Math.round(innerRadius * sin(54));
// top center
points[10] = new Point();
points[10].x = 0;
points[10].y = -radius;
for (int i = 0; i < points.length; i++) {
star.addPoint(originX + points[i].x, originY + points[i].y);
}
g.drawPolygon(star);
}
所以我想为我要画一面旗帜的节目做一个明星。我几乎只局限于 Java Applet class。由于每个具有不同中心的恒星的过程应该相同,我想我会将中心传递给具有 X 和 Y 值的方法,然后使用多边形根据与中心的角度绘制它。问题是出现的形状绝不是星星。
public void paint( Graphics g)
{
super.paint( g );
Polygon star= new Polygon();
int radius=20;
int roundX=(int)Math.round(radius*Math.cos(Math.toRadians(54)));
int roundY=(int)Math.round(radius*Math.sin(Math.toRadians(54)));
int originX=100,originY=100;
//int radius=20;
int topY=originY+radius;
int bottomLeftX=originX+roundX;
int bottomY=originY-roundY;
int middleY=originY+(int)Math.round(radius*Math.sin(Math.toRadians(18)));;
int midRightX=originX+(int)Math.round(radius*Math.cos(Math.toRadians(18)));;
int midLeftX=originX-(int)Math.round(radius*Math.cos(Math.toRadians(18)));
int bottomRightX=originX+roundX;
int bottomRightY=middleY-roundY;
star.addPoint(originX, originY);
star.addPoint(originX, topY);
star.addPoint(bottomLeftX, bottomY);
star.addPoint(midRightX, middleY);
//star.addPoint(midLeftX, middleY);
//star.addPoint(bottomRightX, bottomY);
//star.addPoint(originX, topY);
g.drawPolygon(star);
}
因此左下角的点应与中心成 234 度角或与 x 轴成 54 度角。当我对其中任何一个进行编码并更改操作(+ 或减号)时,它似乎朝着与我预期相反的方向射击。中间点应该在中心上方 18 度,尽管从外观上看它们几乎与 x 轴对齐。我一直在一个一个地禁用点,看看我是否可以调整它以更接近一颗星星,但此时我意识到这相当于在蒙着眼睛和醉酒时雕刻。
如果你能帮我找出问题所在,我将不胜感激。
也许你想要一个五角星。
private double sin(int degree) {
return Math.sin(Math.toRadians(degree));
}
private double cos(int degree) {
return Math.cos(Math.toRadians(degree));
}
@Override
public void paint(Graphics g) {
super.paint(g);
Polygon star = new Polygon();
int radius = 20;
int originX = 100;
int originY = 100;
int innerRadius = (int) Math.round(radius * sin(18) / sin(126));
Point[] points = new Point[11];
// top center
points[0] = new Point();
points[0].x = 0;
points[0].y = -radius;
// inner top left
points[1] = new Point();
points[1].x = -(int) Math.round(innerRadius * cos(54));
points[1].y = -(int) Math.round(innerRadius * sin(54));
// top left
points[2] = new Point();
points[2].x = -(int) Math.round(radius * cos(18));
points[2].y = -(int) Math.round(radius * sin(18));
// inner bottom left
points[3] = new Point();
points[3].x = -(int) Math.round(innerRadius * cos(18));
points[3].y = (int) Math.round(innerRadius * sin(18));
// bottom left
points[4] = new Point();
points[4].x = -(int) Math.round(radius * cos(54));
points[4].y = (int) Math.round(radius * sin(54));
// inner bottom center
points[5] = new Point();
points[5].x = 0;
points[5].y = innerRadius;
// bottom right
points[6] = new Point();
points[6].x = (int) Math.round(radius * cos(54));
points[6].y = (int) Math.round(radius * sin(54));
// inner bottom right
points[7] = new Point();
points[7].x = (int) Math.round(innerRadius * cos(18));
points[7].y = (int) Math.round(innerRadius * sin(18));
// top right
points[8] = new Point();
points[8].x = (int) Math.round(radius * cos(18));
points[8].y = -(int) Math.round(radius * sin(18));
// inner top right
points[9] = new Point();
points[9].x = (int) Math.round(innerRadius * cos(54));
points[9].y = -(int) Math.round(innerRadius * sin(54));
// top center
points[10] = new Point();
points[10].x = 0;
points[10].y = -radius;
for (int i = 0; i < points.length; i++) {
star.addPoint(originX + points[i].x, originY + points[i].y);
}
g.drawPolygon(star);
}