Author Topic: Rounded Rect Function[BB2D]  (Read 3190 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Rounded Rect Function[BB2D]
« on: January 02, 2007 »
Heres a rounded rect function I made with a little animated demo.

The first 4 parameters are same as the standard rect command.

The parameter "round" should be between 0 and half width or height, whichever is shortest. This dtermines how rounded the coerners are. Setting it higher gives some bizzare looking effects.

The col parameter is the argb colour. I might also add a fill parameter too sometime.
Code: [Select]
inc = 1
SetBuffer BackBuffer()
While Not KeyHit(1)
Cls
ROUNDED_RECT(10, 10, 100, 100, r, $FFFFFF)
r = r + inc
If r >= 50 Or r <=0 Then inc = -inc
Flip
Wend

Function ROUNDED_RECT(x_pos, y_pos, width, height, round, col)
   ;DRAW CORNERS
   d = 3 - ((round) Shl 1)
   x = 0
   y = round
   cx1 = x_pos + round: cy1 = y_pos + round
   cx2 = x_pos +width- round: cy2 = y_pos + round
   cx3 = x_pos +width- round: cy3 = y_pos + height- round
   cx4 = x_pos +round: cy4 = y_pos + height- round

   LockBuffer
   Repeat
  ;TOP LEFT CORNER
      WritePixel cx1 - x, cy1 - y, col
      WritePixel cx1 - y, cy1 - x, col

  ;TOP RIGHT CORNER
      WritePixel cx2 + x, cy2 - y, col
      WritePixel cx2 + y, cy2 - x, col
 
      ;BOTTOM RIGHT CORNER
  WritePixel cx3 + x, cy3 + y, col
  WritePixel cx3 + y, cy3 + x, col

  ;BOTTOM LEFT CORNER
  WritePixel cx4 - x, cy4 + y, col
  WritePixel cx4 - y, cy4 + x, col

      If d < 0 Then
         d = d + (x Shl 2) + 6
      Else
         d = d + ((x-y) Shl 2) + 10
         y = y - 1
      End If
      x = x + 1
   Until x > y
   UnlockBuffer
   
   For x = x_pos+round To x_pos+width-round
WritePixel x, y_pos, col
   Next
   For x = x_pos+round To x_pos+width-round
WritePixel x, y_pos+height, col
   Next
   For y = y_pos+round To y_pos+height-round
WritePixel x_pos, y, col
   Next
   For y = y_pos+round To y_pos+height-round
WritePixel x_pos+width, y, col
   Next
End Function
« Last Edit: July 21, 2007 by Shockwave »

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Rounded Rect Function
« Reply #1 on: January 02, 2007 »
Cheers for the cool and handy snippet dude.
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Rounded Rect Function
« Reply #2 on: January 03, 2007 »
Hey youre welcome. I had a short go at doing a filled version, got a bit confused and gave up. Maybe I'll get back to it tomorrow, i'm sure it cant be that hard.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Rounded Rect Function
« Reply #3 on: January 03, 2007 »
Here's one I did that draws rounded corners on triangles in yabasic.  Hey look, 4 1/2 years ago!

Code: [Select]
rem Rounded triangles thing V2 (no circle command), by Jim Shaw 7/8/2002

open window 640,512
repeat
setrgb 1,ran(255),ran(255),ran(255)
rounded_tri(ran(640),ran(512),ran(640),ran(512),ran(640),ran(512),int(ran(10))+1)
until (peek("port1")<>0)
end

sub rounded_tri(x0,y0,x1,y1,x2,y2,r)
local t,dx,dy,a

' fill circle x0,y0,r
' fill circle x1,y1,r
' fill circle x2,y2,r

docircle(x0,y0,r)
docircle(x1,y1,r)
docircle(x2,y2,r)

fill triangle x0,y0 to x1,y1 to x2,y2

if (((x1-x0)*(y2-y0) - (x2-x0)*(y1-y0)) < 0) then
t = x1
x1 = x2
x2 = t
t = y1
y1 = y2
y2 = t
fi

a = -atan(x1-x0,y1-y0)
dx =r*cos(a)
dy =r*sin(a)
fill triangle x0,y0 to x0+dx,y0+dy to x1,y1
fill triangle x0+dx,y0+dy to x1,y1 to x1+dx,y1+dy

a = -atan(x2-x1,y2-y1)
dx =r*cos(a)
dy =r*sin(a)
fill triangle x1,y1 to x1+dx,y1+dy to x2,y2
fill triangle x1+dx,y1+dy to x2,y2 to x2+dx,y2+dy

a = -atan(x0-x2,y0-y2)
dx =r*cos(a)
dy =r*sin(a)
fill triangle x2,y2 to x2+dx,y2+dy to x0,y0
fill triangle x2+dx,y2+dy to x0,y0 to x0+dx,y0+dy
end sub

sub docircle(x,y,r)
local ox,oy,nx,ny,z
ox = r
oy = 0
for z = 1 to 8
nx = r*cos(z/8*2*3.14159)
ny = r*sin(z/8*2*3.14159)
fill triangle x,y to x+ox,y+oy to x+nx,y+ny
ox = nx
oy = ny
next z
end sub

Jim

Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Rounded Rect Function
« Reply #4 on: January 04, 2007 »
Just downloaded yabasic. I was going to give it a go but couldent paste the code in :-[

Edit: Oh I see it tells me how to do it in the documentation.
« Last Edit: January 04, 2007 by mike_g »