4

I would like to make a shape recognition program that would trace a mouse and record it's location at each 1/2 second. How could I use these points to find a rough polygon? In other words, if you just draw a shape resembling a triangle or square, it will more likely be a be a 50-100-gon, how can I simplify it to get the shape I were trying to draw? I know that you could do a genetic algorithm, but do not know exactly how that would work, and I would like to know any alternatives.

edit: convex hulls will not work, concavity is needed to be preserved.

mavrosxristoforos
  • 3,573
  • 2
  • 25
  • 40
invisible bob
  • 864
  • 1
  • 9
  • 24

2 Answers2

3

i'll give this a shot.

  1. lets call the position when the mouse click down event happens point START
  2. every interval take another position called CURR
  3. the lets call the previous CURR, PREV
  4. calculate the slope (delta y/delta x) between CURR and PREV,
  5. calculate the slope of the line between CURR and START
  6. define some threshold for a difference between the two slopes
  7. if the slope crosses the threshold,
    1. store the line between START AND CURR as a SIDE
    2. define CURR as a new START
  8. repeat until CURR is within a certain radius of the original START or crosses one of the previous sides

you might be able to determine the shape simply by counting sides.

David Chan
  • 7,347
  • 1
  • 28
  • 49
  • I like this solution, but I can only accept one answer, and the other one is a little bit more intuitive, so i'll start with it first. – invisible bob Jun 07 '11 at 20:20
  • +1 IMHO, using slopes is much better than calculating triangular areas, as a wider solution for discarding collinear points, especially when dealing with pixel coordinates. – mavrosxristoforos Mar 12 '16 at 22:08
1

For each point along the 100-agon, find the area of the tiny triangle formed by that point and the points on either side. Remove the point that created the smallest triangle. Repeat until the smallest triangle is larger than some threshold.

Fantius
  • 3,806
  • 5
  • 32
  • 49
  • Or repeat until the smallest triangle is larger then some percentage of the main shape's area. – Fantius Jun 04 '11 at 15:12
  • which would be more effective, size or angle measure? – invisible bob Jun 05 '11 at 21:52
  • for example, for Pi in [P1, P2, P3 ... PN], remove point if m∠p(i-1)p(i)p(i+1) > threshold ? – invisible bob Jun 05 '11 at 22:01
  • I don't know. I guess you'd have to try them and see. My guess is that size would be more effective because when the mouse is moving slowly and the generated points are close together, big changes in angles could occur even though it's not a real angle in the intended shape. – Fantius Jun 05 '11 at 23:49
  • but when it's moving faster, the triangles get bigger than when it is moving slow... I almost have the angles running, i'll try both. Maybe a hybrid would work best? – invisible bob Jun 07 '11 at 20:18
  • When you say bigger, I must assume you mean longer. But they should also be very thin. So the area should still be small. – Fantius Jun 07 '11 at 21:57
  • You should be able to create good automated tests for your algorithm attempts. I would do this instead of by-hand subjective testing. For instance, you can create an array of points that represents a perfect triangle at various mouse velocities. You should be able to handle the ideal case well (or perfectly) in order to expect to handle tougher cases. – Fantius Jun 07 '11 at 22:04