CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    Checking objects in a circle

    Does anyone know how to check if an object is in an oval drawn on a java applet screen.

    I'm not sure of the math needed to check this, if you post a code can you please have it "System.out.println("In the oval");"

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Checking objects in a circle

    If the oval is an instance of Ellipse2D, it will have 'contains' methods for single points and rectangular areas.

    Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
    A. Aho and J. Ullman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2010
    Posts
    2

    Re: Checking objects in a circle

    What? That doesn't help me at all. I don't understand what your getting at could you give me an example please?

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Checking objects in a circle

    Quote Originally Posted by B.Star117 View Post
    What? That doesn't help me at all. I don't understand what your getting at could you give me an example please?
    Instead of playing "guess my question" why don't you state exactly what you want to know.

    Why do you say circle in the topic and oval in the question? What is it exactly and how is it represented.

    What's the nature of the "object" you're talking about? How is it represented?

    Be specific and you get specific replies.
    Last edited by nuzzle; February 8th, 2010 at 01:54 AM.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Checking objects in a circle

    Quote Originally Posted by B.Star117 View Post
    What? That doesn't help me at all.
    Oh. OK.

    I don't understand what your getting at could you give me an example please?
    Code:
    Component object = getObject();
    Ellipse2D oval = getOval();
    // check to see if object is inside oval
    Rectangle bounds = object.getBounds();
    boolean isInside = oval.contains(bounds.x, bounds.y, bounds.height, bounds.width);
    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: Checking objects in a circle

    And just in case you aren't using that class, you said:

    I'm not sure the math needed to check this...
    The 'math' needed to do this is simple: You cast a ray, any direction, from the point in question (usually done to the right). If the ray / vector intersects the boundary of the circle (i.e. at some point lies on the circle) only once, then it is inside the circle. If it intersects it twice, or not at all then it is outside the circle.

  7. #7
    Join Date
    Apr 2007
    Posts
    425

    Re: Checking objects in a circle

    Quote Originally Posted by ProgramThis View Post
    And just in case you aren't using that class, you said:



    The 'math' needed to do this is simple: You cast a ray, any direction, from the point in question (usually done to the right). If the ray / vector intersects the boundary of the circle (i.e. at some point lies on the circle) only once, then it is inside the circle. If it intersects it twice, or not at all then it is outside the circle.
    Reading your math suggestions makes me want to pull out my university books again. I'm starting to get rusty. These type of solutions don't come to my mind anymore.
    ------
    If you are satisfied with the responses, add to the user's rep!

  8. #8
    Join Date
    Nov 2009
    Posts
    7

    Re: Checking objects in a circle

    Quote Originally Posted by ProgramThis View Post
    ... You cast a ray, any direction, from the point in question (usually done to the right). If the ray / vector intersects the boundary of the circle (i.e. at some point lies on the circle) only once, then it is inside the circle. If it intersects it twice, or not at all then it is outside the circle.
    Clarfying what you suggest should be a plus

    Mayb a piece of code snip you pick up somewhere for example

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Checking objects in a circle

    Clarfying what you suggest should be a plus
    Mayb a piece of code snip you pick up somewhere for example
    dlorde has given a java class that does this. If you want a code example you could just look at the source code for the java.awt.geom.Ellipse2D classes contains() method and see how that class does it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Checking objects in a circle

    Quote Originally Posted by keang View Post
    If you want a code example you could just look at the source code for the java.awt.geom.Ellipse2D classes contains() method and see how that class does it.
    I suspect the OP lacks the initiative...

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Checking objects in a circle

    I suppose at least the poster asking for code didn't use the OP's line of "What? That doesn't help me at all."
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Feb 2008
    Posts
    966

    Re: Checking objects in a circle

    Quote Originally Posted by Deliverance View Post
    Reading your math suggestions makes me want to pull out my university books again. I'm starting to get rusty. These type of solutions don't come to my mind anymore.
    The fun part is trying to optimize the algorithm that tries to determine this, given an array of points for a polygon.

    I duel majored in Pure Mathematics and Computer Science. This is the type of stuff I would rather be doing than coding stupid webapps for a bank However, for some reason these companies hiring guys to do complex math and embedded c++ code seem to think that paying 20k below market value for a Java developer is going to net them top candidates. I wonder how their simulator is coming along?

  13. #13
    Join Date
    Apr 2007
    Posts
    425

    Re: Checking objects in a circle

    Quote Originally Posted by ProgramThis View Post
    The fun part is trying to optimize the algorithm that tries to determine this, given an array of points for a polygon.

    I duel majored in Pure Mathematics and Computer Science. This is the type of stuff I would rather be doing than coding stupid webapps for a bank However, for some reason these companies hiring guys to do complex math and embedded c++ code seem to think that paying 20k below market value for a Java developer is going to net them top candidates. I wonder how their simulator is coming along?
    Reminds me of what nuzzle was saying in another post with java being a managed language that is 'safe' that they assume anyone can do a good job at architecting and implementing a large system properly.
    ------
    If you are satisfied with the responses, add to the user's rep!

  14. #14
    Join Date
    Feb 2008
    Posts
    966

    Re: Checking objects in a circle

    Sorry Deliverance, rereading my post I realized that I didn't word it quite right. What I meant to convey is that locally they are paying say 50k / year for C++, Simulation software, embedded C developers and over 70k / year for Java developers. So, the guys doing the harder, more complex jobs get paid less. I've seen this paradigm before...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured