Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:55:18+00:00 2026-05-30T04:55:18+00:00

I am trying to test if a point lies within a circle and if

  • 0

I am trying to test if a point lies within a circle and if the point is on the perimeter, it should be included in the results. However, Java’s contains() implementation uses less than instead of less than or equal to. For example consider this snippet:

Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 100, 100);

System.out.println(circle.contains(50, 0));
System.out.println(circle.contains(50, 100));
System.out.println(circle.contains(0, 50));
System.out.println(circle.contains(100, 50));
System.out.println(circle.contains(50, 50));

This prints the following:

false
false
false
false
true

How can I achieve a value of true for all of those cases?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:55:19+00:00Added an answer on May 30, 2026 at 4:55 am

    You have to decide what kind of tolerance your method will use. While your example uses points that are expressible in floating point, there are many points along the border of the ellipse which will not be, and so deciding whether a point is “on the border” isn’t clear-cut. If you don’t much care, then I would suggest making the ellipse slightly “bigger” than you actually want and using the built-in contains() method.

    If you want to write your own method, it’s as simple as taking the formula for an ellipse, plugging in the X and Y values of the point you wish to test, and observing the result:

    bool isInsideOfOrOnBorderOfEllipse = ((x*x)/(a*a) + (y*y)/(b*b)) <= 1;
    

    Note that this still runs into the problem of non-representable points, so some points that you think should be “on the border” won’t be.

    Update: Given that you’re just using the built-in ellipse object (and thus specifying height/width rather than the general ellipse parameters) it would be worthwhile to have a look at the source for contains() here: http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/ffa98eed5766/src/share/classes/java/awt/geom/Ellipse2D.java

    Derive a new class, and then override contains(). In the overridden version, just copy the code, except use <= instead of < and you should be good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to test an AccountController that uses DotNetOpenAuth but I am running into
I am still trying to test my WinForm application, however some of the testing
I am trying to test the 31 reparse point limit mentioned for Symbolic link
I am trying to implement a test project using the Point Cloud Library and
I am trying to test if objects are the results of errors. The use
I am trying to write a small test application that uses the UMLS UTS
I'm using the JMockit framework and I'm trying to test my simple EventBus implementation
I'm trying to test whether a given point (x, y) is located on or
just trying to test for equality in this piece of code, but getting a
Im trying to test my successfully creates a new user after login (using authlogic).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.