Two Ellipses That Determine The Size of One
in
Programming Questions
•
1 year ago
This sketch has two ellipses; one (myBlue) that moves around the screen & another (myBlue2) that is controlled by the mouse. When they are touching, a new green circle will appear over the moving ellipse (myBlue). I would like the green ellipse's width/height to be determined by the ellipse controlled by the mouse. So, depending on the position of the mouse, the green ellipse will be wider or taller. I would like to have the two Blue ellipses inside the green ellipse at all times.
However, asking the green ellipse to get data from the x & y postion has been proving difficult since it will only grab a number & the size will not be relative to the actual positon of the two ellipses. For example, if the mouse controlled ellipse (myBlue2) is at x position 20, the green ellipse will be 20 pixels wide.
Maybe I've been staring at this too long but how do I determine my desired effect?
Should I be using a different function (beginShape?) or create another class?
- Ellipse myBlue;
- Ellipse myBlue2;
- void setup() {
- size(500, 500);
- smooth();
- myBlue = new Ellipse(25);
- myBlue2 = new Ellipse(25);
- }
- void draw() {
- background(255);
- myBlue.display();
- myBlue2.display();
- myBlue.move();
- myBlue2.control();
- if(myBlue.intersect(myBlue2)) {
- myBlue.highlight();
- fill(0, 200, 67, 150);
- //this is where the problem(???) is
- ellipse(myBlue.xpos, myBlue.ypos, myBlue2.xpos+myBlue2.r, myBlue2.ypos);
- }
- else {
- myBlue.display();
- }
- }
- class Ellipse {
- float r;
- float xpos;
- float ypos;
- float speedX;
- float speedY;
- color c;
- Ellipse(float tempR) {
- xpos = width/2;
- ypos = height/2;
- speedX = random(-2, 2);
- speedY = random(-2, 2);
- r = tempR;
- c = color(0, 45, 200);
- }
- void display() {
- noStroke();
- fill(c);
- ellipseMode(CENTER);
- ellipse(xpos, ypos, r*2, r*2);
- stroke(0, 223, 0);
- //line(xpos, ypos, xpos+25, ypos);
- c = color(0, 45, 200);
- }
- void move() {
- xpos += speedX;
- ypos += speedY;
- if(ypos > height || ypos < 0) {
- speedY = speedY * -0.95;
- }
- if(xpos > width || xpos < 0) {
- speedX = speedX * -0.95;
- }
- xpos = constrain(xpos,0,width);
- ypos = constrain(ypos,0,height);
- }
- void control() {
- xpos = mouseX;
- ypos = mouseY;
- xpos = constrain(xpos,0,width);
- ypos = constrain(ypos,0,height);
- }
- void highlight() {
- c = color(234, 0, 44);
- }
- boolean intersect(Ellipse myBlue2) {
- float distance = dist(myBlue.xpos, myBlue.ypos, myBlue2.xpos, myBlue2.ypos);
- if(distance < myBlue.r + myBlue2.r) {
- return true;
- }
- else {
- return false;
- }
- }
- }
1