Polyspiral: Difference between revisions
(→{{header|Java}}: added Java) |
(J draft) |
||
Line 22: | Line 22: | ||
If animation is not practical in your programming environment, you may show 3 frames from different stages of the animation instead. |
If animation is not practical in your programming environment, you may show 3 frames from different stages of the animation instead. |
||
=={{header|J}}== |
|||
[[File:J-polyspiral.gif|200px|thumb|right]] |
|||
I think this is a translation of Java. |
|||
<lang J>require 'gl2 trig media/imagekit' |
|||
coinsert 'jgl2' |
|||
DT =: %30 NB. seconds |
|||
ANGLE =: 0.025p1 NB. radians |
|||
DIRECTION=: 0 NB. radians |
|||
POLY=: noun define |
|||
pc poly;pn "Poly Spiral"; |
|||
minwh 320 320; cc isi isigraph; |
|||
) |
|||
poly_run=: verb define |
|||
wd POLY,'pshow' |
|||
wd 'timer ',":DT * 1000 |
|||
) |
|||
poly_close=: verb define |
|||
wd 'timer 0; pclose' |
|||
) |
|||
sys_timer_z_=: verb define |
|||
recalcAngle_base_ '' |
|||
wd 'psel poly; set isi invalid' |
|||
) |
|||
poly_isi_paint=: verb define |
|||
drawPolyspiral DIRECTION |
|||
) |
|||
recalcAngle=: verb define |
|||
DIRECTION=: 2p1 | DIRECTION + ANGLE |
|||
) |
|||
drawPolyspiral=: verb define |
|||
glclear'' |
|||
x1y1 =. (glqwh'')%2 |
|||
a=. -DIRECTION |
|||
len=. 5 |
|||
for_i. i.150 do. |
|||
glpen glrgb Hue a % 2p1 |
|||
x2y2=. x1y1 + len*(cos,sin) a |
|||
gllines <.x1y1,x2y2 |
|||
x1y1=. x2y2 |
|||
len=. len+3 |
|||
a=. 2p1 | a - DIRECTION |
|||
end. |
|||
) |
|||
poly_run''</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
Revision as of 04:02, 9 March 2016
A Polyspiral is a spiral made of multiple line segments, whereby each segment is larger (or smaller) than the previous one by a given amount. Each segment also changes direction at a given angle.
The task: animate a series of polyspirals, by drawing a complete spiral then incrementing the angle, and (after clearing the background) drawing the next, and so on.
WHILE true x = width / 2 y = height / 2 set length FOR 1 TO 150 drawline change direction by angle increment length ENDFOR increment angle MOD 360
If animation is not practical in your programming environment, you may show 3 frames from different stages of the animation instead.
J
I think this is a translation of Java.
<lang J>require 'gl2 trig media/imagekit' coinsert 'jgl2'
DT =: %30 NB. seconds ANGLE =: 0.025p1 NB. radians DIRECTION=: 0 NB. radians
POLY=: noun define pc poly;pn "Poly Spiral"; minwh 320 320; cc isi isigraph; )
poly_run=: verb define
wd POLY,'pshow' wd 'timer ',":DT * 1000
)
poly_close=: verb define
wd 'timer 0; pclose'
)
sys_timer_z_=: verb define
recalcAngle_base_ wd 'psel poly; set isi invalid'
)
poly_isi_paint=: verb define
drawPolyspiral DIRECTION
)
recalcAngle=: verb define
DIRECTION=: 2p1 | DIRECTION + ANGLE
)
drawPolyspiral=: verb define
glclear x1y1 =. (glqwh)%2 a=. -DIRECTION len=. 5 for_i. i.150 do. glpen glrgb Hue a % 2p1 x2y2=. x1y1 + len*(cos,sin) a gllines <.x1y1,x2y2 x1y1=. x2y2 len=. len+3 a=. 2p1 | a - DIRECTION end.
)
poly_run</lang>
Java
<lang java>import java.awt.*; import java.awt.event.ActionEvent; import javax.swing.*;
public class PolySpiral extends JPanel {
double inc = 0;
public PolySpiral() { setPreferredSize(new Dimension(640, 640)); setBackground(Color.white);
new Timer(40, (ActionEvent e) -> { inc = (inc + 0.05) % 360; repaint(); }).start(); }
void drawSpiral(Graphics2D g, int len, double angleIncrement) {
double x1 = getWidth() / 2; double y1 = getHeight() / 2; double angle = angleIncrement;
for (int i = 0; i < 150; i++) {
g.setColor(Color.getHSBColor(i / 150f, 1.0f, 1.0f));
double x2 = x1 + Math.cos(angle) * len; double y2 = y1 - Math.sin(angle) * len; g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); x1 = x2; y1 = y2;
len += 3;
angle = (angle + angleIncrement) % (Math.PI * 2); } }
@Override public void paintComponent(Graphics gg) { super.paintComponent(gg); Graphics2D g = (Graphics2D) gg; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawSpiral(g, 5, Math.toRadians(inc)); }
public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("PolySpiral"); f.setResizable(true); f.add(new PolySpiral(), BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); }); }
}</lang>