Two Laptop Bag*
The moose likes Beginning Java and the fly likes Drawing a Diamond Shape (like Rectangle, Circle) 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 » Beginning Java
Bookmark "Drawing a Diamond Shape (like Rectangle, Circle)" Watch "Drawing a Diamond Shape (like Rectangle, Circle)" New topic
Author

Drawing a Diamond Shape (like Rectangle, Circle)

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
I have a code that I had once used to draw a Diamond for one of my assignments. At the time I designed the code I felt really stupid coding the stupid code . But now after a long time after the assignment I feel that I need to improve the sharpness of my logic. Could anyone suggest a better alternative to drawing a Diamond Shape??



With Best Regards,
Shyam Prasad Murarka
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Holy macaroni!

I notice that the horizontal distance between your two defining points (abs(x2-x1)) defines the width of the diamond, but the diamond is shifted left or right so that the diamond has one vertex at point1 and point2 isn't even in the diamond's bounding box. (For instance, if point1 is at (10,20) and point2 is at (14, 26) the diamond points are at (10,20), (12,23), (14,20), and (12,17).) Is that what you want?

If so the following should simplify your a little.


If the diamond can be centered in a box that has the two points as opposite corners, the following is even simpler code.



You really don't need the four separate if/then/else blocks. The math just works out in both cases regardless of the relative positions of the two points.

Ryan
[ May 06, 2005: Message edited by: Ryan McGuire ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
Ryan, thanks a million for those pieces of code. Now I seem to be thinking was it really that simple? Anyways, I think I wanted the first piece of code at first but after looking at the second code I thought why not actually do it this way, because even Ovals are bounded inside a Rectangle.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
Ryan, thanks a million for those pieces of code. Now I seem to be thinking was it really that simple? Anyways, I think I wanted the first piece of code at first but after looking at the second code I thought why not actually do it this way, because even Ovals are bounded inside a Rectangle.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Shyam Murarka:
Dear Readers,
Ryan, thanks a million for those pieces of code. Now I seem to be thinking was it really that simple? Anyways, I think I wanted the first piece of code at first but after looking at the second code I thought why not actually do it this way, because even Ovals are bounded inside a Rectangle.


Typically, geometric problems like this can take use of some basic mathematics. In the future, I suggest that you get out some graph paper and draw things out by hand. Then try to figure out if there are any mathematical forumlae that will help simplify things.

For instance, in code Ryan gave above, he realized that the vertices for the diamond are half way between the vertices of the bounding box. I'm not entirely sure what the line "x -= (x2-x1);" is for. I think I could draw the diamond without doing that:

Note that I haven't tested this code, so I'm only 90% confident that it works. I figured this out by labeling the vertices of the bounding rectangle described by (x1, y1) and (x2, y2) and the vertices of the diamond. This makes it so you can just calculate the necessary values for x and y in the code above and then draw 4 lines.

HTH

Layne


Java API Documentation
The Java Tutorial
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Layne,
That code looks familiar.

Ryan
Jeffrey Chu
Greenhorn

Joined: May 01, 2005
Posts: 10
how about using draw polygon....


I'm sorry if i break any rules, im new, please bear with me while I learn the rules.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by Jeffrey Chu:
how about using draw polygon....


A reasonable option. What I disliked about a drawPolygon() in this particular case is that you'd have to declare a couple int arrays and fill them in before calling drawPolygon(). I'm so lazy.

However, I'd guess that doing that would be slightly faster than calling drawLine() four times. And that may be important if you're drawing the diamond hundreds/thousands of time in an animation loop.

Ryan
[ May 08, 2005: Message edited by: Ryan McGuire ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ryan McGuire:
Layne,
That code looks familiar.

Ryan


Doh! It would help if I finished reading your post, eh? Somehow I missed that you posted the exact same thing...

Layne
 
 
subject: Drawing a Diamond Shape (like Rectangle, Circle)
 
Similar Threads
drawRect() issues
Koch Curve
New College Project
Problem with paint method.
Using mouse clicks to draw on a JFrame, problem probably with coordinates