在 n 多边形的每条边上画一个圆
Draw a circle on each edge of an n-polygon
Given a list of n
circles, each of diameter d
, I want to generate an n-gon (polygon of n lengths), with side lengths d
, and draw a circle on each of its edges.
我在开发应用程序时遇到了这个问题。
给定每条边的长度 a
,N 边多边形的半径公式为
我的代码
游戏框架
import javax.swing.JFrame;
public class gameFrame extends JFrame{
static int width = 400;
static int height = 400;
public static void main(String[] args) {
gameFrame frame = new gameFrame();
gamePanel panel = new gamePanel();
frame.add(panel);
frame.setTitle("Tutorial");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
panel.initializeList();
panel.fpsTimer.start();
}
}
游戏面板
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class gamePanel extends JPanel{
static ArrayList<Point2D> coordinates = new ArrayList<Point2D>();
static int d = 20;
Timer fpsTimer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//paint the circles with the coordinates in list 'coordinates'
for(Point2D point : coordinates) {
g2d.setColor(Color.BLACK);
Shape circle = new Arc2D.Double(point.x,
point.y,
d,
d,
0 , 360, Arc2D.CHORD);
g2d.fill(circle);
}
}
public void initializeList() {
Point2D center = new Point2D(0,0);
int n = 15; // number of sides of polygon
float alpha = 360/ n; // angle of each triangle in the polygon
float angle = 0; // incremental angle for the loop
float side = d; // desired length of each side
float radius = (float) (side / (2*Math.sin(Math.PI / n))); // R = a / (2*(sin(PI/n))) formula
//verifying that the circle radius isn't larger than the frame
if(!(radius >= gameFrame.width || radius >= gameFrame.height)) {
//center of the circle
center.x = (gameFrame.width)/2;
center.y = (gameFrame.height)/2;
for(int i = 0 ; i < n ; i++) {
coordinates.add(new Point2D((center.x + Math.sin(angle)*radius),
(center.y -Math.cos(angle)*radius)));
angle += alpha;
}
}
}
}
Point2D(为简单起见)
public class Point2D {
double x;
double y;
public Point2D(double x , double y) {
this.x = x;
this.y = y;
}
}
输出和反射
如上图n=15
和d=20
,输出为
一些圆圈重叠。
令人惊讶的是,如果我在 GamePanel initializeList()
中将 angle += alpha
修改为 angle += alpha+0.295;
,我会得到想要的结果:
对于n=14
,输出更可怕:
我在这个错误上花了很多时间。有谁知道出了什么问题吗?
Java Math 使用以弧度为单位的角度。
所以以弧度为单位计算 alpha:
double alpha = Math.toRadians(360.0/ n);
只需更新线路即可解决问题。
此外,为了更准确,对每个变量使用 double
类型而不是 float
。
Given a list of
n
circles, each of diameterd
, I want to generate an n-gon (polygon of n lengths), with side lengthsd
, and draw a circle on each of its edges.
我在开发应用程序时遇到了这个问题。
给定每条边的长度 a
,N 边多边形的半径公式为
我的代码
游戏框架
import javax.swing.JFrame;
public class gameFrame extends JFrame{
static int width = 400;
static int height = 400;
public static void main(String[] args) {
gameFrame frame = new gameFrame();
gamePanel panel = new gamePanel();
frame.add(panel);
frame.setTitle("Tutorial");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
panel.initializeList();
panel.fpsTimer.start();
}
}
游戏面板
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class gamePanel extends JPanel{
static ArrayList<Point2D> coordinates = new ArrayList<Point2D>();
static int d = 20;
Timer fpsTimer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//paint the circles with the coordinates in list 'coordinates'
for(Point2D point : coordinates) {
g2d.setColor(Color.BLACK);
Shape circle = new Arc2D.Double(point.x,
point.y,
d,
d,
0 , 360, Arc2D.CHORD);
g2d.fill(circle);
}
}
public void initializeList() {
Point2D center = new Point2D(0,0);
int n = 15; // number of sides of polygon
float alpha = 360/ n; // angle of each triangle in the polygon
float angle = 0; // incremental angle for the loop
float side = d; // desired length of each side
float radius = (float) (side / (2*Math.sin(Math.PI / n))); // R = a / (2*(sin(PI/n))) formula
//verifying that the circle radius isn't larger than the frame
if(!(radius >= gameFrame.width || radius >= gameFrame.height)) {
//center of the circle
center.x = (gameFrame.width)/2;
center.y = (gameFrame.height)/2;
for(int i = 0 ; i < n ; i++) {
coordinates.add(new Point2D((center.x + Math.sin(angle)*radius),
(center.y -Math.cos(angle)*radius)));
angle += alpha;
}
}
}
}
Point2D(为简单起见)
public class Point2D {
double x;
double y;
public Point2D(double x , double y) {
this.x = x;
this.y = y;
}
}
输出和反射
如上图n=15
和d=20
,输出为
一些圆圈重叠。
令人惊讶的是,如果我在 GamePanel initializeList()
中将 angle += alpha
修改为 angle += alpha+0.295;
,我会得到想要的结果:
对于n=14
,输出更可怕:
我在这个错误上花了很多时间。有谁知道出了什么问题吗?
Java Math 使用以弧度为单位的角度。
所以以弧度为单位计算 alpha:
double alpha = Math.toRadians(360.0/ n);
只需更新线路即可解决问题。
此外,为了更准确,对每个变量使用 double
类型而不是 float
。