Page 2 of 3
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 12:26 pm
by TylerH
GhostXoPCorp wrote:These trigonometric functions should be used for the curves around the angles of the rectangle. Read again and you will know that the curves are all i am talking about, straight lines in this situation are the least of concern as they should be easier to draw. However i have given advice on the curves portion of the rectangle.
Your failing at being a smart @$$ towards my answer. Please try again in a different response later
My failing? No, I think I've done a reasonably complete job of showing you how asinine your suggestion is.
So he arbitrarily switches between coordinate systems? Seems reasonalbe. So tell me, how do you set the (r,θ) pixel? Hint: It involves switching to rectangular coordinates. With that in mind, what is it that makes polar coordinates so attractive?
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 1:13 pm
by Brendan
Hi,
osmiumusa wrote:My problem seems simple - I can't figure out how to write a graphics routine that draws a rounded rectangle.
Last time I did it, first I split the rectangle into horizontal lines. Then, for the 'n' horizontal lines at the top and bottom I used a lookup table to determine how many pixels to skip on the left and on the right.
The end result looked a bit like this:
Code: Select all
void doRectangleWithSmoothCorners(int topY, int leftX, int bottomY, int rightX) {
int y, x;
for(y = 0; y < curveLines; y++) {
doLine(y + topY, leftX + lookupTable[y], rightX - lookupTable[y]);
}
for(y = topY + curveLines; y < bottomY - curveLines; y++) {
doLine(y, leftX, rightX);
}
for(y = curveLines - 1; y >= 0; y--) {
doLine(bottomY - y, leftX + lookupTable[y], rightX - lookupTable[y]);
}
}
The lookup table could be static (if the size of the corners doesnt' change) or could be calculated dynamically by using "sin()". I did it dynamically, but didn't want to assume an FPU was present (and therefore didn't want to use "sin" to generate the lookup table), so I used a spreadsheet to create a large static table (e.g. with 256 entries) and scaled the large table down to the size I needed.
Cheers,
Brendan
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 1:37 pm
by thomasloven
Kind of out of topic, but this made me think of a story from the childhood of the Macintosh.
http://folklore.org/StoryView.py?projec ... ywhere.txt
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 2:42 pm
by Combuster
And if you don't like lookuptables (or you want to make larger circles than your lookup table's length), something like this should do the job:
Code: Select all
// plot the four starting points
putpixel(x ± radius, y);
putpixel(x, y ± radius);
d1 = radius;
d2 = 0;
// the maximum error equals (x+.5)² = x² + x + .25
e = (radius * radius) + radius; // the squared radius of the bounding arc, so we don't need a sqrt.
// until arcs meet
while (d1 > d2)
{
d2++;
if (d2 * d2 + d1 * d1 >= e) d1--;
// plot eight arcpixels
putpixel(x ± d1, y ± d2); // for every +/- combination
putpixel(x ± d2, y ± d1);
}
For rounded rectangles, you obviously want gaps between each segment of the default circle.
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 3:18 pm
by VolTeK
MDM wrote:Your ideas work, however, they'd be inefficient, and I wouldn't recommend it for low level drawing code. You were rude, and you are still being rude. It doesn't make you look cool or intelligent, it makes you look obnoxious.
Im not trying to look cool, and if you think i was, you probably think i am, and kudos to you find someone else to look up to.
TylerH wrote:My failing? No, I think I've done a reasonably complete job of showing you how asinine your suggestion is.
So he arbitrarily switches between coordinate systems? Seems reasonalbe. So tell me, how do you set the (r,θ) pixel? Hint: It involves switching to rectangular coordinates. With that in mind, what is it that makes polar coordinates so attractive?
Dont question my suggestion, in fact you shouldnt care but to give a better one. Sometimes my ideas work better, and more then likely there are better alternatives. So go ahead, give them, but i dont want to hear about it. Your not the teacher, you can get a degree however for that. Until then prove your intelligence to me through work, not your suggestions. You can make something 1000 different ways, today im only giving a **** about the 1. That happens to be the one i suggested. I dont ask that you care, but if you give any improvements or lectures about it, dont tell me. Ive probably already thought about it.
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 3:39 pm
by Combuster
I just reported your post for unacceptable rudeness - just to prove that a 10x post count is a really really bad metric for being right.
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 3:41 pm
by VolTeK
Combuster wrote:I just reported your post for unacceptable rudeness
Thats nice
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 4:43 pm
by TylerH
GhostXoPCorp wrote:Dont question my suggestion, in fact you shouldnt care but to give a better one. Sometimes my ideas work better, and more then likely there are better alternatives. So go ahead, give them, but i dont want to hear about it. Your not the teacher, you can get a degree however for that. Until then prove your intelligence to me through work, not your suggestions. You can make something 1000 different ways, today im only giving a **** about the 1. That happens to be the one i suggested. I dont ask that you care, but if you give any improvements or lectures about it, dont tell me. Ive probably already thought about it.
You must be really mad, that post is garrulous and incoherent. Seeing as all this requires is basic trig, which was covered in precal, a class in which I got a 100, I could be the teacher. And, "you're" is the contraction for "you are;" "your" is the possessive form of "you."
Code: Select all
void set_pixel(int x, int y);
void round_rectangle(int x, int y, int width, int height, int radius)
{
for(int i = 0;i < width - 2 * radius;i++)
{
set_pixel(x + radius + i, y);
set_pixel(x + radius + i, y + height);
}
for(int i = 0;i < height - 2 * radius;i++)
{
set_pixel(x, y + radius + i);
set_pixel(x + width, y + radius + i);
}
for(int i = 0;i <= radius;i++)
{
set_pixel(x + i, (int)(y + radius - radius * sin(i * (pi/(2 * radius))));
set_pixel(x + i, (int)(y + height - radius + radius * sin(i * (pi/(2 * radius))));
set_pixel(x + width - radius + i, (int)(y + radius * sin(i * (pi/(2 * radius))));
set_pixel(x + width - radius + i, (int)(y + height - radius * sin(i * (pi/(2 * radius))));
}
}
Where's your polar coordinate based solution? Also, notice that I only used sine; there goes one of the proposed bonuses of polar.
To OP: I'm assuming you're using a top-left based coordinate system. If this is a problem, let me know. There may be some overlap by 1. If there is, tell me, and I'll fix it.
EDIT: I forgot to include the declaration of set_pixel.
Re: Rounded rectangles
Posted: Sun Apr 10, 2011 5:46 pm
by VolTeK
Once again, you over look my solution. I merely suggested, and i do not offer that you care about it. You are taking what i made and lecturing my on perfecting it. I do not ask that you perfect it, but to ignore it if you do not like it.
However, just using sine is a very nice alternative and more then likely faster. You didnt need to include my suggestion but to ignore it and post your alternative.
My polar coordinate simply requires 4 lines, and 4 angles to use parts of a circle. An easy and very simple to understand solution to this problem.
No i wont post code. Concept has better value
Re: Rounded rectangles
Posted: Mon Apr 11, 2011 2:18 am
by Combuster
It's Bresenham, and it's
not his algorithm
Re: Rounded rectangles
Posted: Mon Apr 11, 2011 3:09 pm
by osmiumusa
TylerH,
Your almost spot on! I've attached what happens in my little testing bed when it tries to draw the rounded rectangle. It does the first corner very well. The arguments were (90,90,200,150,6)
Re: Rounded rectangles
Posted: Tue Apr 12, 2011 2:13 am
by Solar
osmiumusa wrote:I've attached what happens in my little testing bed when it tries to draw the rounded rectangle.
Is that the code using sin()?
In that case, if you're satisfied with the result,
don't bother to go into trigonometrics. What you have there is not a "round" corner, you just did two 45 degree "turns" instead of one 90 degree one. Which looks acceptable, and is
loads faster and more precise if done directly instead of through some detour including sin() calculations.
Re: Rounded rectangles
Posted: Wed Apr 13, 2011 5:52 am
by osmiumusa
The problem with taking 2 45 degree turns is that when you to up into the higher radii, it will look sloppy. Trig functions see relatively slow but a small price to pay for quality in the rounded rectangle.
Re: Rounded rectangles
Posted: Wed Apr 13, 2011 7:21 pm
by TylerH
I have a shitload of hw to do, so I likely won't get around to it for a few days. Try to see if you can fix it.
Re: Rounded rectangles
Posted: Fri Apr 15, 2011 5:51 am
by osmiumusa
Meh. So do I, but I'll work on it in my spare time. Tell me if you fixed it before I did.