Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Probem with making a ring of circles in java?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kratos321




PostPosted: Sat May 28, 2011 10:27 am   Post subject: Probem with making a ring of circles in java?

ok so i wrote a program which with a fixed orbital radius, makes a ring of circles depending on the user's input of orbit size and number of circles. So, the individual circles around the orbit will be smaller if there:confused: are more of them. It works ok with some most number of circles but for some, there is either an overlap or gap in the circle ring. I think this is because I am rounding and casting all as integer so that it can be compatible with my drawCircle method which calls drawOval. So is there any way to bypass this? Or is there a drawOval method which can have double data type paramaters and not just integers? any other suggestions please?

thanks in advance. Neutral
Sponsor
Sponsor
Sponsor
sponsor
RandomLetters




PostPosted: Sat May 28, 2011 10:36 am   Post subject: RE:Probem with making a ring of circles in java?

the draw commands take integers because they represent individual pixels.

If they took decimals as arguments, it would still have to eventually be rounded or blended to map to each pixel.

I think the problem may be your algorithm.

Keep in mind that in a ring, circles are not touching each other at opposite sides, but slightly towards the center.
..OO
O... O
chrisbrown




PostPosted: Sat May 28, 2011 4:38 pm   Post subject: Re: Probem with making a ring of circles in java?

If you're talking about 1-pixel gaps or overlap, RandomLetters is right and that's just a consequence of how drawing works. You might be able to tweak things a little to minimize it, but it's not really worth the effort.

You might run into some serious overlap if you're not careful with your math. If so, this diagram and knowledge of the chord will help.
DemonWasp




PostPosted: Sat May 28, 2011 11:27 pm   Post subject: RE:Probem with making a ring of circles in java?

If you're seeing gaps larger than a pixel or two, then you're probably rounding way too early. Do all the position arithmetic (especially multiplications) with real numbers (float or double). When you draw, cast them to integers. If you cast to integers first, you can get a huge amount of error.

Example: Calculate the nearest integer value to 200 * ( 6 / 7 )

Using doubles:
6 divided by 7 is: 0.857...
multiplied by 200: 171.428571...

Using ints:
6 divided by 7 is: 0
multiplied by 200: 0

So even with this relatively simple calculation, storing intermediate values in integers resulted in essentially infinite error. You can probably imagine that if the centers of the circles are to be at theta = i * ( 360 / circleCount ) degrees, then you could have potentially a rather large error:

code:

circleCount = 7
i   correct     with ints
0   0           0
1   51.4 = 51   51
2   102.9 = 103 102
3   154.3 = 154 153
4   205.7 = 206 204
5   257.1 = 257 255
6   308.6 = 309 306


And of course, the more calculations you do with less-than-correct numbers, the bigger the errors will get
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: