We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hey guys so i managed to write an array of circles but i want to draw lines where the circles overlap one another and Im not that advanced yet-im quite new ive already written the code for the the circles etc which work but i know i need to put in that line function but im a bit confused as how to write it
here it is;
1. //ARRAY PART -SETUP
Particle [] particleCollection = new Particle [500];
void setup(){
background(255);
size(500,500,P2D);
smooth();
for(int i = 0; i < particleCollection.length; i++){
particleCollection[i] = new Particle(random(0,width),random(0,200));
}
}
void draw(){
background(255);
noFill();
for(int i = 0; i < particleCollection.length; i ++){
particleCollection[i].run();
}
}
2.// ARRAY PARTICLE SYSTEM
class Particle{
float x = 0;
float y = 0;
float speedX = 0.5;
float speedY = 0.5;
float t = 0;
Particle(float _x, float _y){
x = _x;
y = _y;
}
void run(){
display();
move();
bounce();
gravity();
}
void gravity(){
speedY += 0.1;
}
void bounce(){
if( x > width){
speedX = speedX * -1;
}
if( x < 0){
speedX = speedX * -1;
}
if( y > height){
speedY = speedY * -1;
}
if( y < 0){
speedY = speedY * -1;
}
}
void move(){
x += speedX;
y += speedY;
}
void display(){
ellipse(x,y,50,50);
}
}
Answers
When posting code on this forum then select/highlight the code and press Ctrl + K or click on the C button. This will indent your code so that it is properly formatted. I will do this for you when I have posted this.
If you look at this page there are several functions you can use when working with circles, including one to calculate the intersection points of overlapping circles. They might help.
It is not clear what the lines should join. In the following code I have assumed that you want a line that joins the centres of any 2 circles that intersect (i.e. circumferences intersect). I have also provided for different size particles and randomised their starting velocities and have renamed the array to reduce the amount of typing.
Hey! I've made a slim remix. Check it out online: :D
http://studio.processingtogether.com/sp/pad/export/ro.989GaZC5t7EkE/latest
There is this thread in StackOverflow about circle intersection, a PGraphics approach and a cool math one also.
http://stackoverflow.com/questions/13609549/intersecting-circle-overlap/
Hey Quark-thanks so much for the help and the comments in the processing! I havent yet learnt the boolean function so now I think I have an idea as to where it can work and not cheers guys and GoToLoop -looks cool! I will definitely be working on that further when i get more time!
Wrote my very own code and it finally worked;
It's not a function, but 1 of the Java's 8 primitive data-types!
http://processing.org/reference/boolean.html
Method isIntersecting() returns a
boolean
type. Valuetrue
when current Particle object is intersecting w/ the 1 passed.Value
false
otherwise. That returning value decides whether to invoke linkToParticle() within theif
block.