How to have line with a set length point towards a moving ellipse?

how to have the line with //this line behind it, always point towards the ellipse with // this ellipse behind it, while staying 100 pixels long?

float sunx=300;
float suny=60;
int time=600;
float begx= 300;
float begy= 540;
boolean growline= false;

//--------------------------------------------------
void setup() {
  size(600, 600);
  fill(255, 230, 0);
}

//--------------------------------------------------
void draw() {
  fill(255, 230, 0);
  background(0);
  stroke(255, 230, 0);
  ellipse(sunx, suny, 50, 50); // this ellipse 
  fill(#4aff00);
  ellipse(begx, begy, 10, 10);


  if (suny<60) {
    suny=suny-10;
  }

  if (keyPressed) {
    if (key=='p') {
      time=time+10;
      if (sunx>width/2+20) {
        suny=suny+1;
      } else if (sunx<width/2-20) {
        suny--;
      } else if (sunx>width/2-20&&sunx<width/2) {
        suny= suny+0.001;
      } else if (sunx<width/2+20&&sunx>width/2) {
        suny=suny+0.001;
      }
    }
  }
  if (time>1200) {
    time=0;
  }

  sunx=time/2;



  if (growline){

 line() // this line

; {
  }
 }
  if (millis()%5==0) {
    println("works");
    strokeWeight(5);
    stroke(#4aff00);
    if(growline==false){
    growline= true;
    }
    else if(growline){
      growline= false;
    }
  }
  }

Please learn how to format your code

Go back and edit your post (pen icon)

Select code and hit ctrl e

thanks i forgot to do that

1 Like

Look at atan in the reference please or atan2

how do I make a new rectangle appear every 5 seconds and have them be locked in place as soon as they are visible?

float sunx=300;
float suny=60;
int time=600;
float begx= 300;
float begy= 540;

//--------------------------------------------------
void setup() {
  size(600, 600);
  fill(255, 230, 0);
}

//--------------------------------------------------
void draw() {
  fill(255, 230, 0);
  background(0);
  stroke(255, 230, 0);
  ellipse(sunx, suny, 50, 50);
  fill(#4aff00);
  stroke(#4aff00);
  ellipse(begx, begy, 10, 10);


  if (suny<60) {
    suny=suny-10;
  }

  if (keyPressed) {
    if (key=='p') {
      time=time+10;
      if (sunx>width/2+20) {
        suny=suny+1;
      } else if (sunx<width/2-20) {
        suny--;
      } else if (sunx>width/2-20&&sunx<width/2) {
        suny= suny+0.001;
      } else if (sunx<width/2+20&&sunx>width/2) {
        suny=suny+0.001;
      }
    }
  }
  if (time>1200) {
    time=0;
  }

  sunx=time/2;


    translate(begx, begy);
    float a= atan2(suny-height/2, sunx-width/2);
    rotate(a);
    rect(5, -1, 100, 1); 
 
  if (millis()%5==0) {
    println("works");
    strokeWeight(5);
    stroke(#4aff00);
  }
}

Use an ArrayList best with a class that represents a rectangle

Then use list.add() command for the Arraylist when the timer hits 5 seconds

how do i turn the rectangle into a class?

see tutorials, objects please

example with class for rectangles (see mentioned tutorial Objects / Processing.org) and one rectangle is added every 1,8 seconds

(both important paragraphs are at the end of draw(): spawn and display rectangles)

float sunx=300;
float suny=60;
int time=600;
float begx= 300;
float begy= 540;

ArrayList<MyRect> rectList = new ArrayList();  

int timer; 

//--------------------------------------------------

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

//--------------------------------------------------

void draw() {
  background(0);

  fill(255, 230, 0);
  stroke(255, 230, 0);
  ellipse(sunx, suny, 50, 50);

  fill(#4aff00);
  stroke(#4aff00);
  ellipse(begx, begy, 10, 10);


  if (suny<60) {
    suny=suny-10;
  }

  if (keyPressed) {
    if (key=='p') {
      time=time+10;
      if (sunx>width/2+20) {
        suny=suny+1;
      } else if (sunx<width/2-20) {
        suny--;
      } else if (sunx>width/2-20&&sunx<width/2) {
        suny= suny+0.001;
      } else if (sunx<width/2+20&&sunx>width/2) {
        suny=suny+0.001;
      }
    }
  }
  if (time>1200) {
    time=0;
  }

  sunx=time/2;

  translate(begx, begy);
  float a = atan2(suny-height/2, sunx-width/2);
  rotate(a);
  rect(5, -1, 100, 1); 

  if (millis()%5==0) {
    println("works");
    strokeWeight(5);
    stroke(#4aff00);
  }

  // SPAWN 
  if (millis()-timer > 1800) {
    rectList.add(new MyRect()); 
    timer = millis();
  }//

  //display
  for (MyRect currentRect : rectList) {
    currentRect.display();
  }
}

//================================

class MyRect {
  float x=random(width);
  float y=random(height);
  float s=random(11, 49);
  color c = color (random(255), random(255), random(255)); 

  // no constructor yet

  void display() {
    noStroke(); 
    fill(c); 
    rect(x, y, s, s);
  }
}//class
// 

thank you chris, I would also like some help on how to find the end point of the rectangle

float sunx=300;
float suny=60;
int time=600;
float begx= 300;
float begy= 540;

ArrayList<MyRect> rectList = new ArrayList();  

int timer; 

//--------------------------------------------------

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

//--------------------------------------------------

void draw() {
  background(0);

  fill(255, 230, 0);
  stroke(255, 230, 0);
  ellipse(sunx, suny, 50, 50);

  fill(#4aff00);
  stroke(#4aff00);
  ellipse(begx, begy, 10, 10);


  if (suny<60) {
    suny=suny-10;
  }

  if (keyPressed) {
    if (key=='p') {
      time=time+10;
      if (sunx>width/2+20) {
        suny=suny+1;
      } else if (sunx<width/2-20) {
        suny--;
      } else if (sunx>width/2-20&&sunx<width/2) {
        suny= suny+0.001;
      } else if (sunx<width/2+20&&sunx>width/2) {
        suny=suny+0.001;
      }
    }
  }
  if (time>1200) {
    time=0;
  }

  sunx=time/2;
  translate(begx, begy);
  float a = atan2(suny-height/2, sunx-width/2);
  rotate(a);
  rect(5, -1, 100, 1); 

  if (millis()%5==0) {
    println("works");
    strokeWeight(5);
    stroke(#4aff00);
  }

  // SPAWN 
  if (millis()-timer > 1800) {
    rectList.add(new MyRect()); 
    timer = millis();
  }//

  //display
  for (MyRect currentRect : rectList) {
    currentRect.display();
  }
}

//================================

class MyRect {


  void display() {
    rect(begx, begy, 100, 1);
  }
}//class
//

to which lines are you referring to?

these

?

I want to find the opposite side of this rectangle

Couple of remarks

Remark 1

please note that you destroyed the class. The way you have it now means that independent rectangles can’t appear any longer in different position (every 1,8 seconds)


Remark 2

Also note that when you want to use translate and rotate you need to surround the paragraph with popMatrix() and pushMatrix() like here:

  sunx=time/2;
  pushMatrix(); 
  translate(begx, begy);
  float a = atan2(suny-height/2, sunx-width/2);
  rotate(a);
  rect(5, -1, 100, 1); 
  popMatrix(); 

NOW a 2nd green rect appears, pointing right


Remark 3

ALSO to get a more accurate green arrow (pointing to the sun) use begx and begy here

  float a = atan2(suny-begy, sunx-begx);  

Remark 4

Still not sure what you mean here

Chrisir


this point

1 Like


  float x=cos(a)*100+begx;
  float y=sin(a)*100+begy;

  ellipse(x, y, 15, 15);

this is not working as expected

float sunx=300;
float suny=60;
int time=600;
float begx= 300;
float begy= 540;
float a;

ArrayList<MyRect> rectList = new ArrayList();  

int timer; 

//--------------------------------------------------

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

//--------------------------------------------------

void draw() {
  background(0);

  fill(255, 230, 0);
  stroke(255, 230, 0);
  ellipse(sunx, suny, 50, 50);

  fill(#4aff00);
  stroke(#4aff00);
  ellipse(begx, begy, 10, 10);


  if (suny<60) {
    suny=suny-10;
  }

  if (keyPressed) {
    if (key=='p') {
      time=time+10;
      if (sunx>width/2+20) {
        suny=suny+1;
      } else if (sunx<width/2-20) {
        suny--;
      } else if (sunx>width/2-20&&sunx<width/2) {
        suny= suny+0.001;
      } else if (sunx<width/2+20&&sunx>width/2) {
        suny=suny+0.001;
      }
    }
  }
  if (time>1200) {
    time=0;
  }

  sunx=time/2;
  translate(begx, begy);
  a = atan2(suny-height/2, sunx-width/2);
  rotate(a);
  rect(5, -1, 100, 1); 

  if (millis()%5==0) {
    println("works");
    strokeWeight(5);
    stroke(#4aff00);
  }

  // SPAWN 
  if (millis()-timer > 1800) {
    rectList.add(new MyRect()); 
    timer = millis();
  }//

  //display
  for (MyRect currentRect : rectList) {
    currentRect.display();
  }
}
  
//================================

class MyRect {
  
  float x=cos(a)*100+begx;
  float y=sin(a)*100-begy;



  void display() {

  rect(x, y, 15, 15);
  }
}//class
//

what do you mean

in how far is it no working…?

I pointed out a few things

have you read them?

Did you read the tutorial on objects? You are not using the class correctly.

here is your code with the 2 lines :

  float x=cos(a)*100+begx;
  float y=sin(a)*100+begy;

I gotta go - bye.

Full Code


float sunx=300;
float suny=60;
int time=600;
float begx= 300;
float begy= 540;
float a;

ArrayList<MyRect> rectList = new ArrayList();  

int timer; 

//--------------------------------------------------

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

//--------------------------------------------------

void draw() {
  background(0);

  fill(255, 230, 0);
  stroke(255, 230, 0);
  ellipse(sunx, suny, 50, 50);

  fill(#4aff00);
  stroke(#4aff00);
  ellipse(begx, begy, 10, 10);


  if (suny<60) {
    suny=suny-10;
  }

  if (keyPressed) {
    if (key=='p') {
      time=time+10;
      if (sunx>width/2+20) {
        suny=suny+1;
      } else if (sunx<width/2-20) {
        suny--;
      } else if (sunx>width/2-20&&sunx<width/2) {
        suny= suny+0.001;
      } else if (sunx<width/2+20&&sunx>width/2) {
        suny=suny+0.001;
      }
    }
  }
  if (time>1200) {
    time=0;
  }

  pushMatrix(); 
  sunx=time/2;
  translate(begx, begy);
  a = atan2(suny-begy, sunx-begx);
  rotate(a);
  rect(5, -1, 100, 1); 
  popMatrix(); 

  float x=cos(a)*100+begx;
  float y=sin(a)*100+begy;
  fill(255, 2, 2); 
  ellipse(x, y, 22, 22);
  fill(2, 255, 2); 

  if (millis()%5==0) {
    println("works");
    strokeWeight(5);
    stroke(#4aff00);
  }

  // SPAWN 
  if (millis()-timer > 1800) {
    rectList.add(new MyRect()); 
    timer = millis();
  }//

  //display
  for (MyRect currentRect : rectList) {
    currentRect.display();
  }
}

//================================

class MyRect {
  void display() {
    rect(begx, begy, 15, 15);
  }
}//class
//