Dragon curve/D/DFL: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{libheader|DFL}} Translation of Java <lang d>// dfl dragoncurve -gui -release -O private import dfl.all; import std.math; class DrawForm: Form { private int[] turns; ...")
 
(updated)
Line 4: Line 4:


private import dfl.all;
private import dfl.all;
import std.math;
import std.math, std.range;


class DrawForm: Form {
class DrawForm: Form {
Line 11: Line 11:
private double startingAngle;
private double startingAngle;
private double side;
private double side;
private const iter = 14;
private immutable iter = 14;


this() {
this() {
Line 27: Line 27:
}
}


public int[] getSequence(int iterations) {
public int[] getSequence(in int iterations) {
int[] turnSequence;
int[] turnSequence;
for (int i = 0; i < iterations; i++) {
foreach (_; 0 .. iterations) {
auto copy = turnSequence.dup.reverse;
auto copy = turnSequence.retro;
turnSequence ~= 1;
turnSequence ~= 1;
foreach (turn; copy) {
foreach (turn; copy) {
Line 67: Line 67:
Application.run(new DrawForm);
Application.run(new DrawForm);
}
}
catch(Object o) {
catch(Exception e) {
msgBox(o.toString(), "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);
msgBox(e.msg, "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);
result = 1;
result = 1;
}
}

Revision as of 11:07, 29 August 2013

Library: DFL

Translation of Java <lang d>// dfl dragoncurve -gui -release -O

private import dfl.all; import std.math, std.range;

class DrawForm: Form {

   private int[] turns;
   private double startingAngle;
   private double side;
   private immutable iter = 14; 
   this() {
       width = 800;
       height = 600;
       text = "DragonCurve";
       backColor = Color(0xFF, 0xFF, 0xFF);
       startPosition = FormStartPosition.CENTER_SCREEN;
       formBorderStyle = FormBorderStyle.FIXED_DIALOG;
       maximizeBox = false;
       turns = getSequence(iter);
       startingAngle = -iter * (PI / 4);
       side = 400 / pow(2, iter / 2.0);
   }
   public int[] getSequence(in int iterations) {
       int[] turnSequence;
       foreach (_; 0 .. iterations) {
           auto copy = turnSequence.retro;
           turnSequence ~= 1;
           foreach (turn; copy) {
               turnSequence ~= -turn;
           }
       }
       return turnSequence;
   }
   
   protected override void onPaint(PaintEventArgs ea) {
       super.onPaint(ea);
       
       Pen p = new Pen(Color(0, 0, 0));
       double angle = startingAngle;
       int x1 = 230, y1 = 350;
       int x2 = x1 + cast(int) (cos(angle) * side);
       int y2 = y1 + cast(int) (sin(angle) * side);
       ea.graphics.drawLine(p, x1, y1, x2, y2);
       x1 = x2;
       y1 = y2;
       foreach (turn; turns) {
           angle += turn * (PI / 2);
           x2 = x1 + cast(int) (cos(angle) * side);
           y2 = y1 + cast(int) (sin(angle) * side);
           ea.graphics.drawLine(p, x1, y1, x2, y2);
           x1 = x2;
           y1 = y2;
       }
   }

}

int main() {

   int result = 0; 
   try {
       Application.run(new DrawForm);
   }
   catch(Exception e) {
       msgBox(e.msg, "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);        
       result = 1;
   }   
   return result;

}</lang>

Screenshot: [1]