Компонент для редактирования и просмотра функций.
Компонент должен позволять задавать функции при помощи последовательности точек. Функция отображается при помощи линейной аппроксимации. Компонент должен иметь следующие режимы: «Выделение» — перетаскивание точек при помощи мыши, «Добавление» — добавление точек, «Удаление» — удаление точек, «Масштабирование» — изменение масштаба просмотра функции, «Перемещение» — перемещение координатной плоскости (изменение экранных координат начала отсчета). Пример данного компонента приведен ниже (третья точка слева — выделена и перетаскивается):
Текст компонента:
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
unit GraphRed; interface uses SysUtils, Classes, Controls,Graphics,Types,ExtCtrls,windows; const RS=5;//размер квадрата/2 type TpointsListItemP=^TpointsListItem; TpointsListItem=record Point:Tpoint; Next,Previous:TpointsListItemP; end; TGraphRed = class(TCustomControl) private { Private declarations } Move:boolean; FScaleX,FScaleY:real; protected { Protected declarations } PointsListRoot:TpointsListItemP; SelectedPoint:TPointsListItemP; procedure SetScaleX(sx:real); procedure SetScaleY(sy:real); public { Public declarations } constructor Create(AOwner:TComponent);override; procedure Paint; override; procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override; procedure MouseMove(Shift: TShiftState; X, Y: Integer);override; procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override; procedure Refresh; Function InsertPoint(x,y:longint):boolean; Function RemovePoint(var p:TPointsListItemP):boolean; published { Published declarations } property ScaleX:real read FScaleX write SetScaleX; property ScaleY:real read FScaleY write SetScaleY; end; procedure Register; implementation procedure Register; begin RegisterComponents('MyComponents', [TGraphRed]); end; constructor TGraphRed.Create(AOwner:TComponent); begin inherited Create(AOwner); end; procedure TGraphRed.Paint; const nx=3;ny=3;step=50; var cur:TpointsListItemP; point:tpoint; Color:TColor; n,m,y,x:integer; begin //canvas.Rectangle(10,10,100,100); //положить код прорисовки здесь Color:=RGB(255,255,255); canvas.Brush.Color:=Color; canvas.FillRect(GetClientRect()) ; //Прорисовка сетки n:=ClientHeight; While (n>0) do begin n:=n-step; y:=Round((ClientHeight-n)*ScaleY); point.X:=nx; point.y:=n; canvas.PenPos:=point; canvas.Pen.Color:=RGB(200,200,200); canvas.LineTo(ClientWidth,n); canvas.Pen.Color:=RGB(0,0,0); canvas.TextOut(nx,n+canvas.Font.Height div 2,inttostr(y)); end; m:=0; While (m< ClientHeight) do begin m:=m+step; x:=Round(m*ScaleX); y:=ClientHeight-ny+canvas.Font.Height; point.x:=m; point.Y:=y; canvas.PenPos:=point; canvas.Pen.Color:=RGB(200,200,200); canvas.LineTo(m,0); canvas.Pen.Color:=RGB(0,0,0); canvas.TextOut(m-canvas.Font.Size div 2,y,inttostr(x)); end; //Прорисовка фигуры cur:=pointsListRoot; while (cur<>NIL) do begin if (cur^.Previous<>NIL) then begin point.X:=round(cur^.point.x / ScaleX); point.Y:=ClientHeight-Round(cur^.point.Y/ScaleY); canvas.PenPos:=point; canvas.LineTo(Round(cur^.Previous^.Point.x / ScaleX),Clientheight-Round(cur^.Previous^.point.y/ScaleY)) end; if (SelectedPoint=cur) Then canvas.Brush.Color:=RGB(0,255,0); canvas.Rectangle(Round(cur^.point.x / scaleX)-rs,ClientHeight-Round(cur^.point.y/ScaleY)-rs,Round(cur^.point.x / ScaleX)+rs,ClientHeight-Round(cur^.point.y/ScaleY)+rs); if (SelectedPoint=cur) Then canvas.Brush.Color:=Color; cur:=cur^.Next; end; inherited Paint(); end; procedure TGraphRed.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var curRS:longint; begin inherited MouseDown(Button,shift,x,y); y:=Round((clientHeight-y)*ScaleY); x:=Round(x*ScaleX); if(ScaleX>ScaleY) Then CurRS:=RS*Round(ScaleX+1) else CurRS:=RS*Round(ScaleY+1); SelectedPoint:=PointsListRoot; while (SelectedPoint<>NIL) do begin if (x<SelectedPoint^.Point.x+CurRS) and (X>SelectedPoint^.point.X-CurRS) and (Y>SelectedPoint^.point.y-CurRS) and (Y<SelectedPoint^.point.y+CurRS) Then break; SelectedPoint:=SelectedPoint^.Next; end; if (SelectedPoint<>NIL) Then begin case Button of mbLeft: begin Move:=TRUE; Paint(); end; mbRight: begin RemovePoint(SelectedPoint); Paint(); end; end; end else begin if (Button=mbLEft) then begin InsertPoint(x,y); Refresh(); end; end; end; procedure TGraphRed.MouseMove( Shift: TShiftState; X, Y: Integer); var np:TPointsListItemP; curRS:longint; begin inherited MouseMove(shift,x,y); y:=Round((Clientheight-y)*ScaleY); x:=Round(x*ScaleX); if (SelectedPoint<>NIL) Then begin if (Move) Then begin //RemovePoint(SElectedPoint); //InsertPoint(x,y); SelectedPoint^.point.x:=x; SelectedPoint^.point.y:=y; if (SelectedPoint^.Next<>NIL) Then if (SelectedPoint^.Next^.Point.x<SelectedPoint^.point.x) Then begin np:=SelectedPoint^.Next; np^.Previous:=SelectedPoint^.Previous; if (SelectedPoint^.Previous<>NIL) Then SelectedPoint^.Previous^.Next:=np; SelectedPoint^.Previous:=np; SelectedPoint^.Next:=np^.Next; np^.Next:=SelectedPoint; if (SelectedPoint^.Next<>NIL) Then SelectedPoint^.Next^.Previous:=SelectedPoint; if (SelectedPoint=PointsListRoot) Then PointsListRoot:=np; end ; if (SelectedPoint^.Previous<>NIL) Then begin if (SelectedPoint^.Previous^.Point.x>SelectedPoint^.Point.x) Then begin np:=SelectedPoint^.Previous; SelectedPoint^.Previous :=np^.Previous; np^.Next:=SelectedPoint^.Next; if (np^.Previous <>NIL) Then np^.Previous^.Next:=SelectedPoint; if (SelectedPoint^.Next<>NIL) Then SelectedPoint^.Next^.Previous:=np; np^.Previous:=SelectedPoint; SelectedPoint^.Next:=np; if (np=PointsListRoot) Then PointsListRoot:=Selectedpoint; end; end; Paint; end; end; end; procedure TGraphred.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin inherited MouseUp(Button,Shift,x,y); Move:=FALSE; end; procedure TGraphRed.Refresh ; begin Paint; end; Function TGraphRed.InsertPoint(x,y:longint):boolean; var newp,cur:TpointsListItemP; begin new(newp); if (newp=NIL) Then InsertPoint:=FALSE; newp^.point.x:=x; newp^.point.y:=y; cur:=pointsListRoot; if (pointsListRoot<>NIL) Then begin //Поиск места вставки (список упорядочен по увеличению x) if (pointsListRoot^.Point.x<x) Then begin while (cur^.Next<>NIL) do begin if (cur^.Next^.Point.x>x) Then break; cur:=cur^.Next; end; end else begin cur:=NIL; end; end; if (cur<>NIL) Then begin newp^.Previous :=cur; newp^.Next:=cur^.Next; if (cur^.Next<>NIL) then cur^.Next^.Previous:=newp; cur^.Next:=newp; end else begin newp^.Next:=pointsListRoot; newp^.Previous :=NIL; if (pointslistroot<>NIL) Then PointsListRoot^.Previous :=newp; pointsListRoot:=newp; end; SelectedPoint:=newp; //Если дошли до этого места, то добавление было удачным InsertPoint:=TRUE; end; Function TGraphRed.RemovePoint(var p:TPointsListItemP):boolean; begin if (PointsListroot=p) Then PointsListRoot:=p^.Next; if (p^.Previous<>NIL) Then p^.Previous^.Next:=p^.Next; if (p^.Next<>NIL) Then p^.Next^.Previous:=p^.Previous; dispose(p); p:=NIL; RemovePoint:=TRUE; end; procedure TGraphRed.SetScaleX(sx:real); begin if sx=0 then sx:=1; FScaleX:=sx; Paint; end; procedure TGraphRed.SetScaleY(sy:real); begin if sy=0 then sy:=1; FScaleY:=sy; paint; end; end. |
Вывод: Была создана собственная компонента в Delphi 7.
Архив с исходными кодами к лабораторной работе № 3 по визуальному программированию