This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Trying to make a shape. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trying to make a shape." Watch "Trying to make a shape." New topic
Author

Trying to make a shape.

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
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
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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

Joined: Oct 13, 2005
Posts: 37926
    
  22
. . . and the try-catch is inappropriate. An if-else would work better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
. . . and the (Polygon) class cast is incorrect.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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

Joined: Mar 07, 2004
Posts: 110
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

Joined: Oct 13, 2005
Posts: 37926
    
  22
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

Joined: Mar 07, 2004
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
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
You're welcome
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
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.



Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to make a shape.
 
Similar Threads
its a classic! help with area of shapes!
AffineTransform Rectangle2D
A Paint Application with Combo Boxes and Buttons
How do read a double datatype 2D array into your applet code from a text file