Creating Rounded Square

Discussion about using Moai SDK - post questions, bugs and issues here.

Moderators: seebs, franciscotufro

Creating Rounded Square

Postby zandegran » Mon Jul 16, 2012 6:37 am

Hi ppl,
I need to create a square or polygon with rounded corners..
I need an attribute to be adjusted may be from 0 to 1..
0 will change the square to circle and 1 will keep it as a square.

Is there a way to create a shape, which is the difference of two shapes??
If this is possible, then I can create any shapes with rounded edges and allows me to solve some other problems as well.
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: Creating Rounded Square

Postby dana » Mon Jul 16, 2012 1:42 pm

I don't think there's a simple solution to creating a rounded square programmatically. The way I have in mind would be to draw 2 rects that form a plus-shape, then put circles in each corner. I'm terrible at art, but I attempted to put together an image to demonstrate what I mean, hopefully you can get the idea (sorry it's so bad!).

Image

The bounds of each shape would be fairly easy to compute using the 0 to 1 attribute you mentioned. That attribute can represent the radius of the circle, so 0 would draw no circle and the two boxes would overlap to make a square, 1 would set the radius to half the square's width and then all 4 circles would overlap and it'd make a circle.
User avatar
dana
Site Admin
 
Posts: 200
Joined: Fri Mar 02, 2012 6:10 pm
Location: Zipline Games

Re: Creating Rounded Square

Postby Mud » Mon Jul 16, 2012 2:33 pm

That was my first thought, too.

Code: Select all
  1. function drawRoundedRect(x, y, width, height, radius, circlestep)

  2.   circlestep = circlestep or 32

  3.   local hw, hh = width/2, height/2

  4.  

  5.   MOAIDraw.fillRect( x-hw+radius, y-hh,        x+hw-radius, y+hh )

  6.   MOAIDraw.fillRect( x-hw,        y-hh+radius, x+hw,        y+hh-radius )

  7.   MOAIDraw.fillCircle( x-hw+radius, y-hh+radius, radius, circlestep )

  8.   MOAIDraw.fillCircle( x-hw+radius, y+hh-radius, radius, circlestep )

  9.   MOAIDraw.fillCircle( x+hw-radius, y-hh+radius, radius, circlestep )

  10.   MOAIDraw.fillCircle( x+hw-radius, y+hh-radius, radius, circlestep )

  11. end



For instance:

Code: Select all
  1. drawRoundedRect( 0,  100, 200, 100, 30 )

  2. drawRoundedRect( 0,   20, 100,  26,  5 )

  3. drawRoundedRect( 0, -110, 100, 200, 20 )



Would produce:

RoundedRect.PNG
RoundedRect.PNG (3.13 KiB) Viewed 622 times


Which internally looks like:
Attachments
RoundedRectReveal.PNG
RoundedRectReveal.PNG (4.54 KiB) Viewed 622 times
Mud
 
Posts: 57
Joined: Sun Jan 29, 2012 9:54 pm

Re: Creating Rounded Square

Postby zandegran » Tue Jul 17, 2012 2:19 am

Thank you guys,
It is sad that a shape that is a overlapping area of two shapes cannot be created :(
I can use this, but I need to adjust the alpha of these shapes (the rounded rect).
With this it is problematic..

are you sure that there is noway of masking or doing something?
In the attachment there are two circles one with four sides and another red with 200 sides
if I can mask the blue with the red one then I can create a rounded square by reducing the radius of the red circle(mask)
Attachments
Untitled.png
Square in Circle
Untitled.png (9.23 KiB) Viewed 602 times
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: Creating Rounded Square

Postby makotok1123 » Tue Jul 17, 2012 7:41 am

Hello.
I tried to implement to try.

https://github.com/makotok/Hanappe/blob ... aphics.lua
https://github.com/makotok/Hanappe/blob ... sample.lua

How to implement.
Draw a line around the triangle function.
Fill in the trigonometric functions as well.
Attachments
RoundRect.png
RoundRect.png (23.96 KiB) Viewed 588 times
makotok1123
 
Posts: 177
Joined: Fri Jan 06, 2012 11:31 am

Re: Creating Rounded Square

Postby zandegran » Tue Jul 17, 2012 8:42 am

This is what I need Thank you :)
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: Creating Rounded Square

Postby dana » Tue Jul 17, 2012 11:13 am

Thanks for contributing that makotok, it looks good!
User avatar
dana
Site Admin
 
Posts: 200
Joined: Fri Mar 02, 2012 6:10 pm
Location: Zipline Games


Return to Moai SDK

Who is online

Users browsing this forum: No registered users and 1 guest

x