Tech Support Guy banner
Status
Not open for further replies.
1 - 4 of 4 Posts

·
Registered
Joined
·
525 Posts
Discussion Starter · #1 ·
I'm trying to make it so that both of my circles' center points are 25 pixels away from each other. But, I'm not really sure how to do that. I know that the (x,y) coordinate you put in as a parameter for Ellipse2D.Double is at the top left of the bounding box, but really that's all I know.

Two_Circles class: http://fpaste.org/paste/6201
Two_Circles tester: http://fpaste.org/paste/6202

Thanks in advance!
 

·
Registered
Joined
·
6,693 Posts
Hi SpyKiller112,

I've tested your coding, your circles are showing.

Unfortunately, you have to apply the Pythagoras' theorem to find the coordinates of the circle center.
In your example, the coordinate of the first circle center (a, b) are :
a = 5 + sqrt((50*50)/2)
b = 10 + sqrt((50*50)/2)

Also, if D is the distance between 2 points (x1,y1) and (x2,y2), then :
(x1-x2)**2 + (y1-y2)**2 = D**2
 

·
Registered
Joined
·
525 Posts
Discussion Starter · #3 ·
Oh god, thanks a ton. I never would have thought of that! It would be so much easier if the coordinates referred to the center of the ellipse in the first place though.
 

·
Registered
Joined
·
6,693 Posts
Hi SpyKiller112,

To solve the problem, you may add the following class in your program :
Code:
[SIZE=2]
package [COLOR=BLUE]your.package.name[/COLOR];

import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

/**
 *
 * @author Chicon
 */
public abstract class Circle2D extends Ellipse2D {
    public static class Double extends Ellipse2D implements Serializable {
        public double x;
        public double y;
        public double height;
        public double width;
        
        public Double (double x, double y, double radius) {
            double v = this.getValue(radius);
            this.x = x - v;
            this.y = y - v;
            this.width = radius;
            this.height = radius;
        }

        @Override
        public double getX() {
            return this.x;
        }

        @Override
        public double getY() {
            return this.y;
        }

        @Override
        public double getWidth() {
            return this.width;
        }

        @Override
        public double getHeight() {
            return this.height;
        }

        @Override
        public boolean isEmpty() {
            return (width <= 0.0 || height <= 0.0);
        }

        @Override
        public void setFrame(double x, double y, double w, double h) {
            this.x = x;
            this.y = y;
            this.width = w;
            this.height = h;
        }

        public Rectangle2D getBounds2D() {
            return new Rectangle2D.Double(x, y, width, height);
        }
        
        private double getValue(double radius) {
            return Math.sqrt((radius*radius)/2);
        }
    }
}
[/SIZE]
And you adapt your coding this way :
Code:
[SIZE=2]
      //recover Graphics2D
      Graphics2D g2 = (Graphics2D) g;
      
      //draws first circle
      Circle2D.Double circle1 = new Circle2D.Double(40, 45, 50);
      g2.draw(circle1);
[/SIZE]
;)
 
1 - 4 of 4 Posts
Status
Not open for further replies.
Top