Given there are probably going to be rotation and x-y registration issues it seems to me that reducing the problem from 2 dimensions to a single dimension vector may work very swell in practice. It is easier to explain in code but the guts of the idea is simple. No matter how rotated or displaced, any planar figure (and the points define such a figure) will have a constant geometric centroid. Each point will have a constant distance from this centriod in one dimensional space regardless of rotation or displacement. We can use this to generate a very useful vector. We can also make the matching as precise of fuzzy as desired.

use constant X => 0; use constant Y => 1; my @points1 = ( [1,1], [2,2], [3,3] ); # a line like / my @points2 = ( [2,1], [3,2], [4,3] ); # a line like / displaced + R my @points3 = ( [1,1], [2.41,1], [3.82,1]);# a line like - my @template = ( [1,3], [2,2], [3,1] ); # a line like \ my $vp1 = vector(\@points1,2); my $vp2 = vector(\@points2,2); my $vp3 = vector(\@points3,2); my $vt = vector(\@template,2); print "@$vp1\n@$vp2\n@$vp3\ntemplate\n@$vt\n"; sub vector { my ($points, $precision) = @_; $precision ||= 10; # calculate the geometric centroid my ( $cx, $cy ) = (0,0); for (@$points) { $cx += $_->[X]; $cy += $_->[Y]; } $cx /= @$points; $cy /= @$points; # map the 1 dimensional distance of all points from centroid # using simple pythagorean geometry my @vector = map{ sqrt(($cx-$_->[X])**2 + ($cy-$_->[Y])**2) } @$po +ints; # fuzzify to desired precision and sort to create finished vector my $template = "%.${precision}f"; @vector = map{ sprintf $template, $_ } sort { $a <=> $b } @vector; return \@vector; } __DATA__ 0.00 1.41 1.41 0.00 1.41 1.41 0.00 1.41 1.41 template 0.00 1.41 1.41

So in one dimensional space our vector tells us that the 3 lines all match the template. The effects of rotation and displacement have been removed. You can also deal with scaling simply by using normalisation.

Matching the single dimension vector against a template set can be achieved in many ways. Stringifying the vector at a reliable precision and using a hash lookup against the template vectors is an obvious method.

This vector process has some caveats. For any given 1 dimensional vector there are an infinite number of 2 dimensional shapes that map to it. For example a vector of 1,1,1,1 in 1 dimensional space can represent any number of rectangles or a square in 2 dimensional space.

C = centroid . . . . : C : C C . . . .

cheers

tachyon


In reply to Re: Interesting problem fitting points to a template (algorithm?) by tachyon
in thread Interesting problem fitting points to a template (algorithm?) by doowah2004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.