# Intersecting Circles

Page 1 of 1

## 1 Replies - 4305 Views - Last Post: 21 November 2011 - 04:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256681&amp;s=846d4f29957111a58435c8ccd6eafe8e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TuckNorris

Reputation: 0
• Posts: 9
• Joined: 20-November 11

# Intersecting Circles

Posted 21 November 2011 - 04:29 PM

Write a graphics program that asks the user to specify the radii of two circles. The first circle has center (100, 200), and the second circle has center (200, 100). Draw the circles. If they intersect, then color both circles green. Otherwise, color them red. Hint: Compute the distance between the centers and compare it to the radii. Your program should draw nothing if the user enters a negative radius. In your exercise, declare a class Circle and a method boolean intersects(Circle other).

Here is a sample program output:

Use the following class as your main class:
```import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
This program views two circles.
*/
public class CircleViewerSnap
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

CircleComponent component = new CircleComponent(60, 85);

frame.setVisible(true);
}
}

Complete the following classes in your solution:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

/**
This class implements a Circle. It includes a method to test whether
two circles intersect.
*/
public class Circle
{
private double xCenter;
private double yCenter;
private Color color;

/**
Constructs a black circle.
@param x the x-coordinate of the center
@param y the y-coordinate of the center
*/
public Circle(double x, double y, double r)
{
xCenter = x;
yCenter = y;
color = Color.BLACK;
}

/**
Sets the color of this circle.
@param aColor the color
*/
public void setColor(Color aColor)
{
color = aColor;
}

/**
Draws the circles.
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
return;

g2.setColor(color);
// draw the circle
Ellipse2D.Double circle
g2.draw(circle);
}

/**
Tests whether or not the two circles intersect with each other.
@param other the other circle
@return true the two circles intersect
*/
public boolean intersects(Circle other)
{
. . .
}
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
Shows two Circles and tests whether they intersect or not.
*/
public class CircleComponent extends JComponent
{
private Circle circle1;
private Circle circle2;

/**
Constructs a component for showing two circles.
@param r1 the radius of the first circle
@param r2 the radius of the second circle
*/
public CircleComponent(double r1, double r2)
{
String message;

circle1 = new Circle(100, 200, r1);
circle2 = new Circle(200, 100, r2);
Color color;
if (. . .)
color = Color.GREEN;
else
color = Color.RED;
circle1.setColor(color);
circle2.setColor(color);
}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;

circle1.draw(g2);
circle2.draw(g2);
}
}

Use the following class in your solution:

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
This program views two circles.
*/
public class CircleViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

final int FRAME_WIDTH = 400;
final int FRAME_HEIGHT = 400;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CircleViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

double r1 = Double.parseDouble(input);

double r2 = Double.parseDouble(input);

CircleComponent component = new CircleComponent(r1, r2);

frame.setVisible(true);
}
}

```

what to put inplace of . . . ?

Is This A Good Question/Topic? 0

## Replies To: Intersecting Circles

### #2 TuckNorris

Reputation: 0
• Posts: 9
• Joined: 20-November 11

## Re: Intersecting Circles

Posted 21 November 2011 - 04:52 PM

I've tried making a 2D Line from centerx to centery.
Then finding the midpoint and comparing it to the radii.

Shouldn't this work?