Random Circles in circles

I would like to generate circles at a random place in a circle, I do not know how to do this without having the circles generated by the generator overlap the edges of the starting circle

please send code

please send money first LOL

if that is a homework thing i must inform you that
teacher expect us NOT to send full code solutions to students
who instead of learning want go the easy way ?and cheat?
even that is not always clear?
if there is a ready code by a example / tutorial / video /
what solves your question, can i not link to it?


a circle on a random position with a ?fix? radius Ri
must be how far from the big outer circle? with radius R0
so it just touch that circle?
in that case, the center of the new small circle can be how far from the center of the big circle?

2 Likes

A link to an example of this helpful.

I apologize for not being specific, but I plan to tailor the base method of creating a circle at a random location in a circle without touching the edges of the larger circle to fit my needs.

more information in case u are wondering:

The larger circle is at a fixed position.
the size of the smaller circles doesn’t matter as long as it is in the bigger circle
the smaller circle can be as far as possible from the center of the circle assuming it does not cross the boundaries of the larger circle
yes it can touch the boundaries of the larger circle

thanks for your time, sorry for sounding desperate.

good,
so show us your ( ? 10 line code example )
where you draw

  • -1- the outer circle
  • -2- create a random position for the inner circles.
  • -3- ? here missing math for check THE RULE ?
  • -4- draw the inner circle(s)

so possible the -3- question can be a short function
what checks on the inner ? circle to circle collision?
and picks a other set of random position numbers…

//height and width are the screen length in x and y cords

ellipse(height/2,width/2,250,250);
int rand = random(0,4);
if (rand == 0){
ellipse(height/2,width/2-random(0,10),25,25);
}
if (rand == 1){
ellipse(height/2-random(0,10),width/2,25,25);
}
if (rand == 2){
ellipse(height/2-random(0,10),width/2-random(0,10),25,25);
}
if (rand == 3){
ellipse(height/2+random(0,10),width/2+random(0,10),25,25);
}

It isn’t very good, but It is what I have as of now regarding the code

This is very achievable.

http://mathforum.org/library/drmath/view/62892.html
https://processing.org/reference/circle_.html
https://processing.org/reference/random_.html

:slight_smile:

sorry, your code not run here.
also the 0 … 4 thing not makes too much sense
width and height are used for y and x instead x and y
the random position is not covering all canvas…
without a size() you can not show the big circle…

and why you not start with the default "non static mode " structure for processing
setup() and draw()

Think I got it.

Give me a bit to type it

ok this is all my code

I attempted to run a while loop until said parameters regarding the position of the circle are true, however it just pauses upon being run idk how to fix this

The code to create the smaller circle in the larger circle is triggered via pressing “N” (hold shift)

//varriables and stuff regarding the electrons trajectory and the amount of neutrons and protons
int constant = 250;
float angle = 0.05;
float angle1 = 0.05;
int scalar = 100;
float scalar1 = 200;
float speed = 0.01;
float speed1 = 0.02;
int P = 0;
int N = 1;
int randw;
int randl;
int powercound = 0;
int cound= 0;
//makes the screen 500/500 pixels wide and smooth makes things that are drawn smoother
void setup(){
size(500,500);
smooth();
background(255);
background(0);
}
float x = constant + sin(angle) * scalar;
float y = constant + cos(angle) * scalar;
float x1 = constant + sin(angle1) * scalar1;
float y1 = constant + cos(angle1) * scalar1;
//makes the background white instead of gray. 
//Sets the x and y values to the circular equasion's location 60 times a second
//this also draws the neuclues and displays the text that shows the amount of protons and neutrons are present. 
void draw(){
float x = constant + sin(angle) * scalar;
float y = constant + cos(angle) * scalar;
float x1 = constant + sin(angle1) * scalar1;
float y1 = constant + cos(angle1) * scalar1;
fill(255);
//functions that draw electrons and make them look cool
draw_electron(1, x,y);
funky_electron(x,y);
draw_electron(1, x1,y1);
funky_electron(x1,y1);
ellipse(height/2,width/2,50,50);

textSize(10);
fill(50);
text(P+"P",height/2-5,width/2+10);
text(N+"N",height/2-5,width/2);
angle = angle + speed;
angle1 = angle1 + speed1;
}

//This function draws the orbiting electron x and y are its cords, and 10 is its size
void draw_electron(int num, float x, float y){
int i = 0;
while(num > i){
i++;
ellipse(x,y,10,10);
  }
}
//makes electrons look dank
void funky_electron(float x, float y){
  for (float ring = 10; ring >= 0; ring -= random(2, 10)) {
    ellipse(x, y, ring, ring);
  }
}
//triggered when a key if pressed. These if statement check for specific keys
void keyPressed() {
  if (key == 'N') {
    N++;
    while(cound == 0){
      randw = (int)random(0,400);
      randl = (int)random(0,400);
      if (randl >=height/2 - 25 && randl <=height/2 + 25 && randw >=width/2 - 25 && randw <=width/2 + 25){ 
        ellipse(randl,randw,15,15);
        cound++;
    }
 cound = 0; 
}
  }
  if(key == 'P'){
    P++;
    
  }
  //Adds protons and nuetrons upon pressing these buttons
  if(key == 'p'){
    P--;
  }
  if(key == 'n'){
    N--;
  }
  if(N <= 0){
    N=1;
  }
  if(P < 0){
    P=0;
  }
  //Negative protons and nuetrons don't exist :/
  if(P < 0){
    P=1;
  }
  //Emmits energy and moves an electron down a state
  if(key == 'E'){
    while (speed1 != 0.01){
      speed1 = speed1 - speed1/15;
      scalar1 = scalar1/2+50;
      powercound++;
      ellipse(height/2,width/2,250,250);
      fill(255);      
      if (powercound == 255){
        speed1 = 0.01;
      }
    }
      background(255,0,0);
      ellipse(height/2,width/2,5000,5000);
      fill(255,0,0);
  }
  }

yes, while loops are dangerous , might run forever
you should put 2 println() lines in it and print your variables
so you see how often the while is running,
and if the condition ever gets true?


but for a circle to circle collision you need to know

  • the distance of the centers ( need some math from x and y …)
  • the two radius ( not the diameter )

that you do a inner circle collision only means that must be:

dist( C0 , C1 ) + R1 < R0


anyhow sorry, i go back to my original question:

minimal code for the 2 circles ( non static mode )

float Ri = 12, Ro = 125;

void setup() {
  size(300, 300);
  noLoop();
  println("press any key");
}

void draw() {
  background(200, 200, 0);
  fill(0,0,200);
  ellipse(width/2, height/2, 2*Ro, 2*Ro);
  float xpos = random(-width/2, width/2);
  float ypos = random(-height/2, height/2);
// here need to check THE RULE
  float dist = sqrt( sq(xpos) + sq(ypos) );  // distance of the 2 circle centers
  if ( dist + Ri > Ro ) fill(200,0,0);              // do more here? 
  else                  fill(0,200,0);

  ellipse(width/2+xpos, height/2+ypos, 2*Ri, 2*Ri);
}

void keyPressed() {
  redraw();
}