posted 13 years ago

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

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

posted 13 years ago

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 ]

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

posted 13 years ago

Dear Readers,

Ryan,

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.*With Best Regards,

Shyam Prasad Murarka

Shyam Prasad Murarka

Ranch Hand

Posts: 209

Dear Readers,

Ryan,

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.*With Best Regards,

Shyam Prasad Murarka

posted 13 years ago

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

Originally posted by Shyam Murarka:

Dear Readers,

Ryan,thanks a millionfor those pieces of code. Now I seem to be thinkingwas it really that simple?Anyways, I think I wanted the first piece of code at first but after looking at the second code I thoughtwhy 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: 1149

9

posted 13 years ago

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 ]

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 ]