2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,67 @@
|
|||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class LargeUlamSpiral extends JPanel {
|
||||
|
||||
public LargeUlamSpiral() {
|
||||
setPreferredSize(new Dimension(605, 605));
|
||||
setBackground(Color.white);
|
||||
}
|
||||
|
||||
private boolean isPrime(int n) {
|
||||
if (n <= 2 || n % 2 == 0)
|
||||
return n == 2;
|
||||
for (int i = 3; i * i <= n; i += 2)
|
||||
if (n % i == 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics gg) {
|
||||
super.paintComponent(gg);
|
||||
Graphics2D g = (Graphics2D) gg;
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
g.setColor(getForeground());
|
||||
|
||||
double angle = 0.0;
|
||||
int x = 300, y = 300, dx = 1, dy = 0;
|
||||
|
||||
for (int i = 1, step = 1, turn = 1; i < 40_000; i++) {
|
||||
|
||||
if (isPrime(i))
|
||||
g.fillRect(x, y, 2, 2);
|
||||
|
||||
x += dx * 3;
|
||||
y += dy * 3;
|
||||
|
||||
if (i == turn) {
|
||||
|
||||
angle += 90.0;
|
||||
|
||||
if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1))
|
||||
step++;
|
||||
|
||||
turn += step;
|
||||
|
||||
dx = (int) Math.cos(Math.toRadians(angle));
|
||||
dy = (int) Math.sin(Math.toRadians(-angle));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame f = new JFrame();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setTitle("Large Ulam Spiral");
|
||||
f.setResizable(false);
|
||||
f.add(new LargeUlamSpiral(), BorderLayout.CENTER);
|
||||
f.pack();
|
||||
f.setLocationRelativeTo(null);
|
||||
f.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class UlamSpiral extends JPanel {
|
||||
|
||||
Font primeFont = new Font("Arial", Font.BOLD, 20);
|
||||
Font compositeFont = new Font("Arial", Font.PLAIN, 16);
|
||||
|
||||
public UlamSpiral() {
|
||||
setPreferredSize(new Dimension(640, 640));
|
||||
setBackground(Color.white);
|
||||
}
|
||||
|
||||
private boolean isPrime(int n) {
|
||||
if (n <= 2 || n % 2 == 0)
|
||||
return n == 2;
|
||||
for (int i = 3; i * i <= n; i += 2)
|
||||
if (n % i == 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics gg) {
|
||||
super.paintComponent(gg);
|
||||
Graphics2D g = (Graphics2D) gg;
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
g.setStroke(new BasicStroke(2));
|
||||
|
||||
double angle = 0.0;
|
||||
int x = 280, y = 330, dx = 1, dy = 0;
|
||||
|
||||
g.setColor(getForeground());
|
||||
g.drawLine(x, y - 5, x + 50, y - 5);
|
||||
|
||||
for (int i = 1, step = 1, turn = 1; i < 100; i++) {
|
||||
|
||||
g.setColor(getBackground());
|
||||
g.fillRect(x - 5, y - 20, 30, 30);
|
||||
g.setColor(getForeground());
|
||||
g.setFont(isPrime(i) ? primeFont : compositeFont);
|
||||
g.drawString(String.valueOf(i), x + (i < 10 ? 4 : 0), y);
|
||||
|
||||
x += dx * 50;
|
||||
y += dy * 50;
|
||||
|
||||
if (i == turn) {
|
||||
angle += 90.0;
|
||||
|
||||
if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1))
|
||||
step++;
|
||||
|
||||
turn += step;
|
||||
|
||||
dx = (int) Math.cos(Math.toRadians(angle));
|
||||
dy = (int) Math.sin(Math.toRadians(-angle));
|
||||
|
||||
g.translate(9, -5);
|
||||
g.drawLine(x, y, x + dx * step * 50, y + dy * step * 50);
|
||||
g.translate(-9, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame f = new JFrame();
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setTitle("Ulam Spiral");
|
||||
f.setResizable(false);
|
||||
f.add(new UlamSpiral(), BorderLayout.CENTER);
|
||||
f.pack();
|
||||
f.setLocationRelativeTo(null);
|
||||
f.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue