niedziela, 11 września 2022

Voronoi shapes (C++ Builder and Lazarus)




C++ Builder 

private: // User declarations

       double __fastcall write_Distance(int a, int b, int c, int d);
       int r,g,b;
       int date_X[10], date_Y[10];

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

 srand(time(NULL));

}

double __fastcall TForm1::write_Distance(int a, int b, int c, int d)

{

 double result;

 result=sqrt((a-c)*(a-c)+(b-d)*(b-d));

 return result;

}

//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)

{

 TColor base_Color[10];

 int i,j,k,l,w,h;

 double d1,d2;

 j=20000+rand()%20000;

 d1=(1.00*j);

 w=PaintBox1->Width;

 h=PaintBox1->Height;

 for(i=0;i<10;i++)

 {

  r=rand()%255;

  g=rand()%255;

  b=rand()%255;

  base_Color[i]=RGB(r,g,b);

  date_X[i]=rand()%w;

  date_Y[i]=rand()%h;

 }

 for(i=0;i<w;i++)

 {

  for(j=0;j<h;j++)

  {

   d2=d1;

   for(k=0;k<10;k++)

   {

    if(write_Distance(i,j,date_X[k],date_Y[k])<d2)

    {

     l=k;

     d2=write_Distance(i,j,date_X[k],date_Y[k]);

    }

   }

   PaintBox1->Canvas->Pixels[i][j]=base_Color[l];

  }

 }

}


Lazarus


function distance_P(a,b,c,d:Integer):Real;

var

dist: real;

begin

  dist:=Sqrt((a-c)*(a-c)+(b-d)*(b-d));

  distance_P:=dist;

end;   


procedure TForm1.FormCreate(Sender: TObject);

begin

    Randomize;

end;


procedure TForm1.Button1Click(Sender: TObject);

var

  i,j,k,l,x_Width,y_Height: Integer;

  base_Colors : array[0..10] of TColor;

  x_Date : array[0..10] of Integer;

  y_Date : array[0..10] of Integer;

  temp_Distance1, temp_Distance2 : Real;

begin

  i:=20000+random(20000);

  temp_Distance1:=(1.00*i);

  base_Colors[0]:=clLime;

  base_Colors[1]:=clYellow;

  base_Colors[2]:=clRed;

  base_Colors[3]:=clGreen;

  base_Colors[4]:=clBlue;

  base_Colors[5]:=clBlack;

  base_Colors[6]:=clWhite;

  base_Colors[7]:=clAqua;

  base_Colors[8]:=clGray;

  base_Colors[9]:=clPurple;

  base_Colors[10]:=clOlive;

  x_Width:=PaintBox1.Width;

  y_Height:=PaintBox1.Height;

  for i:=0 to 10 do

  begin

   x_Date[i]:=random(x_Width);

   y_Date[i]:=random(y_Height);

  end;

  for i:=0 to x_Width do

  begin

   for j:=0 to y_Height do

   begin

       temp_Distance2:=temp_Distance1;

       for k:=0 to 10 do

       begin

           if distance_P(i,j,x_Date[k],y_Date[k])<temp_Distance2 then

           begin

             l:=k;

             temp_Distance2:=distance_P(i,j,x_Date[k],y_Date[k]);


           end;

       end;

       PaintBox1.Canvas.Pixels[i,j]:=base_Colors[l];

   end;

  end;


end;


end.

              

 

Brak komentarzy:

Prześlij komentarz