java - Draw random circles, coloring in red any circle not intersecting another circle -
i have java swing assignment following objectives:
- when program starts, draws 20 unfilled circles, radius , location of each determined @ random.
- if perimeter line of circle not intersect other circle, draw outline of circle in red. if intersect @ least 1 other circle, draw in black.
- add jbutton that, each time pressed, creates new set of circles described above.
i've completed objectives #1 , #3 above, i'm stumped on objective #2.
before present code, let me give understanding of math behind it. there 2 ways circle can not intersect circle:
- the circles far apart share perimeter point, i.e. distance between centers greater sum of radii (d > r1 + r2). example.
- one circle inside circle, , perimeters not touch, i.e. distance between centers less difference between radii (d < |r1 - r2|). example.
what i've got far:
- to compare circles, must specified before drawn, used for-loop store 20 values in arrays center coordinates (int[] x, int[] y) , radius (double[] radius).
- next, used nested for-loops iterate through array , compare 2 circles, except when circle compared (index j = index k). if circles intersect, g.setcolor(color.red). if not, g.setcolor(color.black).
when execute code, circles without overlap colored red. however, of overlapping circles colored red well. assume non-overlapping at time drawn, intersected thereafter. how fix code account discrepancy in time? (problem area located near bottom, in intersectingcircles class)
import java.awt.borderlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.color; import java.awt.graphics; import javax.swing.jpanel; import javax.swing.jframe; import javax.swing.jbutton; public class buttonframe extends jframe { private final jbutton resetbutton = new jbutton("reset"); public buttonframe() { super("drawing random circles"); setlayout(new borderlayout()); intersectingcircles intersectingcircles = new intersectingcircles(); this.add(intersectingcircles, borderlayout.center); this.add(resetbutton, borderlayout.south); this.setdefaultcloseoperation(exit_on_close); this.setsize(1400, 1400); buttonhandler handler = new buttonhandler(); resetbutton.addactionlistener(handler); } private class buttonhandler implements actionlistener { @override public void actionperformed(actionevent event) { reset(); } } public static void main(string[] args) { buttonframe buttonframe = new buttonframe(); buttonframe.setvisible(true); } public void reset() { buttonframe buttonframe = new buttonframe(); buttonframe.setvisible(true); } } class intersectingcircles extends jpanel { private static final jbutton resetbutton = new jbutton("reset circles"); private static final jframe frame = new jframe("intersecting circles"); @override public void paintcomponent(graphics g) { super.paintcomponent(g); this.setbackground(color.white); int[] x = new int[20]; int[] y = new int[20]; int[] diameter = new int[20]; double[] radius = new double[20]; (int = 0; < 20; i++) { int xcoord = (int)(math.random() * 600); int ycoord = (int)(math.random() * 600); int circlesize = (int)(math.random() * 550); x[i] = xcoord; y[i] = ycoord; diameter[i] = circlesize; radius[i] = circlesize / 2.0; } (int j = 0; j < 20; j++) { (int k = 0; k < 20; k++) { if (k != j) { if (((math.sqrt((x[k] - x[j]) * (x[k] - x[j]) + (y[k] - y[j]) * (y[k] - y[j]))) > (radius[j] + radius[k])) || ((math.sqrt((x[k] - x[j]) * (x[k] - x[j]) + (y[k] - y[j]) * (y[k] - y[j]))) < (math.abs(radius[j] - radius[k])))) g.setcolor(color.red); else g.setcolor(color.black); g.drawoval(x[j], y[j], diameter[j], diameter[j]); } else continue; } } } }
you have logic mistake in if
statement inside cycle - can set black color revert red other pair circle. possible solution draft:
(int j = 0; j < 20; j++) { g.setcolor(color.red); //set non-intersect state (int k = j + 1; k < 20; k++) //avoid excessive work { if (intersect test) { g.setcolor(color.black); break; //can stop here }; g.drawoval(x[j], y[j], diameter[j], diameter[j]); } }
Comments
Post a Comment