Pentagram: Difference between revisions
(→{{header|Java}}: no need actually for recursion) |
m (J) |
||
Line 14: | Line 14: | ||
See also |
See also |
||
* [http://proofsfromthebook.com/2013/08/04/angle-sum-of-a-pentagram/ Angle sum of a pentagram] |
* [http://proofsfromthebook.com/2013/08/04/angle-sum-of-a-pentagram/ Angle sum of a pentagram] |
||
=={{header|J}}== |
|||
Probably the simplest approach is: |
|||
<lang j>plot j./2 1 o./180p_1 %~ 72*i. 6</lang> |
|||
This will give a pentagram with a blue border and a white interior. |
|||
=={{header|Java}}== |
=={{header|Java}}== |
Revision as of 23:49, 21 April 2015
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle.
The vertex of each triangle, a point of the star, is 36 degrees.
The task: draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for
stroke and fill.
See also
J
Probably the simplest approach is:
<lang j>plot j./2 1 o./180p_1 %~ 72*i. 6</lang>
This will give a pentagram with a blue border and a white interior.
Java
<lang java>import java.awt.*; import java.awt.geom.Path2D; import javax.swing.*;
public class Pentagram extends JPanel {
final double degrees144 = Math.toRadians(144);
public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Pentagram"); f.setResizable(false); f.add(new Pentagram(), BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); }); }
public Pentagram() { setPreferredSize(new Dimension(640, 640)); setBackground(Color.white); }
private void drawPentagram(Graphics2D g, int len, int x1, int y1) { double angle = 0;
for (int i = 0; i < 5; i++) { int x2 = x1 + (int) (Math.cos(angle) * len); int y2 = y1 + (int) (Math.sin(-angle) * len); g.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2; angle -= degrees144; } }
private void fillPentagram(Graphics2D g, int len, int x, int y) { double angle = 0;
Path2D.Float p = new Path2D.Float(); p.moveTo(x, y);
for (int i = 0; i < 10; i++) {
x = x + (int) (Math.cos(angle) * len); y = y + (int) (Math.sin(-angle) * len); p.lineTo(x, y);
angle += (i % 2 == 0 ? 0.5 : -1) * degrees144; }
p.closePath(); g.fill(p); }
@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(5, BasicStroke.CAP_ROUND, 0));
g.setColor(new Color(0xFFD700)); // gold fillPentagram(g, 190, 72, 250);
g.setColor(Color.darkGray); drawPentagram(g, 500, 70, 250); }
}</lang>
PostScript
<lang postscript>%!PS-Adobe-3.0 EPSF %%BoundingBox: 0 0 512 512
/n 5 def % 5-star; can be set to other odd numbers
256 440 translate 180 90 n div sub rotate
0 0 moveto n 1 sub { 0 400 rlineto 180 180 n div sub rotate } repeat closepath
gsave .5 setgray eofill grestore stroke
%%EOF</lang>