Dragon curve/D/DFL

From Rosetta Code
Revision as of 20:46, 25 November 2010 by 84.24.16.179 (talk) (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; ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Library: DFL

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

private import dfl.all; import std.math;

class DrawForm: Form {

   private int[] turns;
   private double startingAngle;
   private double side;
   private const 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(int iterations) {
       int[] turnSequence;
       for (int i = 0; i < iterations; i++) {
           auto copy = turnSequence.dup.reverse;
           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(Object o) {
       msgBox(o.toString(), "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);        
       result = 1;
   }   
   return result;

}</lang>

Screenshot: [1]