Soap 2 day.

Форум — Ответы     (  К темам )
 ?  gali: C++ (17-05-2003 08:54:32)
Помогите построить граф, заданный матрицей смежности, или подскажите какие-нибудь графические библиотеки в C++ Builder
 Георгий (17-05-2003 10:57:56)
давай определимся что тебе именно надо:
1. построить в памяти машины граф (например, чтоб потом что-то считать по нему)
2. вывести на экран картинку похожую на граф
 gali (17-05-2003 13:44:20)
Спасибо за ответ. Мне нужно построить "картинку, похожую на граф"
 Георгий (17-05-2003 17:58:15)
компонентов я не знаю, но сейчас за 1,5 часа написал такой код:
TForm1 *Form1;
static Graphics::TBitmap *bmp=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
bmp=new Graphics::TBitmap;
bmp->Width=200;
bmp->Height=200;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
if (bmp)
{
this->PaintBox1->Canvas->Draw(0,0,bmp);
}
}
//---------------------------------------------------------------------------

__fastcall TForm1::~TForm1()
{
bmp->FreeImage();
delete bmp;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//ñ÷èòàåì ÷èñëî óçëîâ
TStrings * str=new TStringList;
AnsiString str1;
int i;
for (i=0;i<10;i++)
if (!this->StringGrid1->Cells[0][i].IsEmpty() && !this->StringGrid1->Cells[1][i].IsEmpty())
{
if (str->IndexOf(this->StringGrid1->Cells[0][i])==-1)str->Add(this->StringGrid1->Cells[0][i]);
if (str->IndexOf(this->StringGrid1->Cells[1][i])==-1)str->Add(this->StringGrid1->Cells[1][i]);
};
this->Memo1->Lines->AddStrings(str);
//&#240;&#224;&#231;&#228;&#224;&#184;&#236; &#234;&#238;&#238;&#240;&#228;&#232;&#237;&#224;&#242;&#251; &#243;&#231;&#235;&#224;&#236; &#232; &#240;&#232;&#241;&#243;&#229;&#236; &#243;&#231;&#235;&#251;
TRect* a=new TRect[str->Count];
for (i=0;i<str->Count;i++)
{
a[i].Left =(i%5)*40;
a[i].Top =(i/5)*40;
a[i].Right =a[i].Left+20;
a[i].Bottom=a[i].Top +20;
bmp->Canvas->Pen->Color=clBlack;
bmp->Canvas->Pen->Style=psSolid;
bmp->Canvas->Brush->Style=bsClear;
bmp->Canvas->TextRect(a[i],a[i].Left+5,a[i].Top+5,str->Strings[i]);
bmp->Canvas->Ellipse(a[i]);
};
//&#240;&#232;&#241;&#243;&#229;&#236; &#241;&#226;&#255;&#231;&#232;
TRect r1,r2;
for (i=0;i<10;i++)
if (!this->StringGrid1->Cells[0][i].IsEmpty() && !this->StringGrid1->Cells[1][i].IsEmpty())
{
r1=a[str->IndexOf(this->StringGrid1->Cells[0][i])];
r2=a[str->IndexOf(this->StringGrid1->Cells[1][i])];
bmp->Canvas->MoveTo((r2.Left+r2.Right)/2,(r2.Top+r2.Bottom)/2);
bmp->Canvas->LineTo((r1.Left+r1.Right)/2,(r1.Top+r1.Bottom)/2);
};
delete[] a;
delete str;
this->PaintBox1->Refresh();
}

вроде рисует узлы и связи между ними.
если хочешь, то на почту могу прислать весь исходник
 Георгий (18-05-2003 03:23:17)
а теперь с возможностью мышкой таскать узлы:

TForm1 *Form1;
static Graphics::TBitmap *bmp=0;
static TStrings * str;
static TRect* a;
static TRect* movedPtr=0;
static int move_x,move_y;
bool Hit(const TRect& r,int X, int Y)
{
return (r.Left<X) && (r.Right >X) && (r.Top <Y) && (r.Bottom>Y);
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
bmp=new Graphics::TBitmap;
bmp->Width=200;
bmp->Height=200;
str=new TStringList;
a=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
if (bmp)
{
this->PaintBox1->Canvas->Draw(0,0,bmp);
}
}
//---------------------------------------------------------------------------

