Loading...
Logo
Processing Forum
Hello,

I would like to ask your help converting processing sketch to eclipse.

I am new to eclipse, I know how to implement class to get all the functionality from the main tab from Chose Sanchez tutorials.

For learning purposes, day to day I try to rewrite other people sketches.

But, when I try to rewrite sketches I usually get errors. Errors comes not from my writing mistakes but from difference in  know, that there is difference with syntax between these 2 applications. 

Please help me make this sketch work in eclipse; 
This would be enormous help for me for now and future. I really want to learn eclipse... 

Thank you very much.

Down here is processing sketch, and below it is eclipse sketch that does not work.

Processing:
Copy code
  1. int number_of_particles;
  2. line[] lines;
  3. collision collision;


  4. void setup() {
  5.   size(1280,720,OPENGL);
  6.   frameRate(20);
  7.   background(0);
  8.   number_of_particles = 6;
  9.   lines = new line[number_of_particles];
  10.   for( int x=0; x < number_of_particles; x++) {
  11.     lines[x] = new line();
  12.     lines[x].spawn_random();
  13.   }
  14.   collision = new collision(lines);
  15. }
  16. void draw() {

  17.   
  18.   for( int x=0; x < number_of_particles; x++) {
  19.     line temp_line = lines[x].draw();
  20.     if( temp_line != null) {
  21.       lines = (line[]) append(lines,temp_line);
  22.       collision.add_line(lines[number_of_particles]);
  23.       number_of_particles ++;
  24.     }
  25.   }

  26.   collision.of_lines();
  27.  
  28. }

  29. float[] collision_line_line(float p1_x, float p1_y, float p2_x, float p2_y, float p3_x, float p3_y, float p4_x, float p4_y) {
  30.   float xD1,yD1,xD2,yD2,xD3,yD3;
  31.   float dot,deg,len1,len2;
  32.   float segmentLen1,segmentLen2;
  33.   float ua,ub,div;

  34.   // calculate differences
  35.   xD1=p2_x-p1_x;
  36.   xD2=p4_x-p3_x;
  37.   yD1=p2_y-p1_y;
  38.   yD2=p4_y-p3_y;
  39.   xD3=p1_x-p3_x;
  40.   yD3=p1_y-p3_y;  

  41.   // calculate the lengths of the two lines
  42.   len1=sqrt(xD1*xD1+yD1*yD1);
  43.   len2=sqrt(xD2*xD2+yD2*yD2);

  44.   // calculate angle between the two lines.
  45.   dot=(xD1*xD2+yD1*yD2); // dot product
  46.   deg=dot/(len1*len2);

  47.   // if abs(angle)==1 then the lines are parallell,
  48.   // so no intersection is possible
  49.   if(abs(deg)==1) return null;

  50.   // find intersection Pt between two lines
  51.   float pt_x;
  52.   float pt_y;
  53.  // Point pt=new Point(0,0);
  54.   div=yD2*xD1-xD2*yD1;
  55.   ua=(xD2*yD3-yD2*xD3)/div;
  56.   ub=(xD1*yD3-yD1*xD3)/div;
  57.   pt_x=p1_x+ua*xD1;
  58.   pt_y=p1_y+ua*yD1;

  59.   // calculate the combined length of the two segments
  60.   // between Pt-p1 and Pt-p2
  61.   xD1=pt_x-p1_x;
  62.   xD2=pt_x-p2_x;
  63.   yD1=pt_y-p1_y;
  64.   yD2=pt_y-p2_y;
  65.   segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);

  66.   // calculate the combined length of the two segments
  67.   // between Pt-p3 and Pt-p4
  68.   xD1=pt_x-p3_x;
  69.   xD2=pt_x-p4_x;
  70.   yD1=pt_y-p3_y;
  71.   yD2=pt_y-p4_y;
  72.   segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);

  73.   // if the lengths of both sets of segments are the same as
  74.   // the lenghts of the two lines the point is actually
  75.   // on the line segment.

  76.   // if the point isn't on the line, return null
  77.   if(abs(len1-segmentLen1)>0.1 || abs(len2-segmentLen2)>0.1)
  78.     return null;

  79.   // return the valid intersection

  80.   float[] output = new float[2];

  81.   output[0] = pt_x;
  82.   output[1] = pt_y;

  83.   return output;
  84. }
  85. class collision {


  86.   line[] lines= new line[0];

  87.   int total;

  88.   collision(line[] lines) {
  89.     this.lines = lines;
  90.     total = lines.length;
  91.   }

  92.   void add_line(line new_line) {
  93.     lines = (line[]) append(lines,new_line);
  94.     total++;
  95.   }

  96.   float[] temp = new float[2];

  97.   void of_lines() {


  98.     for( int x=0; x< total; x++) {

  99.       if(    (lines[x].end.x <0)||(lines[x].end.x>width)   ||   (lines[x].end.y <0)||(lines[x].end.y>height)         ) {
  100.         lines[x].collision = true;
  101.       }

  102.       for( int y=0; y< total; y++) {

  103.         if( ! lines[x].collision) {

  104.           if (x != y) {
  105.             temp =collision_line_line(lines[x].start.x,lines[x].start.y,lines[x].end.x,lines[x].end.y,lines[y].start.x,lines[y].start.y,lines[y].end.x,lines[y].end.y);

  106.             if( temp != null) {

  107.               if( ( abs(temp[0])-abs(lines[x].end.x)  < 5) && (abs(temp[1])-abs(lines[x].end.y) <5 )) {
  108.                 lines[x].collision();
  109.               }
  110.             }
  111.           }
  112.         }
  113.       }
  114.     }
  115.   }
  116. }
  117. class line {

  118.   PVector start = new PVector(); 
  119.   PVector end   = new PVector(); 

  120.   float velocity;
  121.   int way;

  122.   int spawn_base=4;
  123.   int spawn_rate=spawn_base;
  124.   int children_counter;
  125.   int children_max= 5;
  126.   float heading;
  127.   line child_line;

  128.   line() {
  129.     velocity = 2;
  130.   }

  131.   void spawn_random() {

  132.     start.x =  random(0, width);
  133.     start.y =  random(0, height);

  134.     end.x   =  start.x+random(-1, 1) ;
  135.     end.y   =  start.y+random(-1, 1) ;

  136.     heading = get_heading();
  137.   }


  138.   void spawn_location(float x, float y, float heading) {

  139.     way = round(random(-1, 1));

  140.     heading = heading+HALF_PI*way;


  141.     if (heading > TWO_PI) {
  142.       heading-=TWO_PI;
  143.     }

  144.     if (heading < -TWO_PI) {
  145.       heading+=TWO_PI;
  146.     }

  147.     this.heading=heading;


  148.     start.x = x + cos( heading)*velocity;
  149.     start.y = y + sin( heading)*velocity;

  150.     end.x =  start.x + cos( heading)*velocity;
  151.     end.y =  start.y + sin( heading)*velocity;
  152.   }




  153.   boolean spawn_timer() {

  154.     if (children_counter < children_max) {
  155.       if ( spawn_rate == 0 ) {
  156.         spawn_base *=spawn_base;
  157.         spawn_rate= spawn_base;
  158.         children_counter++;  
  159.         return true;
  160.       }
  161.       spawn_rate--;
  162.     }
  163.     return false;
  164.   }

  165.    line child_line() {

  166.     if ( (!collision)  && (spawn_timer()) ) {
  167.       child_line = new line();
  168.       child_line.spawn_location(end.x, end.y, heading);
  169.       return child_line;
  170.     }
  171.     else {
  172.       return null;
  173.     }
  174.   }

  175.   line draw() {
  176.     if ( ! collision) {

  177.       stroke(255);

  178.       line(start.x, start.y, end.x, end.y);
  179.       grow();
  180.       return child_line();
  181.     }
  182.     line(start.x, start.y, end.x, end.y);
  183.     return null;
  184.   }


  185.   float get_heading() {
  186.     return atan2(  start.y - end.y, start.x - end.x);
  187.   }

  188.   void grow() {

  189.     end.x +=  cos(heading)* (velocity);
  190.     end.y +=  sin(heading)* (velocity);
  191.   }

  192.   boolean  collision = false;

  193.   void collision() {
  194.     collision = true;
  195.   }
  196. }

