Janilabo

Janilabo | New TPA distance functions to MSSL

Nov 4th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 15.39 KB | None | 0 0
  1. function MSSL_TPADistanceToPt(TPA: TPointArray; p: TPoint; method: (dtpClosest, dtpFarthest)): Integer;
  2. {==============================================================================]  
  3.   Created: November 5th, 2012.
  4.   Contributors: Janilabo
  5.   Explanation: Returns the distance from TPA to point (p).
  6.                Contains 2 methods: dtpClosest (closest distance) & dtpFarthest (farthest distance)
  7.                Result is based on SCAR's Distance() function.                  
  8. [==============================================================================}
  9. var
  10.   h, i, d: Integer;
  11. begin
  12.   Result := -1;
  13.   h := High(TPA);  
  14.   if (h < 0) then
  15.     Exit;      
  16.   Result := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  17.   if (h > 0) then
  18.   case method of
  19.     dtpClosest:
  20.     for i := 1 to h do
  21.     begin      
  22.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  23.       if (d < Result) then
  24.         Result := d;
  25.     end;
  26.     dtpFarthest:
  27.     for i := 1 to h do
  28.     begin      
  29.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  30.       if (d > Result) then
  31.         Result := d;
  32.     end;    
  33.   end;
  34. end;
  35.  
  36. function MSSL_TPADistanceToPtEx(TPA: TPointArray; p: TPoint; method: (dtpClosest, dtpFarthest); var m_pt: TPoint): Integer;
  37. {==============================================================================]  
  38.   Created: November 5th, 2012.
  39.   Contributors: Janilabo
  40.   Explanation: Returns the distance from TPA to point (p).
  41.                Contains 2 methods: dtpClosest (closest distance) & dtpFarthest (farthest distance)
  42.                TPA's matched (closest/farthest) point is stored to m_pt.
  43.                Result is based on SCAR's Distance() function.                  
  44. [==============================================================================}
  45. var
  46.   h, i, d: Integer;
  47. begin
  48.   Result := -1;
  49.   h := High(TPA);  
  50.   if (h < 0) then
  51.     Exit;      
  52.   Result := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  53.   if (h > 0) then
  54.   case method of
  55.     dtpClosest:
  56.     for i := 1 to h do
  57.     begin      
  58.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  59.       if (d < Result) then
  60.       begin
  61.         Result := d;
  62.         m_pt := TPA[i];
  63.       end;
  64.     end;
  65.     dtpFarthest:
  66.     for i := 1 to h do
  67.     begin      
  68.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  69.       if (d > Result) then
  70.       begin
  71.         Result := d;
  72.         m_pt := TPA[i];
  73.       end;
  74.     end;    
  75.   end;
  76. end;
  77.  
  78. function MSSL_TPADistanceToPtEx2(TPA: TPointArray; p: TPoint; method: (dtpClosest, dtpFarthest); var m_pts: TPointArray): Integer;
  79. {==============================================================================]  
  80.   Created: November 5th, 2012.
  81.   Contributors: Janilabo
  82.   Explanation: Returns the distance from TPA to point (p).
  83.                Contains 2 methods: dtpClosest (closest distance) & dtpFarthest (farthest distance)
  84.                TPA's matched (closest/farthest) points are stored to m_pts.
  85.                Result is based on SCAR's Distance() function.                  
  86. [==============================================================================}
  87. var
  88.   h, i, d, r: Integer;
  89. begin
  90.   Result := -1;
  91.   h := High(TPA);  
  92.   if (h < 0) then
  93.     Exit;      
  94.   Result := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  95.   SetLength(m_pts, (h + 1));
  96.   m_pts[i] := TPA[i];
  97.   if (h > 0) then
  98.   begin
  99.     case method of
  100.       dtpClosest:
  101.       for i := 1 to h do
  102.       begin      
  103.         d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  104.         if (d <= Result) then
  105.         begin
  106.           if (d < Result) then
  107.           begin
  108.             Result := d;
  109.             r := 0;
  110.           end;
  111.           m_pts[r] := TPA[i];
  112.           Inc(r);
  113.         end;
  114.       end;
  115.       dtpFarthest:
  116.       for i := 1 to h do
  117.       begin      
  118.         d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  119.         if (d >= Result) then
  120.         begin  
  121.           if (d > Result) then
  122.           begin
  123.             Result := d;
  124.             r := 0;
  125.           end;
  126.           m_pts[r] := TPA[i];
  127.           Inc(r);
  128.         end;
  129.       end;    
  130.     end;
  131.     SetLength(m_pts, r);  
  132.   end;
  133. end;
  134.  
  135. function MSSL_TPADistanceToTPA(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest)): Integer;
  136. {==============================================================================]  
  137.   Created: November 5th, 2012.
  138.   Contributors: Janilabo
  139.   Explanation: Returns the distance from TPA1 to TPA2.
  140.                Contains 2 methods: dtpClosest (closest distance) & dtpFarthest (farthest distance)
  141.                Matched (closest/farthest) points are stored to m_pt.
  142.                Result is based on SCAR's Distance() function.                  
  143. [==============================================================================}
  144. var
  145.   h1, h2, i1, i2, d: Integer;
  146. begin
  147.   Result := -1;
  148.   h1 := High(TPA1);
  149.   h2 := High(TPA2);  
  150.   if ((h1 < 0) or (h2 < 0)) then
  151.     Exit;      
  152.   Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
  153.   case method of
  154.     dtpClosest:
  155.     for i1 := 0 to h1 do
  156.       for i2 := 0 to h2 do  
  157.       begin      
  158.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  159.         if (d < Result) then
  160.           Result := d;
  161.       end;
  162.     dtpFarthest:
  163.     for i1 := 0 to h1 do
  164.       for i2 := 0 to h2 do
  165.       begin      
  166.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  167.         if (d > Result) then
  168.           Result := d;
  169.       end;    
  170.   end;
  171. end;
  172.  
  173. function MSSL_TPADistanceToTPAEx(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest); var m_pt1, m_pt2: TPoint): Integer;
  174. {==============================================================================]  
  175.   Created: November 5th, 2012.
  176.   Contributors: Janilabo
  177.   Explanation: Returns the distance from TPA1 to TPA2.
  178.                Contains 2 methods: dtpClosest (closest distance) & dtpFarthest (farthest distance)
  179.                Matched (closest/farthest) points are stored to m_pt1 & m_pt2.
  180.                Result is based on SCAR's Distance() function.                  
  181. [==============================================================================}
  182. var
  183.   h1, i1, h2, i2, d: Integer;
  184. begin
  185.   Result := -1;
  186.   h1 := High(TPA1);
  187.   h2 := High(TPA2);      
  188.   if ((h1 < 0) or (h2 < 0)) then
  189.     Exit;      
  190.   Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
  191.   m_pt1 := TPA1[0];
  192.   m_pt2 := TPA2[0];
  193.   case method of
  194.     dtpClosest:
  195.     for i1 := 0 to h1 do
  196.       for i2 := 0 to h2 do
  197.       begin      
  198.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  199.         if (d < Result) then
  200.         begin
  201.           Result := d;
  202.           m_pt1 := TPA1[i1];
  203.           m_pt2 := TPA2[i2];
  204.         end;
  205.       end;
  206.     dtpFarthest:
  207.     for i1 := 0 to h1 do
  208.       for i2 := 0 to h2 do
  209.       begin      
  210.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  211.         if (d > Result) then
  212.         begin
  213.           Result := d;
  214.           m_pt1 := TPA1[i1];
  215.           m_pt2 := TPA2[i2];
  216.         end;
  217.       end;  
  218.   end;
  219. end;
  220.  
  221. function MSSL_TPADistanceToTPAEx2(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest); var m_pts1, m_pts2: TPointArray): Integer;
  222. {==============================================================================]  
  223.   Created: November 5th, 2012.
  224.   Contributors: Janilabo
  225.   Explanation: Returns the distance from TPA1 to TPA2.
  226.                Contains 2 methods: dtpClosest (closest distance) & dtpFarthest (farthest distance)
  227.                ALL matched (closest/farthest) points are stored to m_pts1 & m_pts2.
  228.                Result is based on SCAR's Distance() function.                  
  229. [==============================================================================}
  230. var
  231.   h1, i1, h2, i2, d, r, m: Integer;
  232. begin
  233.   Result := -1;
  234.   h1 := High(TPA1);
  235.   h2 := High(TPA2);  
  236.   if ((h1 < 0) or (h2 < 0)) then
  237.     Exit;      
  238.   Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
  239.   m := Max(h1, h2);
  240.   SetLength(m_pts1, (m + 1));
  241.   SetLength(m_pts2, (m + 1));
  242.   m_pts1[0] := TPA1[0];
  243.   m_pts2[0] := TPA2[0];
  244.   case method of
  245.     dtpClosest:
  246.     for i1 := 0 to h1 do
  247.       for i2 := 0 to h2 do
  248.       begin      
  249.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  250.         if (d <= Result) then
  251.         begin
  252.           if (d < Result) then
  253.           begin
  254.             Result := d;
  255.             r := 0;
  256.           end;
  257.           m_pts1[r] := TPA1[i1];
  258.           m_pts2[r] := TPA2[i2];
  259.           Inc(r);
  260.         end;
  261.       end;
  262.     dtpFarthest:
  263.     for i1 := 0 to h1 do
  264.       for i2 := 0 to h2 do
  265.       begin      
  266.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  267.         if (d >= Result) then
  268.         begin
  269.           if (d > Result) then
  270.           begin
  271.             Result := d;
  272.             r := 0;
  273.           end;
  274.           m_pts1[r] := TPA1[i1];
  275.           m_pts2[r] := TPA2[i2];
  276.           Inc(r);
  277.         end;
  278.       end;  
  279.   end;
  280.   SetLength(m_pts1, r);
  281.   SetLength(m_pts2, r);  
  282. end;
  283.  
  284. function MSSL_TPAInDistanceWithPt(TPA: TPointArray; p: TPoint; dist: Integer): Boolean;
  285. {==============================================================================]  
  286.   Created: November 5th, 2012.
  287.   Contributors: Janilabo
  288.   Explanation: Returns true if point (p) is in distance (dist) with TPA.                
  289. [==============================================================================}
  290. var
  291.   h, i: Integer;
  292. begin
  293.   h := High(TPA);  
  294.   if ((h > -1) and (dist >= 0)) then
  295.     for i := 0 to h do
  296.     begin      
  297.       Result := (Distance(TPA[i].X, TPA[i].Y, p.X, p.Y) <= dist);
  298.       if Result then
  299.         Break;
  300.     end;
  301. end;
  302.  
  303. function MSSL_TPAInDistanceWithPtEx(TPA: TPointArray; p: TPoint; dist: Integer; var m_pt: TPoint): Boolean;
  304. {==============================================================================]  
  305.   Created: November 5th, 2012.
  306.   Contributors: Janilabo
  307.   Explanation: Returns true if point (p) is in distance (dist) with TPA.
  308.                Stores the matched point to m_pt.                
  309. [==============================================================================}
  310. var
  311.   h, i: Integer;
  312. begin
  313.   h := High(TPA);  
  314.   if ((h > -1) and (dist >= 0)) then
  315.     for i := 0 to h do
  316.     begin      
  317.       Result := (Distance(TPA[i].X, TPA[i].Y, p.X, p.Y) <= dist);
  318.       if Result then
  319.       begin  
  320.         m_pt := TPA[i];
  321.         Break;
  322.       end;
  323.     end;
  324. end;
  325.  
  326. function MSSL_TPAInDistanceWithPtEx2(TPA: TPointArray; p: TPoint; dist: Integer; var m_pts: TPointArray): Boolean;
  327. {==============================================================================]  
  328.   Created: November 5th, 2012.
  329.   Contributors: Janilabo
  330.   Explanation: Returns true if point (p) is in distance (dist) with TPA.
  331.                Stores ALL the matched points to m_pts.                
  332. [==============================================================================}
  333. var
  334.   h, i, r: Integer;
  335. begin
  336.   h := High(TPA);  
  337.   if ((h > -1) and (dist >= 0)) then
  338.   begin  
  339.     SetLength(m_pts, (h + 1));
  340.     for i := 0 to h do
  341.     begin      
  342.       if (Distance(TPA[i].X, TPA[i].Y, p.X, p.Y) <= dist) then
  343.       begin  
  344.         m_pts[r] := TPA[i];
  345.         Inc(r);
  346.       end;
  347.     end;                
  348.     Result := (r > 0);
  349.     SetLength(m_pts, r);      
  350.   end;
  351. end;
  352.  
  353. function MSSL_TPAInDistanceWithPt2(TPA: TPointArray; p: TPoint; minDist, maxDist: Integer): Boolean;
  354. {==============================================================================]  
  355.   Created: November 5th, 2012.
  356.   Contributors: Janilabo
  357.   Explanation: Returns true if point (p) is in distance (minDist / maxDist) with TPA.                
  358. [==============================================================================}
  359. var
  360.   d, h, i: Integer;
  361. begin
  362.   h := High(TPA);  
  363.   if ((h > -1) and (minDist >= 0) and (minDist <= maxDist)) then
  364.     for i := 0 to h do
  365.     begin
  366.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);      
  367.       Result := ((d >= minDist) and (d <= maxDist));
  368.       if Result then
  369.         Break;
  370.     end;
  371. end;
  372.  
  373. function MSSL_TPAInDistanceWithPt2Ex(TPA: TPointArray; p: TPoint; minDist, maxDist: Integer; var m_pt: TPoint): Boolean;
  374. {==============================================================================]  
  375.   Created: November 5th, 2012.
  376.   Contributors: Janilabo
  377.   Explanation: Returns true if point (p) is in distance (minDist / maxDist) with TPA.
  378.                Stores the matched point to m_pt.                
  379. [==============================================================================}
  380. var
  381.   d, h, i: Integer;
  382. begin
  383.   h := High(TPA);  
  384.   if ((h > -1) and (minDist >= 0) and (minDist <= maxDist)) then
  385.     for i := 0 to h do
  386.     begin      
  387.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  388.       Result := ((d >= minDist) and (d <= maxDist));
  389.       if Result then
  390.       begin  
  391.         m_pt := TPA[i];
  392.         Break;
  393.       end;
  394.     end;
  395. end;
  396.  
  397. function MSSL_TPAInDistanceWithPt2Ex2(TPA: TPointArray; p: TPoint; minDist, maxDist: Integer; var m_pts: TPointArray): Boolean;
  398. {==============================================================================]  
  399.   Created: November 5th, 2012.
  400.   Contributors: Janilabo
  401.   Explanation: Returns true if point (p) is in distance (minDist / maxDist) with TPA.
  402.                Stores ALL the matched points to m_pts.                
  403. [==============================================================================}
  404. var
  405.   d, h, i, r: Integer;
  406. begin
  407.   h := High(TPA);  
  408.   if ((h > -1) and (minDist >= 0) and (minDist <= maxDist)) then
  409.   begin  
  410.     SetLength(m_pts, (h + 1));
  411.     for i := 0 to h do
  412.     begin      
  413.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  414.       if ((d >= minDist) and (d <= maxDist)) then
  415.       begin  
  416.         m_pts[r] := TPA[i];
  417.         Inc(r);
  418.       end;
  419.     end;                
  420.     Result := (r > 0);
  421.     SetLength(m_pts, r);      
  422.   end;
  423. end;
  424.  
  425. function MSSL_TPAInDistanceWithTPA(TPA1, TPA2: TPointArray; dist: Integer): Boolean;
  426. {==============================================================================]  
  427.   Created: November 5th, 2012.
  428.   Contributors: Janilabo
  429.   Explanation: Returns true if TPA1 is in distance (dist) with TPA2.                
  430. [==============================================================================}
  431. var
  432.   h1, i1, h2, i2: Integer;
  433. begin
  434.   h1 := High(TPA1);  
  435.   h2 := High(TPA2);
  436.   if ((h1 > -1) and (h2 > -1) and (dist >= 0)) then
  437.     for i1 := 0 to h1 do
  438.       for i2 := 0 to h2 do
  439.       begin      
  440.         Result := (dist >= Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y));
  441.         if Result then
  442.           Break;
  443.       end;
  444. end;
  445.  
  446. function MSSL_TPAInDistanceWithTPA2(TPA1, TPA2: TPointArray; minDist, maxDist: Integer): Boolean;
  447. {==============================================================================]  
  448.   Created: November 5th, 2012.
  449.   Contributors: Janilabo
  450.   Explanation: Returns true if TPA1 is in distance (minDist / maxDist) with TPA2.                
  451. [==============================================================================}
  452. var
  453.   d, h1, i1, h2, i2: Integer;
  454. begin
  455.   h1 := High(TPA1);  
  456.   h2 := High(TPA2);
  457.   if ((h1 > -1) and (h2 > -1) and (minDist >= 0) and (minDist <= maxDist)) then
  458.     for i1 := 0 to h1 do
  459.       for i2 := 0 to h2 do
  460.       begin    
  461.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);  
  462.         Result := ((d >= minDist) and (d <= maxDist));
  463.         if Result then
  464.           Break;
  465.       end;
  466. end;
Advertisement
Add Comment
Please, Sign In to add comment