Electrical Circuit

I need help creating this electrical circuit but I do not understand how to make my ellipse follow my circuit rather than continuing in the one direction. Can anyone help me please? I have pasted my coding below:

void setup() {
  size(600, 600);
}

void draw() {
  background(255);
  ellipse(150, 250, 100, 100);

  line(145, 80, 340, 80);
  stroke(126);

  line(145, 400, 340, 400);
  stroke(126);

  line(145, 80, 145, 200);
  stroke(126);

  line(145, 300, 145, 400);
  stroke(126);
  
  line(340, 400, 340, 80);
  stroke (126);

Instead of drawing the lines as simple code
I suggest you have a list (array) of lines.
(Or two arrays of type PVector - one line is from
the PVector in the first array to the PVector in the second array: line(from[i].x,from[i].y, to[i].x,to[i].y); )

Then for loop over the array and display the lines.

Then use lerp() to move ball from line start to line end of all lines.

2 Likes

Thanks very much for the response but unfortunately I am very new to processing and do not completely understand what you mean

Read the tutorial on arrays in the tutorial section

https://www.processing.org/tutorials/arrays/

In the reference

Look at PVector, array and lerp() in the reference

https://www.processing.org/reference/

1 Like

ok, but just as a first step:
you need position and speed ( direction ) thinking:

int movex = 145, movey = 195, dx = 0, dy = -1;

void setup() {
  size(600, 600);
}

void draw() {
  background(255);
  draw_circuit();
  current();
}

void current() {
  ellipse(movex, movey, 10, 10);
// startpoint: movex = 145; movey = 195; dx = 0, dy = -1;
  if ( movey <= 80  ) { dy = 0; dx = 1; }
  if ( movex >= 340 ) { dy = 1; dx = 0; }
  if ( movey >= 400 ) { dy = 0; dx = -1; }
  if ( movex <= 145 && movey > 80 )  { dy = -1; dx = 0; }  
  movex += dx;
  movey += dy;
}

void draw_circuit() {
  stroke(126);
  ellipse(145, 250, 100, 100);
  line(145, 80, 340, 80);
  line(145, 400, 340, 400);
  line(145, 80, 145, 200);
  line(145, 300, 145, 400);
  line(340, 80, 340, 200);
  line(340, 300, 340, 400);
  line(340, 200, 360, 220);
  line(320, 240, 360, 220);
  line(320, 240, 360, 260);
  line(320, 280, 360, 260);
  line(320, 280, 340, 300);
}

1 Like

Regards, Chrisir



int i_init  = 0; 

int i_point = 0; 
int amt     = 0; 

PVector[] from=new PVector[11];
PVector[] to=new PVector[11];

void setup() {
  size(600, 600);
  define_circuit();
}

void draw() {
  background(255);
  draw_circuit();
  current();
}

// --------------------------------------------------------------------
// Tools

void current() {

  // show text 
  fill(0); 
  text("line "+i_point, 
    20, 20); 

  // show ellipse 
  float x = lerp(from[ i_point ].x, to[ i_point ].x, amt/100.0);
  float y = lerp(from[ i_point ].y, to[ i_point ].y, amt/100.0);

  ellipse(x, y, 
    10, 10);

  // manage numbers 
  // increase lerp percentage between 2 points 
  amt+=1;

  // if lerp percentage reached maximum
  if (amt>=100) {
    // reset
    amt=0;
    // next line 
    i_point++;
    if (i_point >= from.length) {
      i_point=0;
    }
  }
}

void draw_circuit() {
  stroke(0); 
  for (int i2=0; i2 < from.length; i2++) {
    line(from[i2].x, from[i2].y, 
      to[i2].x, to[i2].y);
  }//for
}//func 

// ------------------------------------------------------
// Inits 

void define_circuit() {
  addLine(145, 200, 145, 80);

  addLine(145, 80, 340, 80); 
  addLine(340, 80, 340, 200);  

  //---
  addLine(340, 200, 360, 220); 
  addLine(360, 220, 320, 240); 
  addLine(320, 240, 360, 260); 
  addLine(360, 260, 320, 280); 
  addLine(320, 280, 340, 300);
  //---

  addLine(340, 300, 340, 400); 
  addLine(340, 400, 145, 400); 
  addLine(145, 400, 145, 300);
}

void addLine(float x1, float y1, 
  float x2, float y2) {

  from[i_init] = new PVector();
  to[i_init] = new PVector();

  from[i_init].x= x1; 
  from[i_init].y= y1; 

  to[i_init].x= x2; 
  to[i_init].y= y2; 

  i_init++;
}
//
3 Likes