Eclipse code:


Copy code
  1. package lines;

  2. import processing.core.PApplet;


  3. public class Lines extends PApplet {
  4. int number_of_particles = 6;
  5. Line[] lines;
  6. collision collission;
  7. public void setup() {
  8. size(1280,720,P3D);
  9. background(0);
  10. smooth();
  11. lines = new Line[number_of_particles];
  12. for (int x = 0; x < number_of_particles; x++) {
  13. lines[x] = new Line();
  14. lines[x].spawn_random();
  15. }
  16. collission = new collision(lines);
  17. }

  18. public void draw() {
  19. for(int x = 0; x < number_of_particles; x++){
  20. Line temp_line = lines[x].draw();
  21. if ( temp_line != null) {
  22. lines = (Line[]) append(lines,temp_line);
  23. collision.add_line(lines[number_of_particles]);
  24. number_of_particles ++; /////////////////////////////  del to stringa?
  25. }
  26. }
  27. collsion.of_lines();
  28. }
  29. float[] collision_line_line(float p1_x, float p1_y, float p2_x, float p2_y, float p3_x, float p3_y, float p4_x, float p4_y){
  30. float xD1,yD1,xD2,yD2,xD3,yD3;
  31. float dot,deg,len1,len2;
  32. float segmentLen1,segmentLen2;
  33. float ua,ub,div;
  34.  // calculate differences
  35.  xD1=p2_x-p1_x;
  36.  xD2=p4_x-p3_x;
  37.  yD1=p2_y-p1_y;
  38.  yD2=p4_y-p3_y;
  39.  xD3=p1_x-p3_x;
  40.  yD3=p1_y-p3_y; 
  41.  
  42.  //calculate the lengths of the two lines
  43.  len1 = sqrt(xD1*xD1+yD1*yD1);
  44.  len2 = sqrt(xD2*xD2+yD2*yD2);
  45.  
  46.  //calculate angle between the two lines
  47.  dot=(xD1*xD2+yD1*yD2); // dot product
  48.  deg=dot/(len1*len2);
  49.  
  50.  // if abs(angle) == 1 then the lines are parallell,
  51.  // if no -> intersection is possible
  52.  
  53.  if(abs(deg)==1) return null;
  54.  
  55.  //find intersection Pt between two lines;
  56.  float pt_x;
  57.  float pt_y;
  58.  
  59.  div=yD2*xD1-xD2*yD1;
  60.  ua=(xD2*yD3-yD2*xD3)/div;
  61.  ub=(xD1*yD3-yD1*xD3)/div;
  62.  pt_x=p1_x+ua*xD1;
  63.  pt_y=p1_y+ua*yD1;
  64.  
  65.  //calculate the combined length of the two segments: Pt-p1 and Pt-p2
  66.  xD1=pt_x-p1_x;
  67.  xD2=pt_x-p2_x;
  68.  yD1=pt_y-p1_y;
  69.  yD2=pt_y-p2_y;
  70.  segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);
  71. //calculate the combined length of the two segments: Pt-p1 and Pt-p2
  72.  xD1=pt_x-p3_x;
  73.  xD2=pt_x-p4_x;
  74.  yD1=pt_y-p3_y;
  75.  yD2=pt_y-p4_y;
  76.  segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);
  77.  
  78.  // if the lengths of both sets of segments are the same as
  79.  // the lengths of the two lines the point is actually
  80.  // on the line segment
  81.  
  82.  //if the points is not on the line, return null
  83.  
  84.  if(abs(len1-segmentLen1)>0.01 || abs(len2-segmentLen2)>0.01)
  85.    return null;
  86.  
  87.  //return the valid intersection
  88.  
  89.  float[] output = new float[2];
  90.  
  91.  output[0] = pt_x;
  92.  output[1] = pt_y;
  93.  
  94.  return output;  
  95. }
  96. }
class Line:
Copy code
  1. package lines;

  2. import processing.core.PVector;

  3. public class Line {

  4. Lines e;
  5. PVector start = new PVector();
  6. PVector end = new PVector();
  7. float velocity;
  8. int way;
  9.     int spawn_base=4;
  10.     int spawn_rate=spawn_base;
  11.     int children_counter;
  12.     int children_max= 5;
  13.     float heading;
  14.     Line child_line;
  15.     
  16. Line(Lines _e){
  17. e = _e;
  18. velocity = 2;
  19. }
  20. void spawn_random(){
  21. start.x = e.random(0,e.width);
  22. start.y = e.random(0,e.height);
  23. end.x = start.x+e.random(-1,1);
  24. end.y = start.y+e.random(-1,1);
  25. heading = get_heading();  
  26. }
  27. void spawn_location(float x, float y, float heading){
  28. way = e.round(e.random(-1,1));
  29. heading = e.heading+e.HALF_PI*way;
  30. if (heading>e.TWO_PI){
  31. heading-=e.TWO_PI;
  32. }
  33. if (heading<-e.TWO_PI){
  34. heading+=e.TWO_PI;
  35. }
  36. this.heading = heading;
  37. start.x = x + e.cos( heading)*velocity;
  38. start.y = y + e.sin( heading)*velocity;

  39. end.x =  start.x + e.cos( heading)*velocity;
  40. end.y =  start.y + e.sin( heading)*velocity;
  41. }
  42. boolean spawn_timer() {
  43. if(children_counter < children_max){
  44. if(spawn_rate == 0){
  45. spawn_base *= spawn_base;
  46. spawn_rate = spawn_base;
  47. children_counter++;
  48. return true;
  49. }
  50. spawn_rate--;
  51. }
  52. return false;
  53. }
  54. Line child_line() {

  55.    if ( (!collision)  && (spawn_timer()) ) {
  56.      child_line = new Line();
  57.      child_line.spawn_location(end.x, end.y, heading);
  58.      return child_line;
  59.    }
  60.    else {
  61.      return null;
  62.    }
  63.  }
  64.  
  65. Line draw() {
  66.    if ( ! collision) {

  67.      e.stroke(255);

  68.      Line(start.x, start.y, end.x, end.y);
  69.      grow();
  70.      return child_line();
  71.    }
  72.    Line(start.x, start.y, end.x, end.y);
  73.    return null;
  74.  }


  75.  float get_heading() {
  76.    return e.atan2(  start.y - end.y, start.x - end.x);
  77.  }

  78.  void grow() {

  79.    end.x +=  e.cos(heading)* (velocity);
  80.    end.y +=  e.sin(heading)* (velocity);
  81.  }

  82.  boolean  collision = false;

  83.  void collision() {
  84.    collision = true;
  85.  }
  86. }
