Advertisement
WarPie90

Lape - KABOOOOOM *crash*

Sep 27th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.69 KB | None | 0 0
  1. program new;
  2. {$DEFINE E_HIDE_ALL}
  3. {$I SimbaExt_beta/SimbaExt.simba}
  4.  
  5. type
  6.   TMatchData = record
  7.     pattern: TPointArray;
  8.     similarity: Double;
  9.   end;
  10.   TMatchArr = Array of TMatchData;
  11.  
  12.  
  13. procedure SortMatchArr(var arr:TMatchArr; left, right:Int32);
  14. var
  15.   i, j:Integer;
  16.   tmp:TMatchData;
  17. begin
  18.   for i := left+1 to right do begin
  19.     j := i-1;
  20.     tmp := arr[i];
  21.     while (j >= left) and (arr[j].similarity > tmp.similarity) do begin
  22.       arr[j+1] := arr[j];
  23.       j:=j-1;
  24.     end;
  25.     arr[j+1] := Tmp;
  26.   end;
  27. end;
  28.  
  29. function SqDist(p,q:TPoint): Int32;
  30. begin
  31.   Result := Sqr(p.x-q.x) + Sqr(p.x-q.x);
  32. end;
  33.  
  34. function BlobDist(A,B:TPointArray): Double;
  35. var
  36.   tree:TSlackTree;
  37.   node:PSlackTree;
  38.   i:Int32;
  39.   ab,ba:Double;
  40. begin
  41.   tree.Init(A);
  42.   for i:=0 to High(B) do
  43.   begin
  44.     node := tree.Nearest_N(B[i],False);
  45.     ab += SqDist(TSlackTree(node^).split, B[i]);
  46.   end;
  47.   ab := Sqrt(ab / Length(B));
  48.  
  49.   tree.Init(B);
  50.   for i:=0 to High(A) do
  51.   begin
  52.     node := tree.Nearest_N(A[i],False);
  53.     ba += SqDist(TSlackTree(node^).split, A[i]);
  54.   end;
  55.   ba := Sqrt(ba / Length(A));
  56.   Result := MaxE(ab,ba);
  57. end;
  58.  
  59. function kNearest(shapes:T2DPointArray; query:TPointArray; k:Int32): TMatchArr;
  60. var
  61.   temp:TMatchArr;
  62.   curr,test:TPointArray;
  63.   B:TBox;
  64.   i:Int32;
  65.   m:TPoint;
  66. begin
  67.   query := copy(query);
  68.   m := query.Mean();
  69.   OffsetTPA(query, Point(-m.x,-m.y));
  70.  
  71.   SetLength(Result, Length(shapes));
  72.   for i:=0 to High(Shapes) do
  73.   begin
  74.     curr := Copy( Shapes[i] );
  75.     m := curr.Mean();
  76.  
  77.     curr.Offset( -m.x,-m.y );
  78.     Result[i].similarity := BlobDist(curr, query);
  79.     Result[i].pattern := Shapes[i];
  80.   end;
  81.   SortMatchArr(Result,0,high(Result));
  82.   SetLength(Result, k);
  83. end;
  84.  
  85. //call me to crash Lape.
  86. function CharSegmentation(TPA:TPointArray): T2DPointArray;
  87. begin
  88.   Result := ClusterTPA(TPA,5);
  89. end;
  90.  
  91. var
  92.   TPA,textTPA:TPointArray;
  93.   ATPA:T2DPointArray;
  94.   BMP: TRafBitmap;
  95.   Res: TMatchArr;
  96.   i,_:Int32;
  97.   offset:TPoint;
  98. begin
  99.   FindColorsTolerance(TPA,0, 30,20,750,300, 10);
  100.   ATPA := CharSegmentation(TPA);                      ///REPLACE WITH ClusterTPA(TPA,5) AND LAPE DON'T CRASH
  101.  
  102.   TPA := ATPA[Random(0,High(ATPA))];
  103.   Res := kNearest(ATPA, TPA, Length(ATPA));
  104.  
  105.  
  106.   BMP.Create(750,300, $FFFFFF);
  107.   for i:=0 to High(ATPA) do BMP.SetPixels(ATPA[i], 0);
  108.   BMP.SetPixels(TPA, 255);
  109.  
  110.  
  111.   for i:=0 to 6 do
  112.   begin
  113.     textTPA := TPAFromText(
  114.       Replace( ToString(Round(Res[i].similarity, 1)), ',','.',[] ),
  115.       'StatChars', _,_
  116.     );
  117.     offset.x := res[i].pattern[0].x-6;
  118.     offset.y := res[i].pattern[0].y-10;
  119.     OffsetTPA(textTPA, offset);
  120.     BMP.SetPixels(textTPA, $555555);
  121.   end;
  122.   BMP.Debug();
  123.   BMP.Free();
  124. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement