Advertisement
WarPie90

[Simba 1.4] RandomPointTriangle

Jan 26th, 2025 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.04 KB | None | 0 0
  1. // original work at this pastebin - with description:
  2. // https://pastebin.com/7N2iuDsF
  3.  
  4. type
  5.   TTriangle = record A,B,C: TPoint; end;
  6.  
  7. function TTriangle.Contains(P: TPoint): Boolean; constref;
  8. var
  9.   c1, c2, c3: Int32;
  10. begin
  11.   c1 := Sign(srl.CrossProduct(A, B, P));
  12.   c2 := srl.CrossProduct(B, C, P);
  13.   c3 := srl.CrossProduct(C, A, P);
  14.   Result := (c1 * c2 >= 0) and (c1 * c3 >= 0) and (Sign(c2) * c3 >= 0);
  15. end;  
  16.  
  17. function TTriangle.NearestEdge(P: TPoint): TPoint; constref;
  18. var
  19.   Best, Dist: Single;
  20.   X: TPoint;
  21. begin
  22.   Best := srl.DistToLineEx(P, Self.A,  Self.B, Result);
  23.   if (dist := srl.DistToLineEx(P, Self.B, Self.C, X)) < Best then begin Result := X; Best := Dist; end;
  24.   if (dist := srl.DistToLineEx(P, Self.C, Self.A, X)) < Best then Result := X;
  25. end;
  26.  
  27.  
  28. function TTriangle.RandomWeightedPoint(Target: TPoint; weight: Single=10.0; bias: Single=1): TPoint; constref;
  29. var
  30.   r1, r2, r3, u, v, w, t, sum: Single;
  31.   stdev,xr,yr: Single;
  32. const
  33.   eps := 0.0000001;
  34. begin
  35.   if bias > 0 then
  36.   begin
  37.     xr := abs(Self.A.x - Target.x) + abs(Self.B.x - Target.x) + abs(Self.C.x - Target.x);
  38.     yr := abs(Self.A.y - Target.y) + abs(Self.B.y - Target.y) + abs(Self.C.y - Target.y);
  39.  
  40.     Target.X += Round(srl.GaussRand(0, xr/3 * bias) / weight);
  41.     Target.Y += Round(srl.GaussRand(0, yr/3 * bias) / weight);
  42.   end;
  43.  
  44.   if not Self.Contains(Target)  then
  45.     Target := Self.NearestEdge(Target);
  46.  
  47.   r1 := Random();
  48.   r2 := Random();
  49.   if r1 + r2 > 1 then
  50.   begin
  51.     r1 := 1 - r1;
  52.     r2 := 1 - r2;
  53.   end;
  54.  
  55.   r3 := Random();
  56.   sum := 1 + weight * r3 + eps;
  57.   u := r1 / sum;
  58.   v := r2 / sum;
  59.   w := (1 - r1 - r2) / sum;
  60.   t := (weight * r3) / sum;
  61.  
  62.   with Self do
  63.   begin
  64.     Result.X := Round(u * A.X + v * B.X + w * C.X + t * Target.X);
  65.     Result.Y := Round(u * A.Y + v * B.Y + w * C.Y + t * Target.Y);
  66.   end;
  67. end;
  68.  
  69.  
  70. function ArcToTriangle(start, stop: TPoint; radians: single): TTriangle;
  71. begin
  72.   Result.A := start;
  73.   Result.B := stop.Rotate(-radians / 2, start);
  74.   Result.C := stop.Rotate(+radians / 2, start);
  75. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement