情人节红攻瑰--Delphi版本

 

在oschina上看到了用c写的红玫瑰, 以前只见过用js写的, 就随手用delphi翻译了c的代码, 效果还不错哈....

 

原c作者jokeym贴子 http://www.oschina.net/code/snippet_2373787_48760

 

我的改版贴子 http://www.oschina.net/code/snippet_212659_48907

 

以下为代码:

 

[delphi] view plaincopy
 
  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,  
  7.   Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     btn1: TButton;  
  12.     procedure btn1Click(Sender: TObject);  
  13.   private  
  14.   
  15.   public  
  16.     { Public declarations }  
  17.   end;  
  18.   
  19. var  
  20.   Form1: TForm1;  
  21.   
  22. implementation  
  23.   
  24. {$R *.dfm}  
  25.   
  26. uses  
  27.   System.Math;  
  28.   
  29. // 原作者贴子,  
  30. // http://www.oschina.net/code/snippet_2373787_48760  
  31.   
  32. // delphi版本  
  33. // ying32  
  34.   
  35.   
  36. const  
  37.   RAND_MAX = $7FFF;  
  38.   urosesize: Integer = 500;  
  39.   uh: Integer = -250;  
  40.   
  41. type  
  42. // 定义结构体  
  43.   TDOT = record  
  44.     x: Double;  
  45.     y: Double;  
  46.     z: Double;  
  47.     r: Double;  // 红色  
  48.     g: double;  // 绿色  
  49.     // b(蓝色) 通过 r 计算  
  50.   end;  
  51.   
  52. function calc(a, b, c: Double; var d: TDOT): Boolean;  
  53. var  
  54.   j, n, o, w, z: Double;  
  55.   _A, _B: Double;  
  56. begin  
  57.   Result := False;  
  58.   if c > 60 then // 花柄  
  59.   begin  
  60.     d.x := sin(a * 7) * (13 + 5 / (0.2 + power(b * 4, 4))) - sin(b) * 50;  
  61.     d.y := b * urosesize + 50;  
  62.     d.z := 625 + cos(a * 7) * (13 + 5 / (0.2 + power(b * 4, 4))) + b * 400;  
  63.     d.r := a * 1 - b / 2;  
  64.     d.g := a;  
  65.     Exit(True);  
  66.   end;  
  67.   _A := a * 2 - 1;  
  68.   _B := b * 2 - 1;  
  69.   
  70.   if _A * _A + _B * _B < then  
  71.   begin  
  72.     if c > 37 then           // 叶  
  73.     begin  
  74.       j := Trunc(c) and 1;  
  75.       n := IfThen(j <> 0, 6, 4);  
  76.       o := 0.5 / (a + 0.01) + cos(b * 125) * 3 - a * 300;  
  77.       w := b * uh;  
  78.   
  79.       d.x := o * cos(n) + w * sin(n) + j * 610 - 390;  
  80.       d.y := o * sin(n) - w * cos(n) + 550 - j * 350;  
  81.       d.z := 1180 + cos(_B + _A) * 99 - j * 300;  
  82.       d.r := 0.4 - a * 0.1 + power(1 - _B * _B, -uh * 6) * 0.15 - a * b * 0.4 + cos(a + b) / 5 + power(cos((o * (a + 1) + IfThen(_B > 0, w, -w)) / 25), 30) * 0.1 * (1 - _B * _B);  
  83.       d.g := o / 1000 + 0.7 - o * w * 0.000003;  
  84.       Exit(True);  
  85.     end;  
  86.   
  87.     if c > 32 then           // 花萼  
  88.     begin  
  89.       c := c * 1.16 - 0.15;  
  90.       o := a * 45 - 20;  
  91.       w := b * b * uh;  
  92.       z := o * sin(c) + w * cos(c) + 620;  
  93.   
  94.       d.x := o * cos(c) - w * sin(c);  
  95.       d.y := 28 + cos(_B * 0.5) * 99 - b * b * b * 60 - z / 2 - uh;  
  96.       d.z := z;  
  97.       d.r := (b * b * 0.3 + power((1 - (_A * _A)), 7) * 0.15 + 0.3) * b;  
  98.       d.g := b * 0.7;  
  99.       Exit(True);  
  100.     end;  
  101.   
  102.         // 花  
  103.     o := _A * (2 - b) * (80 - c * 2);  
  104.     w := 99 - cos(_A) * 120 - cos(b) * (-uh - c * 4.9) + cos(power(1 - b, 7)) * 50 + c * 2;  
  105.     z := o * sin(c) + w * cos(c) + 700;  
  106.   
  107.     d.x := o * cos(c) - w * sin(c);  
  108.     d.y := _B * 99 - cos(power(b, 7)) * 50 - c / 3 - z / 1.35 + 450;  
  109.     d.z := z;  
  110.     d.r := (1 - b / 1.2) * 0.9 + a * 0.1;  
  111.     d.g := power((1 - b), 20) / 4 + 0.05;  
  112.     Exit(True);  
  113.   end;  
  114. end;  
  115.   
  116. procedure TForm1.btn1Click(Sender: TObject);  
  117. var  
  118.   zBuffer: array of Smallint;  
  119.   i, j: Integer;  
  120.   x, y, z, zBufferIndex: Integer;  
  121.   dot: TDOT;  
  122.   r, g, b: Integer;  
  123. begin  
  124.   SetLength(zBuffer, urosesize * urosesize);  
  125.   
  126.   Canvas.Brush.Color := clWhite;  
  127.   Canvas.FillRect(Rect(0, 0, Width, Height));  
  128.   
  129.   Randomize;  
  130.   for j := to 1999 do  
  131.   begin  
  132.     for i := to 9999 do  
  133.     begin  
  134.       if calc(Random(RAND_MAX) / RAND_MAX, Random(RAND_MAX) / RAND_MAX, (Random(RAND_MAX) mod 46) / 0.74, dot) then  
  135.       begin  
  136.         z := Trunc(dot.z + 0.5);  
  137.         x := Trunc(dot.x * urosesize / z - uh + 0.5);  
  138.         y := Trunc(dot.y * urosesize / z - uh + 0.5);  
  139.         if y >= urosesize then  
  140.           Continue;  
  141.   
  142.         zBufferIndex := y * urosesize + x;  
  143.   
  144.         if (not (zBuffer[zBufferIndex] <> 0)) or (zBuffer[zBufferIndex] > z) then  
  145.         begin  
  146.           zBuffer[zBufferIndex] := z;  
  147.   
  148.                     // 画点  
  149.           r := not Trunc(dot.r * uh);  
  150.           if r < then  
  151.             r := 0;  
  152.           if r > 255 then  
  153.             r := 255;  
  154.           g := not Trunc(dot.g * uh);  
  155.           if g < then  
  156.             g := 0;  
  157.           if g > 255 then  
  158.             g := 255;  
  159.           b := not Trunc(dot.r * dot.r *  - 80);  
  160.           if b < then  
  161.             b := 0;  
  162.           if b > 255 then  
  163.             b := 255;  
  164.           Canvas.Pixels[x + 50, y - 20] := RGB(r, g, b);  
  165.         end;  
  166.       end;  
  167.       Application.ProcessMessages;  
  168.     end;  
  169.     Sleep(1);  
  170.   end;  
  171. end;  
  172.   
  173. end.  

 

 

 

 

http://blog.csdn.net/zyjying520/article/details/46592831

 

posted @ 2015-03-26 19:15  findumars  Views(679)  Comments(0Edit  收藏  举报