1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| TFleche *Fleche;
bool MoveFlag = false;
int Movex,Movey;
__fastcall TFleche::TFleche(TPoint P1, TPoint P2, TPoint P3)
: TCustomControl(Owner)
{
Points[0] = P1; // point d'arrivée
Points[1] = P2; // point de contrôle 1 arc
Points[2] = P2; // point de contrôle 2 arc
Points[3] = P3; // point de départ
label=new TLabel(this);
InsertControl(label);
label->Caption="poids";
begsq=new TShape(this);
InsertControl(begsq);
begsq->Shape=stCircle;
begsq->Visible=true;
begsq->Brush->Color=clGreen;
begsq->Height=5;
begsq->Width=5;
endsq=new TShape(this);
InsertControl(endsq);
endsq->Shape=stCircle;
endsq->Visible=true;
endsq->Brush->Color=clBlue;
endsq->Height=5;
endsq->Width=5;
}
//--
int __fastcall TFleche::Arrondir(double A)
{
int a = A;
double r = (A - a) * 2;
return(a + r);
}
//--
TPoint __fastcall TFleche::Rotation(int X, int Y, double *S, double *C)
{
double h = hypot(X,Y);
double sina = X/h;
double cosa = Y/h;
double x = h * ((sina * *C) + (*S * cosa));
double y = h * ((cosa * *C) - (*S * sina));
return(Point(Arrondir(x),Arrondir(y)));
}
//--
void __fastcall TFleche::Draw(TCanvas *C)
{
C->Pen->Style = psSolid;
C->Pen->Mode = pmCopy;
//calculer sinus et cosinus de l'angle
int x = Points[1].x - Points[0].x;
int y = Points[1].y - Points[0].y;
double h = hypot(x,y);
double si = 0.0;
double co = 1.0;
if(h != 0.0)
{
si = y / h;
co = x / h;
}
//calculer la rotation à appliquer sur chaque sommet du triangle de la flèche
TPoint P1 = Rotation(-4, - 4, &si, &co);
int x1 = Points[0].x - P1.x;
int y1 = Points[0].y + P1.y;
TPoint P2 = Rotation(-4, +4, &si, &co);
int x2 = Points[0].x - P2.x;
int y2 = Points[0].y + P2.y;
TPoint P3 = Rotation(6, 0, &si, &co);
int x3 = Points[0].x - P3.x;
int y3 = Points[0].y + P3.y;
C->Pen->Color = clBlack;
C->MoveTo(x1,y1);
C->LineTo(x2,y2);
C->LineTo(x3,y3);
C->LineTo(x1,y1);
C->Brush->Color = clBlack;
C->FloodFill(Points[0].x, Points[0].y, clBlack, fsBorder);
C->PolyBezier(Points, 3);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::DrawFleche()
{
// Fleche->Canvas->Pen->Color = clBlack;
// Fleche->Canvas->Brush->Color = clBlack;
// Fleche->Canvas->Rectangle(0,0,ClientWidth, ClientHeight);
Fleche->Draw(Fleche->Canvas);
// dessiner le point de contrôle
int x = Fleche->Points[1].x;
int y = Fleche->Points[1].y;
Fleche->Canvas->Brush->Color = clRed;
Fleche->Canvas->Rectangle(x - 3, y - 3, x + 4, y + 4);
Fleche->label->Left=Fleche->Points[1].x+2;
Fleche->label->Top =Fleche->Points[1].y+2;
Fleche->begsq->Left=Fleche->Points[3].x-2;
Fleche->begsq->Top =Fleche->Points[3].y-2;
Fleche->endsq->Left=Fleche->Points[0].x-2;
Fleche->endsq->Top =Fleche->Points[0].y-2;
}
//---------------------------------------------------------------------------
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
Fleche = new TFleche(Point(100,100), Point(200,50), Point(200,200));
InsertControl(Fleche);
Fleche->label->Parent=MainForm;
Fleche->begsq->Parent=MainForm;
Fleche->endsq->Parent=MainForm;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormPaint(TObject *Sender)
{
DrawFleche();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
int minx = Fleche->Points[1].x - 3;
int maxx = Fleche->Points[1].x + 4;
int miny = Fleche->Points[1].y - 3;
int maxy = Fleche->Points[1].y + 4;
if ((X >= minx) && (X < maxx) && (Y >= miny) && (Y <= maxy))
{
MoveFlag = true;
Movex = X;
Movey = Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if (MoveFlag)
{
int x = Fleche->Points[1].x + X - Movex;
int y = Fleche->Points[1].y + Y - Movey;
Fleche->Points[1] = Point(x,y);
Fleche->Points[2] = Point(x,y);
Movex = X;
Movey = Y;
DrawFleche();
}
Hint="x="+IntToStr(X)+",y="+IntToStr(Y);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
MoveFlag = false;
}
//--------------------------------------------------------------------------- |
Partager