piątek, 2 grudnia 2016

Transformic graphics C++ Builder to Visual C++


I translated from polish book - "C++ Builder 20 efektownych programow" - Andrzej Stasiewicz code C++ Builder to Visual C++. 

Class shared for C++ Builder and Visual C++ (scaling the image) 
scale.h
class TSkalowanie
{
private:
double A,B,C,D;
double E,F,G,H;
public:
TSkalowanie(int xe0, int ye0,int eszer,int ewys,double xr0,double yr0,
double rszer, double rwys);
int daj_ekr_x(double xr);
int daj_ekr_y(double yr);
double daj_real_x(int xe);
    double daj_real_y(int ye);
};

scale.cpp

#include <math.h>
#include "StdAfx.h"
#include "scale.h"

TSkalowanie::TSkalowanie(int xe0, int ye0, int eszer, int ewys, double xr0,
double yr0, double rszer, double rwys)
{
A=(double) eszer/rszer;
B=(double) xe0-A*(xr0-rszer/2.);
C=-(double) ewys/rwys;
D=(double)ye0-C*(yr0+rwys/2.);
E=rszer/(double)eszer;
F=xr0-rszer/2.-E*(double)xe0;
G=-rwys/(double)ewys;
H=yr0+rwys/2.-G*(double)ye0;
}
int TSkalowanie::daj_ekr_x(double x)
{
return (int)(A*x+B);
}
int TSkalowanie::daj_ekr_y(double y)
{
return (int)(C*y+D);

}
double TSkalowanie::daj_real_x(int xe)
{
return E*xe+F;
}
double TSkalowanie::daj_real_y(int ye)
{
    return G*ye+H;
}


C++ Builder

__fastcall TForm6::TForm6(TComponent* Owner)
: TForm(Owner)
{
 StringGrid1->Cells[0][0]="ÏFS";
 StringGrid1->Cells[1][0]="#1";
 StringGrid1->Cells[2][0]="#2";
 StringGrid1->Cells[3][0]="#3";
 StringGrid1->Cells[4][0]="#4";

 StringGrid1->Cells[0][1]="A";
 StringGrid1->Cells[1][1]="0,5";
 StringGrid1->Cells[2][1]="0,5";
 StringGrid1->Cells[3][1]="0,5";
 StringGrid1->Cells[4][1]="0,5";

 StringGrid1->Cells[0][2]="B";
 StringGrid1->Cells[1][2]="0";
 StringGrid1->Cells[2][2]="0";
 StringGrid1->Cells[3][2]="0";
 StringGrid1->Cells[4][2]="0";

 StringGrid1->Cells[0][3]="C";
 StringGrid1->Cells[1][3]="0";
 StringGrid1->Cells[2][3]="0";
 StringGrid1->Cells[3][3]="0";
 StringGrid1->Cells[4][3]="0";

 StringGrid1->Cells[0][4]="D";
 StringGrid1->Cells[1][4]="0,5";
 StringGrid1->Cells[2][4]="0,5";
 StringGrid1->Cells[3][4]="0,5";
 StringGrid1->Cells[4][4]="0";

 StringGrid1->Cells[0][5]="E";
 StringGrid1->Cells[1][5]="0";
 StringGrid1->Cells[2][5]="2";
 StringGrid1->Cells[3][5]="1";
 StringGrid1->Cells[4][5]="0";

 StringGrid1->Cells[0][6]="F";
 StringGrid1->Cells[1][6]="0";
 StringGrid1->Cells[2][6]="0";
 StringGrid1->Cells[3][6]="1,73";
 StringGrid1->Cells[4][6]="0";

 StringGrid1->Cells[0][7]="P";
 StringGrid1->Cells[1][7]="33";
 StringGrid1->Cells[2][7]="33";
 StringGrid1->Cells[3][7]="33";
 StringGrid1->Cells[4][7]="0";





}
//---------------------------------------------------------------------------
void __fastcall TForm6::Button2Click(TObject *Sender)
{
 Close();
}
int __fastcall TForm6::random_niesprawiedliwe(int *p, int N)
{
int i,a,suma=0,suma_wag=0;
for(i=0;i<N;i++)
suma_wag+=p[i];
a=random(suma_wag);
for(i=0;i<N;i++)
{
suma+=p[i];
if(a<suma)
break;
}
return i;
}
//---------------------------------------------------------------------------
void __fastcall TForm6::PaintBox1Paint(TObject *Sender)
{
 const int max_iter=50000, max_il_przekszt=4;
 double A[4],B[4],C[4],D[4],E[4],F[4];
 int P[4];
 TSkalowanie skal(0,0,PaintBox1->ClientWidth,PaintBox1->ClientHeight,2,2,5,5);
 int i,nr_przekszt,xe,ye;
 double x,y,x1;
 TColor kolor[]={clRed,clLime,clAqua,clFuchsia};

 for(i=0;i<max_il_przekszt;i++)
 {
A[i]=StringGrid1->Cells[i+1][1].ToDouble();
B[i]=StringGrid1->Cells[i+1][2].ToDouble();
C[i]=StringGrid1->Cells[i+1][3].ToDouble();
D[i]=StringGrid1->Cells[i+1][4].ToDouble();
E[i]=StringGrid1->Cells[i+1][5].ToDouble();
F[i]=StringGrid1->Cells[i+1][6].ToDouble();
P[i]=StringGrid1->Cells[i=1][7].ToInt();
 }
 x=0,y=0;
 for(i=0;i<max_iter;i++)
 {
nr_przekszt=random_niesprawiedliwe(P,max_il_przekszt);
x1=A[nr_przekszt]*x+B[nr_przekszt]*y+E[nr_przekszt];
y=C[nr_przekszt]*x+D[nr_przekszt]*y+F[nr_przekszt];
x=x1;

xe=skal.daj_ekr_x(x);
ye=skal.daj_ekr_y(y);

PaintBox1->Canvas->Pixels[xe][ye]=kolor[nr_przekszt];
 }

}
int __fastcall TForm6::pole_Prostokata(int a, int b)
{
int result;
result=a*b;
return result;
}
//---------------------------------------------------------------------------
void __fastcall TForm6::Button1Click(TObject *Sender)
{
 PaintBox1->Refresh();
}

