java - drawing random circles, storing their coorindates in an array -


this want accomplish homework: design , implement program draws circles, radius , location of each circle determined @ random. if circle not overlap other circle, draw circle in black. if circle overlaps 1 or more circles, draw in cyan. use array store representation of each circle, determine color of each circle. 2 circles overlap if distance between center points less sum of radii.

i close, cannot figure out how use sqrt formula in order compare radii of circles overlap , redraw circle in cyan. i've tried figure out in 2 other posts here: drawing random circles, storing coorindates in array , here: draw random circles, first storing points in array. got pointers, can give me specific pointers figure out how make loop use math.sqrt function in order compare radii , redraw overlapping circle in cyan? thank much.

update: have gotten math.sqrt forumla working, can't figure out how structure loop in order make circle overlaps filled in. tried using nested loop boolean in it, making of circles fill in. thank recommendations far.

here code have far:

 import java.util.random; import javax.swing.jframe; import javax.swing.jpanel; import java.awt.*; import java.awt.event.*; import java.math.*;  public class randomcircles extends jpanel {     private int[] sizearray = new int [5];  // radius of each circle     private int[] xarray = new int [5]; //array store x coordinates of circles     private int[] yarray = new int [5]; //array store y coordinates of circles     private int x1, x2, y1, y2;     private boolean overlap = false;       public randomcircles()     {         random r = new random();          (int = 0; i<xarray.length; i++){             //random numbers 1 20;             xarray[i] = r.nextint(200) + 1;         }         (int = 0; i<yarray.length; i++){             yarray[i] = r.nextint(200) + 1;         }         (int = 0; i<sizearray.length; i++){             sizearray[i] = r.nextint(100) +1;         }          setbackground (color.blue);         setpreferredsize (new dimension(300, 200));     }     //  generates of circles stored in array.      public void paintcomponent (graphics page)     {         super.paintcomponent(page);         (int = 0 ;i<xarray.length; i++) //this iterator draws circles , checks overlapping radii             (int j = 0 ;j<xarray.length; j++)             {                 //boolean overlap = false;                 //is supposed compare radii of circles check if overlap                 {                     if (math.sqrt((xarray[i]-xarray[j])*(xarray[i]-xarray[j])+(yarray[i]-yarray[j])*(yarray[i]-yarray[j])) >sizearray[i]-sizearray[j]);                      boolean overlap = true;                     page.filloval(xarray[i], yarray[i], sizearray[i], sizearray[i]);                     page.setcolor (color.cyan);                     repaint();                   } //draws circles stored in array                 page.drawoval(xarray[i], yarray[i], sizearray[i], sizearray[i]);//outer loop             }     }     public static void main (string[] args)     {         jframe frame = new jframe ("circles");         frame.setdefaultcloseoperation (jframe.exit_on_close);          frame.getcontentpane().add (new randomcircles());          frame.pack();         frame.setvisible(true);     } } 

//math.sqrt((x1-x2)*(x1-x2)-(y1-y2)*(y1-y2)), go , read chapter 7 

should be

math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) 

(you need take square root of sum of squared x , y distances, not difference.)

update:

there couple of problems overlap detection. 2 circles overlap if sum of radii greater distance between centers, you're taking difference of radii , checking if it's less than distance between centers.

also, should skip overlap check whenever i == j (since every circle overlaps itself; you're interested in overlaps between different circles).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -