
Rounded Rects 
juvinious
Member #5,145
October 2004

I've been struggling with accomplishing this probably simple issue for some time now and can't figure it out. I don't understand the math in connecting 4 lines with arcs due to my limited knowledge in math.
I'd like to be able to have my round rect routine to be something like: void doRoundRect(int x, int y, int width, int height, float radius) { doLine(x+radius, y, x+widthradius, y); doLine(x+radius, y+height, x+widthradius,y+height); doLine(x, y+radius,x, y+heightradius); doLine(x+width, y+radius,x+width, y+heightradius); // Needs arcs } Perhaps my arc routine is bad? Any suggestions? __________________________________________ 
orz
Member #565
August 2000

I haven't tried running your code or anything yet, but a few comments: enum {NUM_ARC_SEGMENTS=4}; doArc(x+radius, y+radius, NUM_ARC_SEGMENTS, radius, 180, 90); doArc(x+radius, y+heightradius, NUM_ARC_SEGMENTS, radius, 90, 90); doArc(x+widthradius, y+radius, NUM_ARC_SEGMENTS, radius, 270, 90); doArc(x+widthradius, y+heightradius, NUM_ARC_SEGMENTS, radius, 0, 90);
2. You're doArc is a bit weird I think. The problem is that you use angle as both the angle from the center to the edge of the circle and as the angle along the edge of the circle  these two angles are supposed to be perpendicular to each other, not the same. changes to Whereas, here's how to change it to use the old concept with the correct angles: change so: float angle = startAngle * 3.14159265 / 180; // initial angle in radians float angleInc = sweep * 3.14159265 /(180 * segments); // angle increment float cx = centerX + radius * cos(angle), cy = centerY + radius * sin(angle); float oldcx = cx, oldcy = cy; angle += 3.14159265/2; radius /= segments;//not sure about this line...
actually... I think screwed that one up... but that's the idea : ) 
vbovio
Member #8,283
January 2007

void roundRect(BITMAP *bmp, int x1, int y1, int x2, int y2, int r, int c) { hline(bmp, x1+r, y1, x2r, c); hline(bmp, x1+r, y2, x2r, c); vline(bmp, x1, y1+r, y2r, c); vline(bmp, x2, y1+r, y2r, c); arc(bmp, x1+r, y1+r, itofix(64), itofix(128), r, c); arc(bmp, x2r, y1+r, itofix(0), itofix(64), r, c); arc(bmp, x1+r, y2r, itofix(128), itofix(192), r, c); arc(bmp, x2r, y2r, itofix(192), itofix(256), r, c); }
 
orz
Member #565
August 2000

Heh... vbovio's response is far more useful if you just want it to work : ) 
DanielH
Member #934
January 2001

This is what I made a while ago and it looks just like vbovio's

juvinious
Member #5,145
October 2004

vbovio: I'm not using allegro, although usefull in terms of getting it done it doesn't help me in the understanding of my problem. Specifically in regards to creating an arc. orz: Thanks for the input, the suggestions you provided gave me better results with my dealing with the problem, though I'm still not seeing it. edit: oops had a problem, it's there now... It's all in blue edit2: that looks bad, I uploaded one in white __________________________________________ 
orz
Member #565
August 2000

Are you trying to get a working function, or to understand the math of an arcdrawing function? For a working function, just use vbovio's code. For the math of an arcdrawing function: the first fix I proposed contained 2 errors that I've noticed: should be: The idea here is that we break up our arc of D degrees into N subarcs of D/N degrees, and we aproximate each subarc with a straight line connecting the endpoints. Each endpoint is calculated separately. The second, alternative fix I proposed contains... flaws... The idea of the second method was generally similar... 
juvinious
Member #5,145
October 2004

orz: Ah ok I misread what you were saying, but now I get what you were conveying. Thanks for the help, it works as expected now. __________________________________________ 
Fladimir da Gorf
Member #1,565
October 2001

You could just take the rounded rectangle rendering code from OpenLayer OpenLayer has reached a random SVN version number ;)  Online manual  Installation video! MSVC projects now possible with cmake  Now alvailable as a DevC++ Devpack! (Thanks to Kotori) 
juvinious
Member #5,145
October 2004

Heh, well I looked at Rectangle.cpp if that's what you are talking about. Either I didn't see it or couldn't figure it out. __________________________________________ 
Fladimir da Gorf
Member #1,565
October 2001

Search for the part which begins: if( roundness > 0.0 ) OpenLayer has reached a random SVN version number ;)  Online manual  Installation video! MSVC projects now possible with cmake  Now alvailable as a DevC++ Devpack! (Thanks to Kotori) 
Mr. Big
Member #6,196
September 2005

You can modify the equation of the circle to create rounded rectangles. 
vbovio
Member #8,283
January 2007

look for SuperEllipse equation; and SuperQuadrics in general, you can find interesting shapes using their equations, rounded rects can be achieved with superellipses.  
