Dark Bit Factory & Gravity
PROGRAMMING => Other languages => Blitz => Topic started by: mike_g 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.
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
-
Cheers for the cool and handy snippet dude.
Clyde.
-
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.
-
Here's one I did that draws rounded corners on triangles in yabasic. Hey look, 4 1/2 years ago!
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
-
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.