Guest User

http://stackoverflow.com/questions/17641308

a guest
Jul 14th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.61 KB | None | 0 0
  1. // data point it TRectF
  2. // all y values are >= 0
  3. // http://stackoverflow.com/questions/17641308
  4.  
  5. // very generic framework - suboptimal in many many senses.
  6. // in other words - no "premature optimization" :-)
  7.  
  8. // and not testing - just drafting
  9.  
  10. type
  11.   PropagationConfig = record
  12.      x_delta, y_delta: single;
  13.      poly_coeff: array of single; // p[0]*x^0 + p[1]*x^2 + p[2]*x^4 + ...
  14.   end;
  15. // the programmer should fill those values to his tastes of what is beautiful or purposeful
  16.  
  17.   function EnrichData(const Input: any stream of TRectF): same as input stream of TRectF;
  18.   begin
  19.      for every pair of data points in Input
  20.      do emit into Result ( EnrichPair( datapoint 1, datapoint 2, propagation config ) );
  21.   end;
  22.  
  23.  function EnrichPair( dp1, dp2: TRectF; const config: propagationconfig): array of TRectF;
  24.  var tempDP: TRectF;
  25.  begin
  26.     if dp1.x = dp2.x then begin
  27.        SetLength(Result,1);
  28.        Result[0].x := dp1.x;
  29.        Result[0].y := Max(dp1.y, dp2.y);
  30.        Exit;
  31.     end;
  32.  
  33.     if dp1.x > dp2.x then begin
  34.        tempDP := dp1; dp1 := dp2; dp2 := tempDP;  
  35.     end;
  36.  
  37.     if dp2.x - dp1.x <= config.x_delta then begin
  38.        SetLength(Result,2);
  39.        Result[0] := dp1;
  40.        Result[1] := dp2;
  41.        Exit;
  42.     end;
  43.  
  44.     SetLength(Result, 1 + Ceil(dp2.x - dp1.x) );
  45.  
  46.     Result[Low(Result)] := dp1; Result[High(Result)] := dp2;
  47.    
  48.     for i := Low(Result) + 1 to High(Result) - 1 do begin
  49.         Result[i].x  := Result [i-1].x + config.x_delta;
  50.         Result[i].y := 0;
  51.     end;
  52.  
  53.     for i := Low(Result) + 1 to High(Result) - 1 do begin
  54.         Result[i].y := Estimate(dp1, Result[i].x, config);
  55.         if Result[i].y < config.y_delta then break; // too far from peak - no more traces
  56.     end;
  57.  
  58.     for i := High(Result) - 1 downto Low(Result) + 1 do begin
  59.         y := Estimate(dp2, Result[i].x, config);
  60.         if Result[i].y > y then break; // adjacent peak has more influence here
  61.         Result[i].y := y;
  62.         if Result[i].y < config.y_delta then break; // too far from peak - no more traces
  63.     end;
  64.  end;
  65.  
  66.  function Estimate(const peak: TRectF; const place: single; const config: PropagationConfig): single;
  67.  var delta: single; i: integer;
  68.  begin
  69.      delta := abs(place - peak.x);
  70.      if delta = 0 then Exit(peak.y);
  71.      
  72.      Result := 0;
  73.      for i := Low(config.poly_coeff) to High(config.poly_coeff) do
  74.          Result := Result + config.poly_coeff[i - Low(config.poly_coeff)] *
  75.                          Power(1 + delta , 2 * ( i - Low(config.poly_coeff) ) );
  76.      
  77.      Result := peak.y / Result; // fall down from peak to the sides
  78.  end;
Add Comment
Please, Sign In to add comment