Witam!
Mam problem z programem. Działa poprawnie, lecz jest jedna rzecz, którą muszę poprawić, gdyż jest ona niezgodna z pewnymi zasadami, a przynajmniej ja tak to zinterpretowałem. Mianowicie w klasie głównej (Image) pierwsze trzy zmienne są publiczne i odwołuję się do nich później, a powinny być prywatne, publiczna natomiast powinna być funkcja zapisu piksela, którą miałem w poprzednim programie, tylko troszkę inaczej ona musi teraz wyglądać i tu właśnie mam problem. Poprzedni program jest pod tym linkiem:
http://4programmers.net/Forum/viewtopic.php?id=133324&start=20
A to kod obecnego programu:
#include <iostream.h>
#include <math.h>
typedef unsigned char byte;
class Image
{
public:
byte *tab; //tablica przechowujaca wartosci pikseli obrazu
int width;
int height;
void alloc(int width, int height) //utworzenie tablicy
{
this->width = width;
this->height = height;
tab = new byte[width*height];
}
void dealloc() //zniszczenie tablicy
{
delete [] tab;
}
Image() //konstruktor
{
alloc(0, 0);
}
Image(int width, int height) //konstruktor
{
alloc(width, height);
}
~Image() //destruktor
{
dealloc();
}
int getWidth() const //zwraca width
{
return width;
}
int getHeight() const //zwraca height
{
return height;
}
void fillInImage(byte value) //nadaje ten sam kolor wszystkim pikselom
{
for (int i=0; i<width*height; i++)
{
tab[i] = value;
}
}
void print() //wyswietlenie obrazka
{
for (int i=0; i<width*height; i++)
{
cout<<tab[i]<<" ";
if(i%width==width-1)
cout<<"\n";
}
}
};
class Shape
{
public:
virtual void draw(Image& stab, byte color) = 0; //funkcja rysujaca figure
virtual void drawWithBorder(Image& stab, byte color, byte colorborder) = 0; //funkcja rysujaca figure i obramowanie
virtual ~ Shape() //destruktor
{}
};
class Rectangle : public Shape
{
public:
int x1,y1,szerokosc,wysokosc;
Rectangle(int x11, int y11, int szer, int wys): //konstruktor
x1(x11),
y1(y11),
szerokosc(szer),
wysokosc(wys)
{};
void draw( Image& stab, byte color)
{
char obwod;
int w;
int h;
int x2=x1+szerokosc-1;
int y2=y1+wysokosc-1;
for(w=0;w<stab.width;w++)
for(h=0;h<stab.height;h++)
{
if( (w>=x1&&w<=x2) && (h>=y1&&h<=y2) )
stab.tab[(h-1)*stab.width+(w-1)]=color;
}
}
void drawWithBorder( Image& stab, byte color, byte colorborder)
{
char obwod;
int w;
int h;
int x2=x1+szerokosc-1;
int y2=y1+wysokosc-1;
for(w=0;w<stab.width;w++)
for(h=0;h<stab.height;h++)
{
if((1+w>=x1&&w<=x2+1) && (1+h>=y1&&h<=y2+1) )
stab.tab[(h-1)*stab.width+(w-1)]=colorborder;
if( (w>=x1&&w<=x2) && (h>=y1&&h<=y2) )
stab.tab[(h-1)*stab.width+(w-1)]=color;
}
}
};
class Square : public Shape
{
public:
int x1,y1,bok;
Square(int x11,int y11,int boki): //konstruktor
x1(x11),
y1(y11),
bok(boki)
{};
void draw( Image& stab, byte color)
{
int w;
int h;
int x2=x1+bok-1;
int y2=y1+bok-1;
for(w=0;w<stab.width;w++)
for(h=0;h<stab.height;h++)
{
if( (w>=x1&&w<=x2) && (h>=y1&&h<=y2) )
stab.tab[(h-1)*stab.width+(w-1)]=color;
}
}
void drawWithBorder( Image& stab, byte color, byte colorborder)
{
int w;
int h;
int x2=x1+bok-1;
int y2=y1+bok-1;
for(w=0;w<stab.width;w++)
for(h=0;h<stab.height;h++)
{
if((1+w>=x1&&w<=x2+1) && (1+h>=y1&&h<=y2+1) )
stab.tab[(h-1)*stab.width+(w-1)]=colorborder;
if( (w>=x1&&w<=x2) && (h>=y1&&h<=y2) )
stab.tab[(h-1)*stab.width+(w-1)]=color;
}
}
};
class Circle : public Shape
{
public:
int x,y,r;
Circle(int x1, int y1, int r1): //konstruktor
x(x1),
y(y1),
r(r1)
{};
void draw(Image& stab, byte color)
{
for (int a=r;a>0;a=a-1)
{
for(int z=1;z<360;z=z+1 )
{
int h,w;
h=(int)(a*sin((3.14/180)*z)+(y-1));
w=(int)(a*cos((3.14/180)*z)+(x-1));
if(h>=0&&w>=0&&h<stab.height&&w<stab.width)
{
stab.tab[h*stab.width+w]=color;
}
}
}
}
void drawWithBorder(Image& stab, byte color, byte colorborder)
{
for (int a=r;a>0;a=a-1)
{
for(int z=1;z<360;z=z+1 )
{
int h,w;
h=(int)(a*sin((3.14/180)*z)+(y-1));
w=(int)(a*cos((3.14/180)*z)+(x-1));
if(h>=0&&w>=0&&h<stab.height&&w<stab.width)
{
stab.tab[h*stab.width+w]=color;
h=(int)((r+1)*sin((3.14/180)*z)+(y-1));
w=(int)((r+1)*cos((3.14/180)*z)+(x-1));
stab.tab[h*stab.width+w]=colorborder;
}
}
}
}
};
int main()
{
char obwod;
cout<<endl<<"Zostanie stworzony obiekt o wymiarach:"<<endl;
Image tab(30, 30);
cout<<endl<<"Width = "<<tab.getWidth();
cout<<endl<<"Height = "<<tab.getHeight()<<endl;
cout<<endl<<"Wypelnimy tlo jednolitym kolorem (kropkami), a nastepnie narysujemy figury."<<endl<<endl;
tab.fillInImage('.');
Shape *wsk;
Rectangle r(8,5,15,6); //okreslenie polozenia i rozmiaru prostokota
Circle c(21,21,6); //okreslenie srodka i promienia kola
Square s(5,18,5); //okreslenie polozenia i rozmiaru kwadratu
cout<<"Czy narysowac takze obramowania figur? t/n: ";
cin>>obwod;
switch (obwod)
{
case 't':
wsk= &r;
wsk -> drawWithBorder(tab,'R','*'); //wypelnienie prostokota
wsk= &c;
wsk -> drawWithBorder(tab,'C','*'); //wypenienie kola
wsk= &s;
wsk -> drawWithBorder(tab,'S','*'); //wypelnienie kwadratu
case 'n':
wsk= &r;
wsk -> draw(tab,'R'); //wypelnienie prostokota
wsk= &c;
wsk -> draw(tab,'C'); //wypenienie kola
wsk= &s;
wsk -> draw(tab,'S'); //wypelnienie kwadratu
}
cout<<endl;
tab.print(); //wywoluje funkcje print
return 0;
}
Oczywiście robiłem już pierwsze próby stworzenia funkcji zapisu piksela która zastąpiłaby późniejsze "grzebanie" w tablicy "tab", ale są one mierne w tej chwili :-/. Czy mógłby mi ktoś pomóc?