finding the overlap of two circles

Hello, all I have a question. I have a problem for class to draw two overlapping circles which i have done no problem.But I get extra points if I can color in all three parts a different color the two circles are easy it's the the center or the overlap that I am having trouble with.i figured I would use fillArc to do the overlap but how do I find the parts that overlap.Thanks.

Code:

import javax.swing.*;

import java.awt.*;

/**

* Created with IntelliJ IDEA.

* User: root

* Date: 6/9/12

* Time: 4:31 PM

* To change this template use File | Settings | File Templates.

*/

public class Quiz2A extends JFrame

{

Quiz2A()

{

NewPanel panel = new NewPanel();

add(panel);

}

public static void main(String[] args)

{

Quiz2A frame = new Quiz2A();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

frame.setSize(350,250);

frame.setTitle("Quiz2B");

frame.setLocationRelativeTo(null);

}

class NewPanel extends JPanel

{

protected void paintComponent(Graphics graphics)

{

graphics.drawOval(50,50,100,100);

graphics.drawOval(125,50,120,120);

//graphics.fillArc(100,50,50,20,20,75);

}

}

}

Re: finding the overlap of two circles

Perhaps the Graphics method setXORMode() will help. (Your commented out line ought to suggest that fillArc() won't help - even after all your trigonometry and stuff you get two overlapping arcs and you want the intersection. Same problem!)

Re: finding the overlap of two circles

Thanks man I will look into that.

Re: finding the overlap of two circles

Thank a lot man that worked perfectly.

Re: finding the overlap of two circles

You're welcome.

(In case anyone happens on the thread and wants a more complex approach... Java has a Shape interface which represent shapes - not surprisingly. There is a concrete implementation of Shape called Area which represents closed shapes like ellipses etc. This Area class actually allows you to do "constructive geometry" like unions, subtractions etc of one Area from another. And the Graphics2D class lets you render these areas filled and stroked however you like. The API docs have details, and Oracle's Tutorial describes an example in detail.)

Re: finding the overlap of two circles

Then there's the possibly simpler approach of Compositing Graphics. Filling the first circle, then successively filling the second with SRC_IN and SRC_OUT (with different colors for each of the three) would solve the stated problem.

db

Re: finding the overlap of two circles

Moved from New to Java

db