We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › svg files and line draw
Page Index Toggle Pages: 1
svg files and line draw (Read 311 times)
svg files and line draw
May 14th, 2009, 11:31am
 
Hey,
I'm new to processing, and am still adjusting.

I've been working with importing .svg files
and was wondering if i could impliment a code to connect each randomly displayed svg with a black line?
Maybe to have them follow random lines branhcing off,
eg: tree of lines, svg are the leaves.


Thanks in advance.


Quote:

import processing.pdf.*;

String default_rule = "F[+aF][+F]F[-F]";

int    default_iterations = 4;
float  default_angle = PI / 3;
float  default_angle_chaos = 0.1;
float  default_extension = 40;
float  default_extension_chaos = 0.1;

float  col_saturation = random(80,100);
float  col_brightness = random(100,100);
float  col_base = random(100);
float  color_counter = col_base;
float  color_chaos = random(1,3);
float  rotation_chaos = random(0,6);
float  col_transparency = random(60,60);

int     default_x_offset = 400;
int     default_y_offset = 400;

boolean default_draw_tips = true;

String  default_filename = "iterations-0.pdf";

PShape m;

LSystem system;

class LSystem
{
 String axiom,
        string;
 String [] rules;
 String rule;
 float [] state;
 float [][] state_stack;
 int stack_size = 0;
 int pos = 0;
 
 
 float angle = default_angle;
 float angle_chaos = default_angle_chaos;
 float extension = default_extension;
 float extension_chaos = default_extension_chaos;
 
 LSystem ()
 {
   axiom = "F";
   string = "F";
   state = new float[3];
   state[0] = 0;
   state[1] = 0;
   state[2] = 0;
   
   rule = default_rule;
   state_stack = new float[4096][3];
 }
 
 void iterate ()
 {
   this.iterate(1);
 }
 
 void iterate (int count)
 {
   for (int i = 0; i < count; i++)
   {
     String string_next = "";
     
     for (int j = 0; j < string.length(); j++)
     {
       char c = string.charAt(j);
       if (c == 'F')
       {
         string_next = string_next + rule;
       } else {
         string_next = string_next + c;
       }
     }
     string = string_next;
   }
 }


 
 void draw()
 {
     
     translate(default_x_offset, default_y_offset);
     // rotate(1 * PI);

     for (int i = 0; i < string.length(); i++)
     {
       
       this.drawSegment();
       // addition rotate
       rotate(rotation_chaos * PI);
     }
     
     
 }
 
 void drawSegment ()
 {
   noStroke();
   if (pos >= string.length())
      { return; }
   
   char c = string.charAt(pos);
   switch (c)
   {
         case 'F':
           float ext_this = extension + random(-1.0 * extension * extension_chaos, extension * extension_chaos);
           float x_delta = ext_this * sin(state[2]);
           float y_delta = ext_this * cos(state[2]);

                   
           // line(state[0], state[1], state[0] + x_delta, state[1] + y_delta);
           
           // draw shape instead of line
           fill((color_counter)%100,col_saturation,col_brightness,col_transparency);
           color_counter += random(color_chaos);
           stroke(0);
           strokeWeight(0.01);
           shape(m,state[0], state[1],60,60) ;
           noStroke();
           
           state[0] += x_delta;
           state[1] += y_delta;
           
           if (default_draw_tips)
           {
                           
               // line(state[0], state[1], state[0] + 0.1, state[1] + 0.1);
               
               // draw shape instead of line
               fill((color_counter)%100,col_saturation,col_brightness,col_transparency);
               color_counter += random(color_chaos);
               stroke(0);
               strokeWeight(0.01);
               shape(m,state[0], state[1],40,40) ;
               noStroke();
           }

           break;
         case '-':
           state[2] -= (angle + random(-1 * angle * angle_chaos, angle * angle_chaos));
           break;
         case '+':
           state[2] += (angle + random(-1 * angle * angle_chaos, angle * angle_chaos));
           break;
         case '[':
           arraycopy(state, state_stack[stack_size++]);
           break;
         case ']':
           arraycopy(state_stack[--stack_size], state);
           break;
   }
   
   pos++;
 }
}

void setup ()
{
 int iterations = default_iterations;

 size(1280,800);
 background(250);
 frameRate(50);
 smooth();
 colorMode(HSB, 100);
 stroke(250);
 strokeWeight(0);
 
 // begin PDF creation - uncomment next line
 // beginRecord(PDF, default_filename);
 
 // load shape
 m = loadShape("dl2.svg");   //lade dein Bild
 m.disableStyle();
 shapeMode(CENTER);
 

 
 system = new LSystem();
 system.iterate(iterations);
 
 system.draw();
 
 // end PDF creation - uncomment next line
 // endRecord();
}

void _draw ()
{
 // To draw segment by segment, rename this routine to draw()
 // and remove system.draw() from setup().
 translate(default_x_offset, default_y_offset);
 // rotate(1.5 * PI);

 system.draw();
 for (int i = 0; i < random(10, 30); i++)
 {
   system.drawSegment();
 }
}


Page Index Toggle Pages: 1