Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Trying to make a shape.

Atrus Greyor
Ranch Hand
Posts: 110
With the following code, I get some odd stuff. Not only are shapes really small, but with items larger then 3 sides, the shapes are really convoluted.

And the code I'm using to drive it....

Campbell Ritchie
Marshal
Posts: 55715
163
You are using the wrong parameters for Math.sin and Math.cos.
That is one thing I have noticed. There might be more.

Campbell Ritchie
Marshal
Posts: 55715
163
. . . and the try-catch is inappropriate. An if-else would work better.

Campbell Ritchie
Marshal
Posts: 55715
163
. . . and the (Polygon) class cast is incorrect.

Campbell Ritchie
Marshal
Posts: 55715
163
You are doing your arithmetic in a very convoluted fashion. Don't use pow(x, 2) for squares, use x * x. It is not obvious why you are squaring something, taking its square root and then halving it.

And a size of 20 is liable to be very small regardless.

Atrus Greyor
Ranch Hand
Posts: 110

I'm sorry about the small size comment, I was thinking of previous examples where I had the size at 500.

And why is the polygon cast incorrect in this instance? Note that I only put it there to test how many points there were, and is not a permanent feature, it would be removed.

Why would an if-else work better? I mean, not only does the try-catch solve the problem of creating a circle vs a polygon, it also handles when the user puts in less then 3 sides.

And my math at this point? I am trying to find the center, assuming that the shape has be circumscribed around the shape I am attempting to create. As such I use the distance formula. That will give me the diameter of the circle....and since I know the size, I just realized that using all that math is such a waste and the center is just (size/2, size/2).

And yes, it should be double i not int i...

And, Campbell Ritchie is right, Math.sin() and Math.cos() take the degree measurement in radians not as degrees as I was putting them in.

That fixes my problem, however I would still like some answers to my previous questions.

Thanks guys!

Campbell Ritchie
Marshal
Posts: 55715
163
You will have to experiment with the size; maybe 100 will work better. Remember the size is in pixels, not mm.

It is very inefficient to throw an Exception and catch it in the same method; an Exception is to tell the calling method that a call was unsuccessful. You can do it much more efficiently by
if (sides < 3)
. . . Ellipse2D . . .
else
. . . Polygon . . .

If you really don't want to handle less than 3 sides, put that in the documentation for the method and throw an IllegalArgumentException (not NumberFormatException) if (sides < 3). Then let the calling method handle the Exception.

Class-casting is very error-prone. What happens if you pass 2 sides and return an Ellipse2D? An Exception. If you really need a cast, put it after the instanceof bit . . . ((Polygon) s).npoints . . .

Are you getting the centre point correctly from size / 2? You can always change that. It's a lot easier when you can simplify things.

And beware of floating-point arithmetic in for loops. Suggest you change the loop to
for (int i = 0; i < sides; i++)
. . . sin(2 * PI * i / sides) . . .
I tried your loop with sides = 6 and it printed out 7 points. That is because all floating-point arithmetic is prone to imprecision. Try this line (from the JavaRanch FAQ):
System.out.println(1.0 - 1.0 / 3.0 - 2.0 / 3.0);

You have obviously got it to work. Well done. And sorry that I was rather rude this morning.

Atrus Greyor
Ranch Hand
Posts: 110
Oh don't worry, you were not rude.

And, I am aware that casting is prone to errors, how ever that cast was temporary, just for the testing of the code, since I knew that I was only testing the polygon aspect. It has been removed now that it works.

Thanks though, I'll take what you said into consideration.

Campbell Ritchie
Marshal
Posts: 55715
163
You're welcome

Bill Shirley
Ranch Hand
Posts: 457
For what it's worth, below is my re-coding (this is not a code mill, he already had a solution - and seeing other's is a useful thing).

I'm not too familiar with AWT/Swing, and it seems to be refreshing the canvas after my polygon is written. I'm not sure if that's my environment, or my code.

There are other things I would change that I didn't were I going to be maintaining this code.

 Don't get me started about those stupid light bulbs.