Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Drawing a Diamond Shape (like Rectangle, Circle)

Ranch Hand
Posts: 209
• Number of slices to send:
Optional 'thank-you' note:
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??

Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Ranch Hand
Posts: 209
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 209
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 3061
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Shyam Murarka:
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
Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:
Layne,
That code looks familiar.

Ryan

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:

Ryan McGuire
Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Jeffrey Chu:

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
• Number of slices to send:
Optional 'thank-you' note:

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

 I'm all tasted up for a BLT! This tiny ad wants a monte cristo! Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat