This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Getting a set of points where all the points included are the same distance away from a given point?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if the title is confusing. I wasn't sure how to say it concisely.

I am working with JavaFX. I have made Circle objects that represent the orbital paths of planets in a solar system. They have a transparent fill color, with white outlines.

I want to randomly choose the points to display the planets. However, I want each planet to be displayed somewhere along the white circle-outline indicating it's orbital path.

This means I need to find a way to gather a set of points that are exactly the same distance from the center of the circle-outline.

The only way I can think of to do this is to iterate through every point in the window that displays the solar system and check it's distance from the center.

I feel like there's probably a faster way for the program to find all the points at the same distance from the center. However, I am not the greatest at math, and I'm not sure how to implement it.

How can create a method that finds all the points X distance away from point Y and that runs more quickly than my brute-force method would?

 
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Phil,

well, using the old goniometrics will do. If the center point Y = Point2D.Double(cx, cy), then any point that is at a distance D will be of the form:

phi is a double, representing the angle of the point with the x-axis. You can get some points by taking some values for phi (phi going from 0 to 2*PI).
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops...

I used the math notation for the sum. Of couse that should read as:

instead of
 
Phil Patterson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Oops...

I used the math notation for the sum. Of couse that should read as:

instead of



Thank you this seems like a good solution. Could you help me figure this out a little more though? I'm not sure which Point2D class you are using. Are you using something other than java.awt.geom.Point2D or javafx.geometry.Point2D? Because only java.awt.geom.Point2D has the Double() nested class, and only javafx.geometry.Point2D has the add() method.
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, my apologies.

I meant to use the JavaFX Point2D, but I seldom use JavaFX and I assumed the two (from Java and FX) would be basically identical. Indeed, it is in Java that Point2D is an interface. and Point2D.Double an implementation. In JavaFX Point2D is a class. So, at first glance I'd say: throw away everywhere I used that .Double.

Edit: highest time for me to go to bed... in Java Point2D is an abstract class, not an interface.... sorry for making a mess of this!  
 
Marshal
Posts: 65041
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but you have got the principles correct.
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . but you have got the principles correct.

You're not gonna tell me you were surprised, aren't you?    
 
Phil Patterson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. I am working on getting it right. I realized the method Piet gave me wasn't working so I looked it up and found the formula Campbell mentioned. I have modified my point-finding method to try and use it, but I'm having trouble figuring out why the planets aren't on the orbital paths.

Here is the method I use to find the points. There's a picture of my output below it.



It looks like both planets are about the same distance away from their orbital paths.

My output:

 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Phil Patterson wrote:Thanks everyone. I am working on getting it right. I realized the method Piet gave me wasn't working  (...)


I beg your pardon???      

Here's what I meant:

For instance:

Your planets are indeed far off from the circles. Can you show us your painting code and how you define your planets?
 
Phil Patterson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:

Phil Patterson wrote:Thanks everyone. I am working on getting it right. I realized the method Piet gave me wasn't working  (...)


I beg your pardon???      

Here's what I meant:

For instance:

Your planets are indeed far off from the circles. Can you show us your painting code and how you define your planets?



Whoops! I looked it over again, and I realized that I was giving the wrong distance value. I had a distance from the center of the sun, to the edge, and a distance from the edge of the sun to to orbital path. Instead of adding both, I only passed an argument for the distance from the edge of the sun to the orbital path. That's what was causing the planets to appear closer to the sun than they should have.

Sorry, that was a dumb mistake. Thanks for bearing with me. My randomizePlanetPosition() method works fine now.
 
Campbell Ritchie
Marshal
Posts: 65041
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Phil Patterson wrote:. . . a dumb mistake. . . . .

My very first post here, on 13th October 2005, had a similarly daft mistake in. We all make that sort of mistake; don't let it worry you.
 
Piet Souris
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, and as Campbell says, makng mistakes is what we all did and still do.

If you want, a slight change would be to make ellipses instead of circles. That would involve a small change to the formula, For instaance:

where 0 < factor is a factor that determines the exact shape of the eliips. The axes are aligned to the x- and y-axes, but you can use a Transform to rotate it, It does make the code a bit more complex, though.
 
Our first order of business must be this tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!