Processing sketch conversion to eclipse.
in
Integration and Hardware
•
5 months ago
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:
- int number_of_particles;
- line[] lines;
- collision collision;
- void setup() {
- size(1280,720,OPENGL);
- frameRate(20);
- background(0);
- number_of_particles = 6;
- lines = new line[number_of_particles];
- for( int x=0; x < number_of_particles; x++) {
- lines[x] = new line();
- lines[x].spawn_random();
- }
- collision = new collision(lines);
- }
- void draw() {
- for( int x=0; x < number_of_particles; x++) {
- line temp_line = lines[x].draw();
- if( temp_line != null) {
- lines = (line[]) append(lines,temp_line);
- collision.add_line(lines[number_of_particles]);
- number_of_particles ++;
- }
- }
- collision.of_lines();
- }
- 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) {
- float xD1,yD1,xD2,yD2,xD3,yD3;
- float dot,deg,len1,len2;
- float segmentLen1,segmentLen2;
- float ua,ub,div;
- // calculate differences
- xD1=p2_x-p1_x;
- xD2=p4_x-p3_x;
- yD1=p2_y-p1_y;
- yD2=p4_y-p3_y;
- xD3=p1_x-p3_x;
- yD3=p1_y-p3_y;
- // calculate the lengths of the two lines
- len1=sqrt(xD1*xD1+yD1*yD1);
- len2=sqrt(xD2*xD2+yD2*yD2);
- // calculate angle between the two lines.
- dot=(xD1*xD2+yD1*yD2); // dot product
- deg=dot/(len1*len2);
- // if abs(angle)==1 then the lines are parallell,
- // so no intersection is possible
- if(abs(deg)==1) return null;
- // find intersection Pt between two lines
- float pt_x;
- float pt_y;
- // Point pt=new Point(0,0);
- div=yD2*xD1-xD2*yD1;
- ua=(xD2*yD3-yD2*xD3)/div;
- ub=(xD1*yD3-yD1*xD3)/div;
- pt_x=p1_x+ua*xD1;
- pt_y=p1_y+ua*yD1;
- // calculate the combined length of the two segments
- // between Pt-p1 and Pt-p2
- xD1=pt_x-p1_x;
- xD2=pt_x-p2_x;
- yD1=pt_y-p1_y;
- yD2=pt_y-p2_y;
- segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);
- // calculate the combined length of the two segments
- // between Pt-p3 and Pt-p4
- xD1=pt_x-p3_x;
- xD2=pt_x-p4_x;
- yD1=pt_y-p3_y;
- yD2=pt_y-p4_y;
- segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);
- // if the lengths of both sets of segments are the same as
- // the lenghts of the two lines the point is actually
- // on the line segment.
- // if the point isn't on the line, return null
- if(abs(len1-segmentLen1)>0.1 || abs(len2-segmentLen2)>0.1)
- return null;
- // return the valid intersection
- float[] output = new float[2];
- output[0] = pt_x;
- output[1] = pt_y;
- return output;
- }
- class collision {
- line[] lines= new line[0];
- int total;
- collision(line[] lines) {
- this.lines = lines;
- total = lines.length;
- }
- void add_line(line new_line) {
- lines = (line[]) append(lines,new_line);
- total++;
- }
- float[] temp = new float[2];
- void of_lines() {
- for( int x=0; x< total; x++) {
- if( (lines[x].end.x <0)||(lines[x].end.x>width) || (lines[x].end.y <0)||(lines[x].end.y>height) ) {
- lines[x].collision = true;
- }
- for( int y=0; y< total; y++) {
- if( ! lines[x].collision) {
- if (x != y) {
- 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);
- if( temp != null) {
- if( ( abs(temp[0])-abs(lines[x].end.x) < 5) && (abs(temp[1])-abs(lines[x].end.y) <5 )) {
- lines[x].collision();
- }
- }
- }
- }
- }
- }
- }
- }
- class line {
- PVector start = new PVector();
- PVector end = new PVector();
- float velocity;
- int way;
- int spawn_base=4;
- int spawn_rate=spawn_base;
- int children_counter;
- int children_max= 5;
- float heading;
- line child_line;
- line() {
- velocity = 2;
- }
- void spawn_random() {
- start.x = random(0, width);
- start.y = random(0, height);
- end.x = start.x+random(-1, 1) ;
- end.y = start.y+random(-1, 1) ;
- heading = get_heading();
- }
- void spawn_location(float x, float y, float heading) {
- way = round(random(-1, 1));
- heading = heading+HALF_PI*way;
- if (heading > TWO_PI) {
- heading-=TWO_PI;
- }
- if (heading < -TWO_PI) {
- heading+=TWO_PI;
- }
- this.heading=heading;
- start.x = x + cos( heading)*velocity;
- start.y = y + sin( heading)*velocity;
- end.x = start.x + cos( heading)*velocity;
- end.y = start.y + sin( heading)*velocity;
- }
- boolean spawn_timer() {
- if (children_counter < children_max) {
- if ( spawn_rate == 0 ) {
- spawn_base *=spawn_base;
- spawn_rate= spawn_base;
- children_counter++;
- return true;
- }
- spawn_rate--;
- }
- return false;
- }
- line child_line() {
- if ( (!collision) && (spawn_timer()) ) {
- child_line = new line();
- child_line.spawn_location(end.x, end.y, heading);
- return child_line;
- }
- else {
- return null;
- }
- }
- line draw() {
- if ( ! collision) {
- stroke(255);
- line(start.x, start.y, end.x, end.y);
- grow();
- return child_line();
- }
- line(start.x, start.y, end.x, end.y);
- return null;
- }
- float get_heading() {
- return atan2( start.y - end.y, start.x - end.x);
- }
- void grow() {
- end.x += cos(heading)* (velocity);
- end.y += sin(heading)* (velocity);
- }
- boolean collision = false;
- void collision() {
- collision = true;
- }
- }
- package lines;
- import processing.core.PApplet;
- public class Lines extends PApplet {
- int number_of_particles = 6;
- Line[] lines;
- collision collission;
- public void setup() {
- size(1280,720,P3D);
- background(0);
- smooth();
- lines = new Line[number_of_particles];
- for (int x = 0; x < number_of_particles; x++) {
- lines[x] = new Line();
- lines[x].spawn_random();
- }
- collission = new collision(lines);
- }
- public void draw() {
- for(int x = 0; x < number_of_particles; x++){
- Line temp_line = lines[x].draw();
- if ( temp_line != null) {
- lines = (Line[]) append(lines,temp_line);
- collision.add_line(lines[number_of_particles]);
- number_of_particles ++; ///////////////////////////// del to stringa?
- }
- }
- collsion.of_lines();
- }
- 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){
- float xD1,yD1,xD2,yD2,xD3,yD3;
- float dot,deg,len1,len2;
- float segmentLen1,segmentLen2;
- float ua,ub,div;
- // calculate differences
- xD1=p2_x-p1_x;
- xD2=p4_x-p3_x;
- yD1=p2_y-p1_y;
- yD2=p4_y-p3_y;
- xD3=p1_x-p3_x;
- yD3=p1_y-p3_y;
- //calculate the lengths of the two lines
- len1 = sqrt(xD1*xD1+yD1*yD1);
- len2 = sqrt(xD2*xD2+yD2*yD2);
- //calculate angle between the two lines
- dot=(xD1*xD2+yD1*yD2); // dot product
- deg=dot/(len1*len2);
- // if abs(angle) == 1 then the lines are parallell,
- // if no -> intersection is possible
- if(abs(deg)==1) return null;
- //find intersection Pt between two lines;
- float pt_x;
- float pt_y;
- div=yD2*xD1-xD2*yD1;
- ua=(xD2*yD3-yD2*xD3)/div;
- ub=(xD1*yD3-yD1*xD3)/div;
- pt_x=p1_x+ua*xD1;
- pt_y=p1_y+ua*yD1;
- //calculate the combined length of the two segments: Pt-p1 and Pt-p2
- xD1=pt_x-p1_x;
- xD2=pt_x-p2_x;
- yD1=pt_y-p1_y;
- yD2=pt_y-p2_y;
- segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);
- //calculate the combined length of the two segments: Pt-p1 and Pt-p2
- xD1=pt_x-p3_x;
- xD2=pt_x-p4_x;
- yD1=pt_y-p3_y;
- yD2=pt_y-p4_y;
- segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2);
- // if the lengths of both sets of segments are the same as
- // the lengths of the two lines the point is actually
- // on the line segment
- //if the points is not on the line, return null
- if(abs(len1-segmentLen1)>0.01 || abs(len2-segmentLen2)>0.01)
- return null;
- //return the valid intersection
- float[] output = new float[2];
- output[0] = pt_x;
- output[1] = pt_y;
- return output;
- }
- }
- package lines;
- import processing.core.PVector;
- public class Line {
- Lines e;
- PVector start = new PVector();
- PVector end = new PVector();
- float velocity;
- int way;
- int spawn_base=4;
- int spawn_rate=spawn_base;
- int children_counter;
- int children_max= 5;
- float heading;
- Line child_line;
- Line(Lines _e){
- e = _e;
- velocity = 2;
- }
- void spawn_random(){
- start.x = e.random(0,e.width);
- start.y = e.random(0,e.height);
- end.x = start.x+e.random(-1,1);
- end.y = start.y+e.random(-1,1);
- heading = get_heading();
- }
- void spawn_location(float x, float y, float heading){
- way = e.round(e.random(-1,1));
- heading = e.heading+e.HALF_PI*way;
- if (heading>e.TWO_PI){
- heading-=e.TWO_PI;
- }
- if (heading<-e.TWO_PI){
- heading+=e.TWO_PI;
- }
- this.heading = heading;
- start.x = x + e.cos( heading)*velocity;
- start.y = y + e.sin( heading)*velocity;
- end.x = start.x + e.cos( heading)*velocity;
- end.y = start.y + e.sin( heading)*velocity;
- }
- boolean spawn_timer() {
- if(children_counter < children_max){
- if(spawn_rate == 0){
- spawn_base *= spawn_base;
- spawn_rate = spawn_base;
- children_counter++;
- return true;
- }
- spawn_rate--;
- }
- return false;
- }
- Line child_line() {
- if ( (!collision) && (spawn_timer()) ) {
- child_line = new Line();
- child_line.spawn_location(end.x, end.y, heading);
- return child_line;
- }
- else {
- return null;
- }
- }
- Line draw() {
- if ( ! collision) {
- e.stroke(255);
- Line(start.x, start.y, end.x, end.y);
- grow();
- return child_line();
- }
- Line(start.x, start.y, end.x, end.y);
- return null;
- }
- float get_heading() {
- return e.atan2( start.y - end.y, start.x - end.x);
- }
- void grow() {
- end.x += e.cos(heading)* (velocity);
- end.y += e.sin(heading)* (velocity);
- }
- boolean collision = false;
- void collision() {
- collision = true;
- }
- }
- class collision {
- line[] lines= new line[0];
- int total;
- collision(line[] lines) {
- this.lines = lines;
- total = lines.length;
- }
- void add_line(line new_line) {
- lines = (line[]) append(lines,new_line);
- total++;
- }
- float[] temp = new float[2];
- void of_lines() {
- for( int x=0; x< total; x++) {
- if( (lines[x].end.x <0)||(lines[x].end.x>width) || (lines[x].end.y <0)||(lines[x].end.y>height) ) {
- lines[x].collision = true;
- }
- for( int y=0; y< total; y++) {
- if( ! lines[x].collision) {
- if (x != y) {
- 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);
- if( temp != null) {
- if( ( abs(temp[0])-abs(lines[x].end.x) < 5) && (abs(temp[1])-abs(lines[x].end.y) <5 )) {
- lines[x].collision();
- }
- }
- }
- }
- }
- }
- }
- }
1