class collide:


Copy code
  1. class collision {


  2.   line[] lines= new line[0];

  3.   int total;

  4.   collision(line[] lines) {
  5.     this.lines = lines;
  6.     total = lines.length;
  7.   }

  8.   void add_line(line new_line) {
  9.     lines = (line[]) append(lines,new_line);
  10.     total++;
  11.   }

  12.   float[] temp = new float[2];

  13.   void of_lines() {


  14.     for( int x=0; x< total; x++) {

  15.       if(    (lines[x].end.x <0)||(lines[x].end.x>width)   ||   (lines[x].end.y <0)||(lines[x].end.y>height)         ) {
  16.         lines[x].collision = true;
  17.       }

  18.       for( int y=0; y< total; y++) {

  19.         if( ! lines[x].collision) {

  20.           if (x != y) {
  21.             temp =collision_line_line(lines[x].start.x,lines[x].start.y,lines[x].end.x,lines[x].end.y,lines[y].start.x,lines[y].start.y,lines[y].end.x,lines[y].end.y);

  22.             if( temp != null) {

  23.               if( ( abs(temp[0])-abs(lines[x].end.x)  < 5) && (abs(temp[1])-abs(lines[x].end.y) <5 )) {
  24.                 lines[x].collision();
  25.               }
  26.             }
  27.           }
  28.         }
  29.       }
  30.     }
  31.   }
  32. }

Thank you.

Replies(3)

The easiest way is to export the sketch (then use the generated java file). Or what I usually do on linux is just run the sketch (from the processing ide) then pick up the generated java file from the /tmp folder (it will be somewhere else on  Mac/Windows). This generates a file with all inner classes, there is tool in Netbeans (I expect Eclipse, has such a tool) to refactor inner classes to self contained classes. Do this sort of thing a few times and you will soon get to know the different syntax.
Dear monkstone,

By this line:
.... Or what I usually do on linux is just run the sketch (from the processing ide) then pick up the generated java file from the /tmp folder (it will be somewhere else on  Mac/Windows).  ...

Do you mean:
1) Open processing and run the sketch;
2) after running the script there is temporary processing file created

Or you mean:

1) Open eclipse somehow run the script (How to run processing in eclipse?)

I tried to search through processing files, there were no temporary files.
But in eclipse I found files named:
.tmp449117427675497990.instance

Is it .tmp file you wrote?

Sorry for silly questions

When you run the sketch in the processing ide a temporary java file is created, on linux that ends up in a sub folder in the /tmp directory (actually two folders are created, one contains the java file one contains the class file). If you run the processing_java command line script, then you decide where that temporary file gets created http://wiki.processing.org/w/Command_Line.