__fastcall TForm1::~TForm1()
{
bmp->FreeImage();
delete bmp;
delete[] a;
delete str;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//&#241;&#247;&#232;&#242;&#224;&#229;&#236; &#247;&#232;&#241;&#235;&#238; &#243;&#231;&#235;&#238;&#226;
str->Clear();
AnsiString str1;
int i;
for (i=0;i<10;i++)
if (!this->StringGrid1->Cells[0][i].IsEmpty() && !this->StringGrid1->Cells[1][i].IsEmpty())
{
if (str->IndexOf(this->StringGrid1->Cells[0][i])==-1)str->Add(this->StringGrid1->Cells[0][i]);
if (str->IndexOf(this->StringGrid1->Cells[1][i])==-1)str->Add(this->StringGrid1->Cells[1][i]);
};
this->Memo1->Lines->AddStrings(str);
//&#240;&#224;&#231;&#228;&#224;&#184;&#236; &#234;&#238;&#238;&#240;&#228;&#232;&#237;&#224;&#242;&#251; &#243;&#231;&#235;&#224;&#236; &#232; &#240;&#232;&#241;&#243;&#229;&#236; &#243;&#231;&#235;&#251;
if (a) delete[]a;
a=new TRect[str->Count];
for (i=0;i<str->Count;i++)
{
a[i].Left =(i%5)*40;
a[i].Top =(i/5)*40;
a[i].Right =a[i].Left+20;
a[i].Bottom=a[i].Top +20;
};//for (i=0;i<str->Count;i++)
this->Button2->Click();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (movedPtr)
{
movedPtr->Top +=Y-move_y;
movedPtr->Bottom+=Y-move_y;
movedPtr->Left +=X-move_x;
movedPtr->Right +=X-move_x;
move_y=Y;
move_x=X;
Button2->Click();
};
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i;
bmp->Canvas->Brush->Style=bsSolid;
bmp->Canvas->Brush->Color=clWhite;
bmp->Canvas->FillRect(TRect(0,0,200,200));
for (i=0;i<str->Count;i++)
{
bmp->Canvas->Pen->Color=clBlack;
bmp->Canvas->Pen->Style=psSolid;
bmp->Canvas->Brush->Style=bsClear;
bmp->Canvas->TextRect(a[i],a[i].Left+5,a[i].Top+5,str->Strings[i]);
bmp->Canvas->Ellipse(a[i]);
};//for (i=0;i<str->Count;i++)
//&#240;&#232;&#241;&#243;&#229;&#236; &#241;&#226;&#255;&#231;&#232;
TRect r1,r2;
for (i=0;i<10;i++)
if (!this->StringGrid1->Cells[0][i].IsEmpty() && !this->StringGrid1->Cells[1][i].IsEmpty())
{
r1=a[str->IndexOf(this->StringGrid1->Cells[0][i])];
r2=a[str->IndexOf(this->StringGrid1->Cells[1][i])];
bmp->Canvas->MoveTo((r2.Left+r2.Right)/2,(r2.Top+r2.Bottom)/2);
bmp->Canvas->LineTo((r1.Left+r1.Right)/2,(r1.Top+r1.Bottom)/2);
};
this->PaintBox1->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
//&#237;&#224;&#245;&#238;&#230;&#232;&#236; &#237;&#224; &#247;&#242;&#238; &#237;&#224;&#230;&#224;&#235;&#232; &#232; &#239;&#238;&#228;&#227;&#238;&#242;&#224;&#226;&#235;&#232;&#226;&#224;&#229;&#236; &#253;&#242;&#238; &#234; &#228;&#226;&#232;&#230;&#229;&#237;&#232;&#254;
int i;
if (Button==mbLeft)
{
for (i=0;i<str->Count;i++)
if (Hit(a[i],X,Y)){movedPtr=&a[i];move_x=X;move_y=Y;break;};
//isMoved=true;
};//if (Button==mbLeft)
}
//---------------------------------------------------------------------------

void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
//&#228;&#226;&#232;&#230;&#229;&#237;&#232;&#229; &#238;&#234;&#238;&#237;&#247;&#229;&#237;&#238;
int i;
if (Button==mbLeft && movedPtr)
{
movedPtr->Top +=Y-move_y;
movedPtr->Bottom+=Y-move_y;
movedPtr->Left +=X-move_x;
movedPtr->Right +=X-move_x;
Button2->Click();
//isMoved=true;
};//if (Button==mbLeft)
movedPtr=0;
}
//---------------------------------------------------------------------------

опять же — если кто заинтересовался, то могу выслать весь проект в архиве, по почте
 Provider (18-05-2003 03:47:01)
Я заинтересовался!!! И даже очень...
Вышли весь пожалуйста, если не трудно....
арес указан.....
 gali (18-05-2003 08:35:47)
Спасибо, мне очень понравилось. Если можешь, то пришли мне по почте.
 Георгий (18-05-2003 10:11:29)
кто просил, тем послал, а если ещё будут желающие, то берите с georiestar.pisem.net — там это называется graph.rar