• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to make a shape.

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and the try-catch is inappropriate. An if-else would work better.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and the (Polygon) class cast is incorrect.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your for-loop, adding doubles to ints won't work properly.
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not trying to be argumentative, but I'd like more answers please.

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
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic