Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #3320

Re: Programming question determining two circle's relation

From byhesed <byhesed@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Programming question determining two circle's relation
Date 2011-04-30 08:42 -0700
Organization http://groups.google.com
Message-ID <1598343d-42e2-4a65-9677-20c12675b174@n2g2000prj.googlegroups.com> (permalink)
References <892252a0-e31d-426b-8f47-d37c0b2ec364@17g2000prr.googlegroups.com> <ZbSdnTVJKocTTCbQnZ2dnUVZ_hCdnZ2d@earthlink.com> <511ca68f-7866-4658-9b2b-b1c0c013a030@a21g2000prj.googlegroups.com>

Show all headers | View raw


On 4월30일, 오후6시50분, byhesed <byhe...@gmail.com> wrote:
> On 4월30일, 오후6시08분, Patricia Shanahan <p...@acm.org> wrote:
>
>
>
>
>
>
>
>
>
> > On 4/30/2011 1:33 AM, byhesed wrote:
> > ...> - double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) +
> > > Math.pow(circle.y - getY(), 2.0));
> > ...
> > >    public boolean contains(double x, double y) {
> > >            double distance = Math.pow(getX() - x, 2.0) + Math.pow(getY() - y,
> > > 2.0);
> > >            if (distance<= radius)
> > >                    return true;
> > >            else
> > >                    return false;
> > >    }
>
> > ...
>
> > I suggest comparing the distance calculation you actually use in the
> > contains(double x, double y) method to the distance calculation you
> > described.
>
> > If you have a calculation you need to do a lot, why not write and test a
> > separate method to do it, rather than writing the calculation in-line
> > repeatedly?
>
> > What is the contains(Circle2D circle) method supposed to return if
> > circle contains this?
> > Patricia
>
> I know the source code I had posted is silly.
> I do not perform any optimization because it is just an example.
> It is an exercise from a Java book, Introduction to Java Programming 8/
> e.
> The reason I don't add a separate method is the author suggests not
> adding any other data fields and methods.
>
> Anyway, thank you for your advice.

Sorry, there were errors in my source code in determining the position
of two circles.

	public boolean contains(double x, double y) {
		double distance = Math.sqrt(Math.pow(getX() - x, 2.0) +
Math.pow(getY() - y, 2.0));
		if (distance <= Math.sqrt(radius))
			return true;
		else
			return false;
	}

	public boolean contains(Circle2D circle) {
		double distance = Math.sqrt(Math.pow(getX() - circle.getX(), 2.0) +
Math.pow(getY() - circle.getY(), 2.0));
		if (distance <= Math.abs(Math.sqrt(radius) -
Math.sqrt(circle.radius)))
			return true;
		else
			return false;

	}

	public boolean overlaps(Circle2D circle) {
		double distance = Math.sqrt(Math.pow(getX() - circle.getX(), 2.0) +
Math.pow(getY() - circle.getY(), 2.0));
		if (Math.abs(Math.sqrt(radius) - Math.sqrt(circle.radius)) <
distance && distance < Math.abs(Math.sqrt(radius) +
Math.sqrt(circle.radius)))
			return true;
		else
			return false;
	}

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Programming question determining two circle's relation byhesed <byhesed@gmail.com> - 2011-04-30 01:33 -0700
  Re: Programming question determining two circle's relation byhesed <byhesed@gmail.com> - 2011-04-30 01:59 -0700
    Re: Programming question determining two circle's relation "Gavino" <invalid@invalid.invalid> - 2011-04-30 17:51 +0200
      Re: Programming question determining two circle's relation byhesed <byhesed@gmail.com> - 2011-04-30 09:01 -0700
        Re: Programming question determining two circle's relation "Gavino" <invalid@invalid.invalid> - 2011-04-30 18:33 +0200
    Re: Programming question determining two circle's relation byhesed <byhesed@gmail.com> - 2011-04-30 02:03 -0700
  Re: Programming question determining two circle's relation byhesed <byhesed@gmail.com> - 2011-04-30 02:50 -0700
    Re: Programming question determining two circle's relation byhesed <byhesed@gmail.com> - 2011-04-30 08:42 -0700
      Re: Programming question determining two circle's relation "Gavino" <invalid@invalid.invalid> - 2011-04-30 17:59 +0200
    Re: Programming question determining two circle's relation "John B. Matthews" <nospam@nospam.invalid> - 2011-04-30 17:25 -0400
      Re: Programming question determining two circle's relation Patricia Shanahan <pats@acm.org> - 2011-04-30 17:03 -0700
        Re: Programming question determining two circle's relation "John B. Matthews" <nospam@nospam.invalid> - 2011-05-01 03:10 -0400
  Re: Programming question determining two circle's relation Patricia Shanahan <pats@acm.org> - 2011-04-30 02:08 -0700
  Re: Programming question determining two circle's relation Roedy Green <see_website@mindprod.com.invalid> - 2011-04-30 11:49 -0700

csiph-web