⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form.~cpp

📁 用户指定顶点
💻 ~CPP
📖 第 1 页 / 共 2 页
字号:
        for(int i=1;i<=Count;i++){
                t[i][i]=0;
        }
        for(int q=2;q<=Count;q++){
                for(int i=1;i<=Count-q+1;i++){
                        int j=i+q-1;

                        t[i][j]=t[i+1][j]+Ttriangle(i-1,i,j);
                        s[i][j]=i;

                for(int k=i+1;k<=q+i-1;k++){
                        double r=t[i][k]+t[k+1][j]+Ttriangle(i-1,k,j);
                        if(r<t[i][j]){
                                t[i][j]=r;
                                s[i][j]=k;
                        }
                }
        }
        }
        Image1->Canvas->Pen->Style=psDot;
        Image1->Canvas->Pen->Color=clNavy;
        Image1->Canvas->Pen->Width=1;
        DrawLines(1,Count);
        Image1->Canvas->Pen->Style=bsSolid;
        Image1->Canvas->Pen->Color=clAqua;
        Image1->Canvas->Pen->Width=2;
        for(int i=0;i<Count;i++){ //connect every two neighbor points except the last two
                Image1->Canvas->MoveTo(points[i].XCoordinate,points[i].YCoordinate);
                Image1->Canvas->LineTo(points[i+1].XCoordinate,points[i+1].YCoordinate);
        }

}


/*before 7.22        for(int k=2;k<=Count;k++){
                for(i=1;i<=Count-k+1;i++){
                        j=i+k-1;
                        if(s[i][j]!=0){
                                Image1->Canvas->MoveTo(points[i-1]->XCoordinate,
                                points[i-1]->YCoordinate);
                                Image1->Canvas->LineTo(points[k]->XCoordinate,
                                points[k]->YCoordinate);
                                Image1->Canvas->MoveTo(points[k]->XCoordinate,
                                points[k]->YCoordinate);
                                Image1->Canvas->LineTo(points[j]->XCoordinate,
                                points[j]->YCoordinate);
                        }
                }
7.21 具体实现
7.18建立本属于Function类 由于参数传递有问题 于7.20并为Form1类 */
//-----------------------------------------------------------------------------




void __fastcall TForm1::Button2Click(TObject *Sender)
{
        float length=0;
        decompose();
        if(Count==2){length=0;}
        else {length=(float)((t[1][Count]-PLength())/2); }    //计算真正的权值
        Image1->Canvas->TextOutA(0,0,"Length of all the chords is:  "+FloatToStr(length));
        Button2->Enabled=false;
        e1->Enabled=false;

        for(int i=0;i<Count;i++){
                for(int j=0;j<Count;j++){
                        Lasts[i][j]=s[i][j];
                }
        }
}

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

void TForm1::DrawLines(int i,int j){
        int k;
        if((i!=j)&&((s[i][j])!=0)){
                k=s[i][j];                  //get the docomposition point
                Image1->Canvas->MoveTo(points[i-1].XCoordinate,points[i-1].YCoordinate);
                Image1->Canvas->LineTo(points[k].XCoordinate,points[k].YCoordinate);
                Image1->Canvas->MoveTo(points[k].XCoordinate,points[k].YCoordinate);
                Image1->Canvas->LineTo(points[j].XCoordinate,points[j].YCoordinate);
                DrawLines(i,k);             //using recursion
                DrawLines(k+1,j);
        }
        HasDraw=2;
}


//------------------------------------------------------------------------------
__fastcall TForm1::~TForm1(){
        for(int i=0;i<Count;i++){
                delete NodeImage[i];
        }
        delete []points;
}
//-----------------------------------------------------------------------------






void __fastcall TForm1::New1Click(TObject *Sender)
{       int result;
        if(HasDraw==2){
        if(Form1->Caption==DefaultName){
                result=Application->MessageBoxA("Save the image?","Save",4);
        }
        if(result==6){
                        Save1Click(Sender);
                }
        }

        Form1->Caption="Unnamed";
        Image1->Canvas->TextOutA(0,0,"                                                                           ");
        Image1->Canvas->Pen->Color=clWhite;
        Image1->Picture->LoadFromFile(PathName+"code/4.bmp");
        Image1->Canvas->Refresh();
        if(PointFlag==1){
        for(int i=0;i<=Count;i++){
                delete NodeImage[i];
        }
        delete []points;
        }
        Count=-1;
        FormCreate(Sender);
        t[1][Count]=0;
        BitBtn1->SetFocus();


}
//---------------------------------------------------------------------------