Visual C++

#pragma endregion
private: System::Drawing::Color give_Color(int x)
{
if(x==0)
return System::Drawing::Color::Fuchsia;
else if(x==1)
return System::Drawing::Color::Red;
else if(x==2)
return System::Drawing::Color::Lime;
else if(x==3)
return System::Drawing::Color::Aqua;
else
return System::Drawing::Color::Blue;
}
private: System::Void init_Random()
{
srand(time(NULL));
}
private: System::Int16 random_unfair(int *p, int N)
{
int i,a,suma=0,suma_wag=0;
            for(i=0;i<N;i++)
            suma_wag+=p[i];
            a=rand()%suma_wag;
            for(i=0;i<N;i++)
            {
        suma+=p[i];
        if(a<suma)
        break;
           }
           return i;
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
dataGridView1->RowCount=8;
dataGridView1->ColumnCount=5;
int i;
for(i=0;i<dataGridView1->ColumnCount;i++)
dataGridView1->Columns[i]->Width=70;
for(i=0;i<dataGridView1->RowCount;i++)
dataGridView1->Rows[i]->Height=35;
dataGridView1->Rows[0]->Cells[0]->Value="ÏFS";
dataGridView1->Rows[0]->Cells[1]->Value="#1";
dataGridView1->Rows[0]->Cells[2]->Value="#2";
dataGridView1->Rows[0]->Cells[3]->Value="#3";
dataGridView1->Rows[0]->Cells[4]->Value="#4";


dataGridView1->Rows[1]->Cells[0]->Value="A";
dataGridView1->Rows[1]->Cells[1]->Value="0,5";
dataGridView1->Rows[1]->Cells[2]->Value="0,5";
dataGridView1->Rows[1]->Cells[3]->Value="0,5";
dataGridView1->Rows[1]->Cells[4]->Value="0";

dataGridView1->Rows[2]->Cells[0]->Value="B";
dataGridView1->Rows[2]->Cells[1]->Value="0";
dataGridView1->Rows[2]->Cells[2]->Value="0";
dataGridView1->Rows[2]->Cells[3]->Value="0";
dataGridView1->Rows[2]->Cells[4]->Value="0";

dataGridView1->Rows[3]->Cells[0]->Value="C";
dataGridView1->Rows[3]->Cells[1]->Value="0";
dataGridView1->Rows[3]->Cells[2]->Value="0";
dataGridView1->Rows[3]->Cells[3]->Value="0";
dataGridView1->Rows[3]->Cells[4]->Value="0";

dataGridView1->Rows[4]->Cells[0]->Value="D";
dataGridView1->Rows[4]->Cells[1]->Value="0,5";
dataGridView1->Rows[4]->Cells[2]->Value="0,5";
dataGridView1->Rows[4]->Cells[3]->Value="0,5";
dataGridView1->Rows[4]->Cells[4]->Value="0";

dataGridView1->Rows[5]->Cells[0]->Value="E";
dataGridView1->Rows[5]->Cells[1]->Value="0";
dataGridView1->Rows[5]->Cells[2]->Value="2";
dataGridView1->Rows[5]->Cells[3]->Value="1";
dataGridView1->Rows[5]->Cells[4]->Value="0";

dataGridView1->Rows[6]->Cells[0]->Value="F";
dataGridView1->Rows[6]->Cells[1]->Value="0";
dataGridView1->Rows[6]->Cells[2]->Value="0";
dataGridView1->Rows[6]->Cells[3]->Value="1,73";
dataGridView1->Rows[6]->Cells[4]->Value="0";

dataGridView1->Rows[7]->Cells[0]->Value="P";
dataGridView1->Rows[7]->Cells[1]->Value="33";
dataGridView1->Rows[7]->Cells[2]->Value="33";
dataGridView1->Rows[7]->Cells[3]->Value="33";
dataGridView1->Rows[7]->Cells[4]->Value="0";

 
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Graphics^ graph=panel1->CreateGraphics();
Bitmap^ bitmap=gcnew Bitmap(panel1->Width,panel1->Height);
 
const int max_iter=50000,max_il_przekszt=4;
double A[4],B[4],C[4],D[4],E[4],F[4];
int P[4];
TSkalowanie skal(0,0,panel1->Width,panel1->Height,2,2,5,5);
int i,nr_przekszt,xe,ye;
double x,y,x1;
for(i=0;i<max_il_przekszt;i++)
{
;
A[i]=Convert::ToDouble(dataGridView1->Rows[1]->Cells[i+1]->Value);
B[i]=Convert::ToDouble(dataGridView1->Rows[2]->Cells[i+1]->Value);
C[i]=Convert::ToDouble(dataGridView1->Rows[3]->Cells[i+1]->Value);
D[i]=Convert::ToDouble(dataGridView1->Rows[4]->Cells[i+1]->Value);
E[i]=Convert::ToDouble(dataGridView1->Rows[5]->Cells[i+1]->Value);
F[i]=Convert::ToDouble(dataGridView1->Rows[6]->Cells[i+1]->Value);
P[i]=Convert::ToInt16(dataGridView1->Rows[7]->Cells[i+1]->Value);

}
x=0,y=0;
for(i=0;i<max_iter;i++)
{
nr_przekszt=random_unfair(P,max_il_przekszt);
x1=A[nr_przekszt]*x+B[nr_przekszt]*y+E[nr_przekszt];
y=C[nr_przekszt]*x+D[nr_przekszt]*y+F[nr_przekszt];
x=x1;
xe=skal.daj_ekr_x(x);
ye=skal.daj_ekr_y(y);
bitmap->SetPixel(xe,ye,give_Color(nr_przekszt));
 
}
graph->DrawImage(dynamic_cast<Image^>(bitmap),10,10);

 
}
};
}




Brak komentarzy:

Prześlij komentarz