PDA

View Full Version : How to check if drawn shape is circle?



psych
August 15th, 2010, 05:58 AM
Hi,

Let's say i have a simple app where on mouse move user draws a shape. What i am trying to accomplish is to detect if drawn shape is circle so was wondering what would be the best or easier way to do that?

I believe i should do some math based on every drawn point coordinates but i need a push in right direction from more experienced people around here so any advice would be appreciated :)

Cheers

Sirisian
August 16th, 2010, 01:37 AM
Take samples from the drawn circles. I assume they're points. Take the average position of all the points and call this the center. Find the average distance to the center from each point. (point - center).Length() for each point then divide by the number of points. Call this the radius. Then find the error of each point. (point - center).Length() - radius. That value should be less than a percentage of the radius. The next step is to find out if the points are in a clockwise or counter clockwise ordering. (The points around the center will begin at the first point then go clockwise or counter clockwise but they'll never go back the other way, if that makes sense).

So in general what I've described is that you need to find out if first the points are in a general circular area. Then the next step is to make sure that person drew a circle by ensuring the points are going clockwise or counter-clockwise.

Depending on what shapes you want to detect this can be hard. I mean a square at certain sizes looks like a circle, but if all you need is a circular shape, then what I described works.

psych
August 16th, 2010, 05:04 AM
Thank you for such a detailed explanation, i'll try to convert the algoritm to as 3.0 code and see how it goes. Thanks once again.

Cheers

bluemagica
August 16th, 2010, 09:53 AM
In gesture recognition, you would be better off dividing the screen into tiny tile space. When the user clicks and the drags, mark the current tile as visited and push it into an array. And when the user releases the mouse, match your shape algorithm with this array, It really becomes a lot simpler like this, than directly dealing with the shape after it has been drawn.

To write a shape algorithm, think of the properties of the shape. Then include an "error" factor in there.Sirisian has given an almost prefect algorithm, but you should try thinking about the rules yourself a bit....it will make implementing the algorithm easier.