Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Brainer

By: a guest on Oct 27th, 2007  |  syntax: Delphi  |  size: 1.19 KB  |  views: 122  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function TShape.Validate: Boolean;
  2.  
  3.   { .: LinesCross :. }
  4.   function LinesCross(x0, y0, x1, y1, x2, y2, x3, y3: Integer): Boolean;
  5.   var
  6.     D, AB, CD: Single;
  7.   begin
  8.         D := (x1 - x0) * (y3 - y2) - (y1 - y0) * (x3 - x2);
  9.           if (Abs(D) < 0.001) then
  10.     begin
  11.       Result := False;
  12.       exit;
  13.     end;
  14.           AB := ((y0 - y2) * (x3 - x2) -(x0 - x2) * (y3 - y2)) / D;
  15.           if (AB > 0.0) and (AB < 1.0) then
  16.     begin
  17.       CD := ((y0 - y2) * (x1 - x0) - (x0 - x2) * (y1 - y0)) / D;
  18.                   if (CD > 0.0) and (CD < 1.0) then
  19.       begin
  20.         Result := False;
  21.         exit;
  22.       end;
  23.     end;
  24.     Result := True;
  25.   end;
  26.  
  27. var
  28.   I, J: Integer;
  29. begin
  30.   Result := True;
  31.  
  32.   for I := 0 to FPoints.Count -4 do
  33.     for J := I + 1 to FPoints.Count -2 do
  34.     begin
  35.       if LinesCross(TShapeVec(FPoints.Items[I]).X,
  36.         TShapeVec(FPoints.Items[I]).Y,
  37.         TShapeVec(FPoints.Items[I + 1]).X,
  38.         TShapeVec(FPoints.Items[I + 1]).Y,
  39.         TShapeVec(FPoints.Items[J]).X,
  40.         TShapeVec(FPoints.Items[J]).Y,
  41.         TShapeVec(FPoints.Items[J + 1]).X,
  42.         TShapeVec(FPoints.Items[J + 1]).Y) then
  43.       begin
  44.         Result := False;
  45.         exit;
  46.       end;
  47.     end;
  48. end;