if / else statement

edited January 2017 in How To...

Hi There,

I want to make a function where the different elements (images and shapes) does not overlap each other. The positions of the different elements are random. So in every circumstances (positions) the elements does not reach each other. I have to do this with a if / else statement. Can somebody help me with this issue? Thanks

Tagged:

Answers

  • I have to do this with a if / else statement.

    Do you mean that this is a homework assignment? If so, what is the full assignment?

    In either case can you share the code that you have thus far?

  • It's not for 'homework'.. Its just for my own project..

  • This is what i have for now. The elements are bouncing with the window.

    float UnitS = 1; float mP = UnitS; float mS = UnitS; float g = 2; // float eW = 100; float eH = 2; // // void setup() { size(500, 750); noStroke(); }

    void draw() { background(#21EA73); Elmt(0+mP , height/7, eW, 80, #CF6946); Elmt(width/2 , height/3, eH+mS, 80+mS/4, #CF6946); }

    void Elmt(float x, float y, float w, float h, color c) { rectMode(CENTER); fill(c); rect(x, y, w, h); // change postion mP = mP + g; if (mP > width-eW/2) { mP = width-eW/2; g = -g; } else if (mP < eW/2) { mP = eW/2; g = -g; } // change position mS = mS + g; if (mS > width-eH/2) { mS = width-eH/2; g = -g; } else if (mS < 20) { mS = eW; g = -g; } }

  • edited January 2017 Answer ✓

    You can use if to check for collisions, as @GoToLoop suggests, and this should work for small numbers of small objects -- although some configurations will still make your next object placement impossible, so you may want to try a certain number of times and then give up.

    At a higher count and density of objects this if-then checking whether the new object will collide becomes incredibly slow and inefficient, as you will need to guess dozens, hundreds, or millions of times to find a space that "fits." What you are dealing with at that point is a packing problem.

    https://en.m.wikipedia.org/wiki/Packing_problems

Sign In or Register to comment.