void __fastcall TForm1::Exit1Click(TObject *Sender)
{
        Form1->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::e1Click(TObject *Sender)
{
        if(Count>=0){
                delete NodeImage[Count];
                Count--;
        }
        Edit5->Text=IntToStr(Count+1);
        Image1->Canvas->TextOutA(0,15*(Count+2),"                                   ");
        CanSet=false;
        if(Count<2){Button1->Enabled=false;   Button2->Enabled=false;}

}
//---------------------------------------------------------------------------

void __fastcall TForm1::connect1Click(TObject *Sender)
{
        Button1Click(Sender);        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Decompose1Click(TObject *Sender)
{
        Button2Click(Sender);        
}
//---------------------------------------------------------------------------




void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
        Edit3->Text=IntToStr(X);
        Edit4->Text=IntToStr(Y);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Save1Click(TObject *Sender)
{
        if(HasDraw==2){
        if(Form1->Caption==DefaultName){
                Form1->Caption=InputBox("save","Put in the name",DefaultName);
                }
        if(Form1->Caption!=DefaultName){
                Image1->Picture->SaveToFile(PathName+"history/"+Form1->Caption+".bmp");
        }
        }

}
//---------------------------------------------------------------------------


void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
        if(xEnter==1&&yEnter==1&&IntFlag==1){
                if(TagetX>=0&&TagetX<=Image1->Width
                &&TagetY>=0&&TagetY<=Image1->Height){
                for(int i=0;i<=Count;i++){
                if(points[i].select==1){
                        select=1;
                        NodeImage[i]->Left=TagetX+(Image1->Left)-4.5;
                        NodeImage[i]->Top=TagetY-4.5+(Image1->Top);
                        points[i].XCoordinate=TagetX;
                        points[i].YCoordinate=TagetY;
                        Image1->Canvas->TextOutA(0,15*(i+1),IntToStr(i+1)+"    "+
                        IntToStr(points[i].XCoordinate)+"  "+IntToStr(points[i].YCoordinate));
                        NodeImage[i]->Picture->LoadFromFile(PathName+"/code/2.bmp");
                        }
                }
                if(select!=1){
                        Count++;
                        points[Count].SetNode(TagetX,TagetY);
                        NodeImage[Count]=new TImage(Image1);  //realize a n object
                        NodeImage[Count]->Picture->LoadFromFile(PathName+"/code/2.bmp");
                        NodeImage[Count]->Left=TagetX+(Image1->Left)-4.5;
                        NodeImage[Count]->Top=TagetY-4.5+(Image1->Top);
                        NodeImage[Count]->Width=9;
                        NodeImage[Count]->Height=9;
                        NodeImage[Count]->Parent=Form1;
                        NodeImage[Count]->Tag=Count;
                        NodeImage[Count]->OnClick=NodeImageClick;
                        Edit5->Text=IntToStr(Count+1);
                        Image1->Canvas->TextOutA(0,15*(Count+1),IntToStr(Count+1)+"    "+
                        IntToStr(points[Count].XCoordinate)+"  "+IntToStr(points[Count].YCoordinate));
                        PointFlag=1;
                        }
                }
        }
        if(Count>1){
                Button1->Enabled=true;
                connect1->Enabled=true;
        }
        Edit6->Clear();
        Edit7->Clear();
        Edit6->Enabled=false;
        Edit7->Enabled=false;
        CanSet=false;
        BitBtn2->Enabled=false;
        IntFlag=1;
        BitBtn1->SetFocus();
        TagetX=-1;
        TagetY=-1;
        select=0;
        for(int i=0;i<=Count;i++){
                        points[i].select=0;
        }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit6Change(TObject *Sender)
{       BitBtn2->Enabled=true;
        BitBtn2->Enabled=true;
        xEnter=1;
        if(Edit6->Text!=""&&IntFlag==1)
        TagetX=StrToInt(Edit6->Text);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit7Change(TObject *Sender)
{       BitBtn2->Enabled=true;
        yEnter=1;
        if(Edit7->Text!=""&&IntFlag==1)
        TagetY=StrToInt(Edit7->Text);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Edit6KeyPress(TObject *Sender, char &Key)
{
        BitBtn2->Enabled=true;
        if(Key!=8&&Key!=46&&Key!=13){
        if((Key<=47)||(Key>=58)){
        IntFlag=0;
        }
        }
        if(Key==13)Edit7->SetFocus();

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit7KeyPress(TObject *Sender, char &Key)
{       BitBtn2->Enabled=true;
        if(Key!=8&&Key!=46&&Key!=13){
        if((Key<=47)||(Key>=58)){
        IntFlag=0;
        }
        }
        if(Key==13) BitBtn2->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn2KeyPress(TObject *Sender, char &Key)
{
        if(Key==13) BitBtn2Click(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn1KeyPress(TObject *Sender, char &Key)
{
        if(Key==13)   BitBtn1Click(Sender);
}
//---------------------------------------------------------------------------

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


void __fastcall TForm1::History1Click(TObject *Sender)
{
        if(HasDraw==2){
        if(Form1->Caption==DefaultName){
                int result=Application->MessageBoxA("Save the image?","Save",4);
                if(result==1){
                        Save1Click(Sender);
                }
        }
        }
        New1Click(Sender);

        if(Open->Execute())
        {
                Image1->Picture->LoadFromFile(Open->FileName);
                Form1->Caption=Open->FileName;
                BitBtn1->Enabled=false;
                BitBtn2->Enabled=false;
                Delete->Enabled=false;
                e1->Enabled=false;
                connect1->Enabled=false;
                Decompose1->Enabled=false;
        }
        Save1->Enabled=false;
}
//---------------------------------------------------------------------------




void __fastcall TForm1::DeleteClick(TObject *Sender)
{
        e1Click(Sender);        
}
//---------------------------------------------------------------------------





void __fastcall TForm1::About1Click(TObject *Sender)
{
        About->Show();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Help2Click(TObject *Sender)
{
        AnsiString ExePath=ExtractFilePath(Application->ExeName);
	ShellExecute(NULL,"open",(ExePath+"PDSHELP.chm").c_str(),
    	NULL, NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -