Pentagram

From Rosetta Code
Pentagram is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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, and background.

For the fill it should be assumed that all points inside the triangles and the pentagon are inside the pentagram.


See also

J

Probably the simplest approach is:

<lang j>require'plot' 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

Works with: Java version 8

<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 200 600

/n 5 def % 5-star; can be set to other odd numbers

/s { gsave } def /r { grestore } def /g { .7 setgray } def /t { 100 exch translate } def /p { 180 90 n div sub rotate 0 0 moveto n { 0 160 rlineto 180 180 n div sub rotate } repeat closepath } def

s 570 t p s g eofill r stroke r % even-odd fill s 370 t p s g fill r stroke r % non-zero fill s 170 t p s 2 setlinewidth stroke r g fill r % non-zero, but hide inner strokes

%%EOF</lang>

The following isn't exactly what the task asks for, but it's kind of fun if you have a PS interpreter that progressively updates. The program draws a lot of stars, so it's extremely likely that some of them are pentagrams... <lang postscript>%!PS-Adobe-3.0 EPSF %%BoundingBox: 0 0 400 400

% randomly choose from 5- to 35-stars /maxpoint 35 def /minpoint 5 def /maxradius 30 def

/rnd1 { rand 16#80000000 div } def /rnd { rnd1 mul} def /rndi { 2 index sub rnd1 mul 1 index div cvi mul add} def /line { rotate 0 rlineto } def

/star { gsave /n minpoint 2 maxpoint rndi def /r maxradius rnd def /a 180 180 n div sub def /b 360 a n mul sub n div def

400 rnd 400 rnd translate 360 rnd rotate 0 0 moveto n { r a line r b line } repeat closepath rnd1 rnd1 rnd1 3 { 2 index 1 exch sub } repeat gsave setrgbcolor fill grestore setrgbcolor stroke grestore } def

0 setlinewidth 2000 {star} repeat showpage %%EOF</lang>

Python

Works with: Python version 3.4.1

<lang python>import turtle

turtle.bgcolor("green") t = turtle.Turtle() t.color("red", "blue") t.begin_fill() for i in range(0, 5):

   t.forward(200)
   t.right(144)

t.end_fill()</lang>

Racket

<lang racket>#lang racket (require 2htdp/image)

(overlay

(star-polygon 100 5 2 "outline" (make-pen "blue" 4 "solid" "round" "round"))
(star-polygon 100 5 2 "solid" "cyan"))</lang>

Tcl

This implementation draws a simple pentagram on a Canvas widget. <lang Tcl> package require Tk 8.6 ;# lmap is new in Tcl/Tk 8.6

set pi [expr 4*atan(1)]

pack [canvas .c] -expand yes -fill both  ;# create the canvas

update  ;# draw everything so the dimensions are accurate

set w [winfo width .c]  ;# calculate appropriate dimensions set h [winfo height .c] set r [expr {min($w,$h) * 0.45}]

set points [lmap n {0 1 2 3 4 5} {

   set n [expr {$n * 2}]
   set y [expr {sin($pi * 2 * $n / 5) * $r + $h / 2}]
   set x [expr {cos($pi * 2 * $n / 5) * $r + $w / 2}]
   list $x $y

}] set points [concat {*}$points]  ;# flatten the list

puts [.c create line $points]

  1. a fun reader exercise is to make the shape respond to mouse events,
  2. or animate it!

</lang>