WarPie90

Simple curved line

Nov 4th, 2017 (edited)
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.53 KB | None | 0 0
  1. program new;
  2. {$I SRL/OSR.simba}
  3. {$R-}
  4.  
  5. var
  6.   bmp: TMufasaBitmap;
  7.  
  8. function SmoothLine(A,B:Vector2; z: Int32; curvyness:Double=1): TPointArray;
  9. var
  10.   t,d,r,rx,ry: Double;
  11.   i,j,k: Int32;
  12.   vec,prev: Vector2;
  13.   v,tmp: array of Vector2;
  14. begin
  15.   d := Hypot(A.x-B.x, A.y-B.y) / z * curvyness;
  16.   r := d/2;
  17.   SetLength(v, z);
  18.   v[0] := A;
  19.   for i:=1 to z-1 do
  20.   begin
  21.     t := srl.SkewedRand(0,-0.5,1.5);
  22.     v[i].x := ((1-t)*v[i-1].x + t*B.x) + Random()*d-r;
  23.     v[i].y := ((1-t)*v[i-1].y + t*B.y) + Random()*d-r;
  24.   end;
  25.   v[z-1] := B;
  26.  
  27.   t := 0;
  28.   prev := A;
  29.   while t <= 1 do
  30.   begin
  31.     tmp := Copy(v);
  32.     for i:=High(tmp) downto 1 do
  33.       for k:=0 to i do
  34.       begin
  35.         tmp[k].x := tmp[k].x + (tmp[k+1].x - tmp[k].x) * t;
  36.         tmp[k].y := tmp[k].y + (tmp[k+1].y - tmp[k].y) * t;
  37.       end;
  38.     vec := tmp[0];
  39.  
  40.     Result += TPAFromLine(Round(prev.x),Round(prev.y), Round(vec.x),Round(vec.y));
  41.     prev := vec;
  42.     t += 0.01;
  43.   end;
  44.   Result += TPAFromLine(Round(prev.x),Round(prev.y), Round(B.x),Round(B.y));
  45. end;
  46.  
  47.  
  48. var
  49.  p: TPoint;
  50.  a: TPoint=[50,50];
  51.  b: TPoint=[430,380];
  52.  i,c: Int32;
  53. begin
  54.   bmp := GetMufasaBitmap(CreateBitmap(500,500));
  55.   bmp.Debug();
  56.  
  57.   while True do
  58.   begin
  59.     bmp.Clear();
  60.     bmp.DrawTPA(TPAFromLine(a.x,a.y,b.x,b.y), $FFFFFF);
  61.  
  62.     for p in SmoothLine([a.x,a.y], [b.x,b.y],  25, 1.3) do
  63.     try
  64.       bmp.SetPixel(p.x,p.y, $FF00FF);
  65.       Inc(i);
  66.       if i mod 3 = 0 then bmp.Debug();
  67.     except end;
  68.     bmp.Debug();
  69.     Wait(500);
  70.   end;
  71.   bmp.Debug();
  72. end.
Add Comment
Please, Sign In to add comment