Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Rounded Rect — Gideros Forum

Rounded Rect

unclekinguncleking Member
edited April 2014 in Code snippets
Extended from http://giderosmobile.com/forum/discussion/346/snippet-arccircleellipse
function getCorner(shape, startAngle, xOffset, yOffset, radius)
 
	-- simple check to simplify the logic of rect.
	if(radius  == 0 ) then return end
 
        -- can be further optimised !
	local xRadius = radius
	local yRadius = radius
	local sides = radius
	local arcAngle = 0.25	-- always 90 degrees for the rounded rect.
 
        -- increase or decrease this as per accuracy required. This works fine for me atleast
	local angleStep = arcAngle / sides
 
	local xx = math.cos(startAngle*2*math.pi) * xRadius + xOffset
	local yy = math.sin(startAngle*2*math.pi) * yRadius + yOffset
 
 
	for i = 1,sides do
	    local angle = startAngle + i * angleStep
 	        shape:lineTo((math.cos(angle*2*math.pi) * xRadius) + xOffset, (math.sin(angle*2*math.pi) * yRadius) + yOffset)
	    end
end
 
 
-- lots of variables here are left as exercise for coder.
 
        local backgroundColor = 0xff00ff
        local borderColor = 0x00ff00
-- change these as per your liking
-- they all can be different
        local borderTopLeftRadius = 20
        local borderTopRightRadius = 30
        local borderBottomLeftRadius = 40
        local borderBottomRightRadius = 0
 
-- width of rectangle
        local width = 300
-- height of rectangle
        local height = 200
 
-- The actual shape is here
	local shape = Shape.new()
	shape:setFillStyle(Shape.SOLID, backgroundColor)
	shape:setLineStyle(borderWidth, borderColor)
	shape:beginPath()
 
	shape:moveTo(0, 0)
	-- top left
	shape:moveTo(borderTopLeftRadius, 0)
	shape:lineTo(width - borderTopRightRadius, 0)
 
        -- top right corner
	getCorner(shape, 0.75, width - borderTopRightRadius, borderTopRightRadius , borderTopRightRadius)
	-- line from top right corner to bottom.
	shape:lineTo(width, height - borderTopRightRadius - borderBottomRightRadius)
 
        -- bottom right corner
	getCorner(shape, 0, width - borderBottomRightRadius, height - borderBottomRightRadius, borderBottomRightRadius)
 
	-- line to bottom left corner
	shape:lineTo(0 + borderBottomLeftRadius, height)
 
	-- bottom left corner
	getCorner(shape, 0.25, borderBottomLeftRadius, height - borderBottomLeftRadius, borderBottomLeftRadius)
 
	-- line to top left corner
	shape:lineTo(0, borderTopLeftRadius)
	-- top left corner
	getCorner(shape, 0.5, borderTopLeftRadius, borderTopLeftRadius, borderTopLeftRadius)
 
        shape:endPath()
 
    -- add the shape to stage or parent sprite and rotate / animate / position like any other shape.
         stage:addChild(shape)

Likes: ar2rsawseen

+1 -1 (+1 / -0 )Share on Facebook

Comments

Sign In or Register to comment.