File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Creating a circle with N segments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating a circle with N segments" Watch "Creating a circle with N segments" New topic

Creating a circle with N segments

Eric Chang
Ranch Hand

Joined: Jan 27, 2004
Posts: 113
So I have no idea if this is the proper forum for this questions...but I'll give it a shot.
Lets say I am given a center point (as a Latitude/Longitude) and a radius in some unit (let's say km). And I'm also given a number of points to make up the circle (say 100). How the hell would I calculate the location of the points on the circle? And to make it more difficult (but this part isn't absolutely necessary I suppose), we are talking about a circle on the Earth, which means you wouldn't actually have a perfect circle without factoring in the curvature of the the earth, etc.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
By circle on the earth, you mean a small circle rather than a Great Circle, I presume.

I suggest you simplify things by making the following assumptions:
  • The Earth is a perfect sphere.
  • You are measuring the radius of the circle along the ground.
  • Both those assumptions are probably not true in real life.

    Simplify things even more:
  • Try a circle 300 in radius; this will fit nicely onto a normal computer screen at 1024X768.
  • Put it on a Swing Component, most likely a JPanel.
  • Paint it by drawing dots in the paintComponent() method.
  • Copy the Graphics object with its create() method.
  • Cast it to Graphics2D
  • Apply a translate(500, 350) to it.
  • Apply a scale(1.0, -1.0) as well.
  • If I have given you the correct numbers, you will now have your origin near the centre of the screen and positive numbers going up or right.
  • The angle of a circle is 2*PI, so divide 2*PI by the number of points round the circle.
  • Use a for-loop to iterate round the circle, drawing one point at a time. The location of the centre of the spot is (radius*cosA, radius*sinA) where A is the angle in radians you have reached.
  • That should keep you busy for at least the next 10 minutes. Then let's see if we can work something out for a curved surface . . .
    Eric Chang
    Ranch Hand

    Joined: Jan 27, 2004
    Posts: 113
    Hahah, yeah I was planning on making those assumptions if I went crazy trying to deal with the fact that the earth is not a perfect sphere.

    So maybe a little bit more background is needed to get the best solution...I'm writing a tool that will generate Flight Plan scenarios where my "planes" will be flying aimlessly in circles. So that is why I need my points in lat/lon format (degrees or DDMMHHSS...doesn't matter since I can convert those easily).
    So the tool will ask the user for the number of points that make up the circle (or rather, the number of "reports" the radar gets for the plane as it goes around in the circle) as well as the center point and the radius.
    I guess those assumptions are fine since it doesn't have to be a perfect circle on a curved surface.
    Bill Shirley
    Ranch Hand

    Joined: Nov 08, 2007
    Posts: 457

    Bill Shirley - bshirley -
    if (Posts < 30) JavaRanchFAQ);
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 46349
    A degree of latitude is 60 nautical miles at the equator. Well, I think it is; I was probably 8 years old when I last read that. I am assuming the world is a perfect sphere, so latitude degrees would be 60 n-miles everywhere, but longitude gets closer as you approach the poles.

    So, if your plane is 60 n-miles east of your present position, and you are at a latitude of 60�, that is

    1 * 60 / 60 / cos(60�)

    degrees east of you. Since cos(60�) is 0.5, you can work that out as 2� to your east.
    If the plane is 60 n-miles north of you, that is 1� to your north.

    If the plane is 100 n-miles away to the north-east, it is 70.7 n-miles to your east, and 70.7 n-miles to your north (sin45� = con45� = 0.7071 = �sqrt2).
    Now you will have to work out whether the lines of latitude are closer together as you go north and allow a bit of a correction. If you are at 60�N then that plane is 70.7 / 60 degrees north of you, and something like

    1 * 70.7 / 60 / cos(~61.2�)

    to your east. And if you really want to be more accurate, the whole thing will get even more complicated.

    By the way: when you work out sin and cos and tan, you assume 0� is on the right (3o'clock) and work round anti-clockwise, so 90� is top, 180� is left and 270� bottom.
    The Math.cos and Math.sin methods require their arguments in radians, but there is a degreesToRadians method in the Math class.

    I hope this has helped more than confused you
    I agree. Here's the link:
    subject: Creating a circle with N segments
    It's not a secret anymore!