Rounded rectangles [Finally Solved, see last post]

Programming, for all ages and all languages.
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: Rounded rectangles

Post 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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Rounded rectangles

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Re: Rounded rectangles

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Rounded rectangles

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Rounded rectangles

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Rounded rectangles

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Rounded rectangles

Post by VolTeK »

Combuster wrote:I just reported your post for unacceptable rudeness
Thats nice
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: Rounded rectangles

Post 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.
Last edited by TylerH on Sun Apr 10, 2011 6:10 pm, edited 1 time in total.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Rounded rectangles

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Rounded rectangles

Post by Combuster »

It's Bresenham, and it's not his algorithm :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
osmiumusa
Posts: 20
Joined: Sun Dec 12, 2010 5:47 pm

Re: Rounded rectangles

Post 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)
Attachments
Test number 1
Test number 1
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Rounded rectangles

Post 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.
Every good solution is obvious once you've found it.
osmiumusa
Posts: 20
Joined: Sun Dec 12, 2010 5:47 pm

Re: Rounded rectangles

Post 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.
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: Rounded rectangles

Post 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.
osmiumusa
Posts: 20
Joined: Sun Dec 12, 2010 5:47 pm

Re: Rounded rectangles

Post 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. :mrgreen:
Post Reply