Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing a Diamond Shape (like Rectangle, Circle)

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??

 
Ryan McGuire
Ranch Hand
Posts: 1072
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan McGuire
Ranch Hand
Posts: 1072
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne,
That code looks familiar.

Ryan
 
Jeffrey Chu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about using draw polygon....
 
Ryan McGuire
Ranch Hand
Posts: 1072
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic