• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How do I find intersection of "GeneralPath" objects

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there any method in java.awt to find the intersection of two "GeneralPath" objects? I couldnt find any method in the class API. Can somebody tell me how to find the intersection region of two path objects?

Iam thinking of a way like, getting the path iterator for one "GeneralPath" object and checking each point in this, whether it lies in the another "GeneralPath" object using "contains" method. Is there any other best way to do this...?

Appreciate any good response..... Thanks,
 
Marshal
Posts: 80656
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know; I looked up GeneralPath and found it described as a legacy class; the recommend the java.awt.geom.Path2D subclasses (Float and Double). There are intersects methods, but they seem to be similar to contains. I think you are right that you will have to count the points in pathA which are inside pathB and the points in pathB which are inside pathA, put them in a List and produce a new Path.

Beware: Be sure that the two original Paths wind in the same direction, and also that they represent continuous lines without gaps. There appears to be a new getPathIterator method which will probably help you.
 
Ravi Kotha
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for immediate reply....

I tried to follow the winding rule, but wasn't clear. What is the main purpose of this winding rule? Also you mentioned like "to check whether all the paths are continuous straight lines without gaps", is there any mechanism to check the path is continuous? My assumption is like, I will be cautious when creating the path without any gaps.

Also, I can not make use of java.awt.geom.Path class, as I am using 1.4.2 API.

Thank you very much frnd,
 
Campbell Ritchie
Marshal
Posts: 80656
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have checked and GeneralPath is considered up-to-date programming in J1.4.2. The "winding rules" are means the GeneralPath object uses to work out whether a particular point is inside or outside the area of the object. I think non zero is easier to use.
You have to check that the Path is closed, and that it doesn't have any "moveTo" operations, otherwise you have a gap in the line drawn.

Try getting a PathIterator for pathA, if its Points are inside pathB if(pathB.contains(p)). . . put "p" in a List, then repeat for the other way round.

Possible easier way I have just found. Look up the Area class in J2SE1.4.2. You can use a Shape in its constructor,
  • Use pathA and pathB in the constructors to set up areaA and areaB.
  • Set up a third Area (temp) same shape as areaA. Don't say "temp = areaA"
  • subtract areaA from the temp Area,
  • and subtract what remains of the temp Area from areaB.
  • Now re-create the GeneralPath from what remains of areaB.

  • [ March 27, 2007: Message edited by: Campbell Ritchie ]
     
    Ravi Kotha
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much Ritchie.. hope the Area class will do for me. Thanks once again...
     
    Campbell Ritchie
    Marshal
    Posts: 80656
    477
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . Only too pleased to help. But I think I have said areaA when I meant areaB and vice versa.

    Sorry
     
    Ravi Kotha
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yup, I got the point.

    Here is a doubt in case of GeneralPath.


    if we say will it gives the iterator of boundary region points or for all the points...?
     
    Campbell Ritchie
    Marshal
    Posts: 80656
    477
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Don't know. Difficult to tell from the PathIterator API page. I would have thought all the points, but I think you will have to try it and see. Put a while loop in:-
    That will allow you to test the point counting.
     
    Campbell Ritchie
    Marshal
    Posts: 80656
    477
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Have made a mistake.
    You can't get the Point of an iterator, only an int which represents the locations of a segment.
    So you can't get Points out of the PathIterator.

    The Area method does work; I have tried it.

    [edit]Changed float[] which is the parameter type to int which is the return type of the PathIterator method.[/edit]
    [ April 02, 2007: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 80656
    477
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Actually you might be able to get the coordinates out of the PathIterator after all. The float[] array passed will contain the values of the points. Read the )]PathIterator page in the API.

    It is a rather odd way to pass a parameter, rather like the C method of passing a pointer as an argument; you get the result in the array passed, and the number of points can be guessed from the return type.
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For people who are looking for an answer
    http://forums.sun.com/thread.jspa?threadID=5222850&tstart=1108
     
    Sheriff
    Posts: 22849
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please Don't Wake The Zombies. The last post was over 18 months old.
     
    author
    Posts: 3285
    13
    Mac OS X Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Moshe and welcome to Javaranch!

    I appreciate the enthusiasm to help, but we tryo not to WakeTheZombies around here
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic