A Flash Developer Resource Site

# Thread: How to check if shape is circle?

1. ## How to check if shape is circle?

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

2. Well, it's never going to be a perfect circle, right? What you're talking about borders on handwriting analysis. So you need an algorithm to collate the points and detect whether it's circular, sort of. The first question I'd ask would be, what are the alternatives? It's relatively easy to tell a circle from a line or a squiggle, because a circle's end point is the same as (or in this case, close to) its start point, and a circle encompasses an area. If that were the only point of divergence, I'd recommend checking start and end points and the area covered. Telling a hand-drawn circle from a hand-drawn square gets a lot more complicated. You have to think in terms of what gestures people make (left and right handed people make different gestures, too) when they make those shapes. On paper. Then extrapolate to what they might do with a mouse or a trackball. In that case, you might want to look at the most extreme angles between various points; if there are <5 and they add up to >345º, you're probably dealing with a square; if no angle is >30º and they add up to >345º and the end-point is within N pixels of the start-point, you're probably looking at a circle. And so on. Think backwards from what you do when you draw a circle and write on paper what the rules are for this as opposed to that.

In terms of actual code, I think it's pretty simple; you're polling mouse positions on each move and stacking them into a vector or array of 2D points, then running some kind of static analysis on the continuing relationships between those points. That's where you have to get creative. The best I can tell you based on your post is that several decades of research into algorithms for this type of thing have shown that there's a negative correlation between the number of possibilities graphed and the performance of the algorithm. So the first thing you should deal with in any case is defining and limiting the possible outcomes; then look at how best to sort an array of 2D points into one of those. The solution depends on the problem.

3. Thank you very much for your help josh, appreciate time and effort you put into this reply. I'll do some more thinking on this and try to create algorithm that will give me decent result and let you know once i am done.

cheers

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap