Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.236.100.210] |
|
Страницы: (2) 1 [2] все ( Перейти к последнему сообщению ) |
Сообщ.
#16
,
|
|
|
unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls; type TPointFlt = packed record X, Y: Double; end; TPointAim = packed record Position: TPoint; Pressed: Boolean; end; TPointLink = packed record Position: TPoint; Speed: TPointFlt; Accelerarion: TPointFlt; Link: LongInt; Rope: LongInt; Cycle: Boolean; end; { TForm1 } TForm1 = class(TForm) Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; _X, _Y: Integer); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); procedure FormPaint(Sender: TObject); procedure Timer1Timer(Sender: TObject); private public end; var Form1: TForm1; balance: TPointLink = (Position: (X: 25; Y: 10); Speed: (X: 0; Y: 0); Accelerarion: (X: 0; Y: 98000); Link: -1; Rope: 0; Cycle: False); points: array [0 .. 3] of TPoint = ((x:80; y:100), (x:150; y:120), (x:200; y:80), (x:250; y:110)); curpos: TPointAim = (Position: (X: 0; Y: 0); Pressed: False); latest: TDateTime; implementation {$R *.lfm} { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject); begin Repaint; end; function dst(X, Y: LongInt): Double; overload; begin dst := sqrt(X * X + Y * Y); end; function dst(X, Y: Double): Double; overload; begin dst := sqrt(X * X + Y * Y); end; procedure TForm1.FormPaint(Sender: TObject); var i: LongInt; t: TDateTime; c: Double; u: TPointFlt; z, v, w: TPoint; s: TColor; begin t := latest; latest := Now; t := (latest - t) * 86400; for i := Low(points) to High(points) do begin Canvas.Arc(points[i].x - 10, points[i].y - 10, points[i].x + 10, points[i].y + 10, points[i].x, points[i].y - 10, points[i].x, points[i].y - 10); with balance, curpos.Position do if (curpos.Pressed) and (dst(X - points[i].x, Y - points[i].y) < sqrt(800)) then begin Canvas.Arc(points[i].x - 7, points[i].y - 7, points[i].x + 7, points[i].y + 7, points[i].x, points[i].y - 7, points[i].x, points[i].y - 7); if (Link = -1) or (Link <> i) then begin Link := i; Rope := Round(dst(Position.X - points[i].x, Position.Y - points[i].y)); end; end else if not curpos.Pressed then balance.Link := -1; end; with balance do begin if Link <> -1 then begin u.X := Position.X + t * (Speed.X + t * Accelerarion.X / 2000); u.Y := Position.Y + t * (Speed.Y + t * Accelerarion.Y / 2000); c := dst(points[Link].x - u.X, points[Link].y - u.Y); if (not Cycle) and (c < Rope) then begin Position.X := Round(u.X); Position.Y := Round(u.Y); Speed.X := Speed.X + t * Accelerarion.X / 1000; Speed.Y := Speed.Y + t * Accelerarion.Y / 1000; end else begin Cycle := True; if c > Rope then begin Position.X := Round(u.X + (c - Rope) * (points[Link].x - Position.X) / Rope); Position.Y := Round(u.Y + (c - Rope) * (points[Link].y - Position.Y) / Rope); end; c := dst(Accelerarion.X / 1000, Accelerarion.Y / 1000); u.X := (c / Rope) * (points[Link].x - Position.X) + Accelerarion.X / 1000; u.Y := (c / Rope) * (points[Link].y - Position.Y) + Accelerarion.Y / 1000; inc(Position.X, Round(t * (Speed.X + t * u.X / 2))); inc(Position.Y, Round(t * (Speed.Y + t * u.Y / 2))); Speed.X := Speed.X + t * u.X; Speed.Y := Speed.Y + t * u.Y; z := Point(Round(Position.X + u.X - Accelerarion.X / 1000), Round(Position.Y + u.Y - Accelerarion.Y / 1000)); v := Point(Round(Position.X + Accelerarion.X / 1000), Round(Position.Y + Accelerarion.Y / 1000)); w := Point(Round(Position.X + u.X), Round(Position.Y + u.Y)); end; end else begin inc(Position.X, Round(t * (Speed.X + t * Accelerarion.X / 2000))); inc(Position.Y, Round(t * (Speed.Y + t * Accelerarion.Y / 2000))); Speed.X := Speed.X + t * Accelerarion.X / 1000; Speed.Y := Speed.Y + t * Accelerarion.Y / 1000; Cycle := False; end; if Position.X < 6 then begin Position.X := 6; Speed.X := 0; Accelerarion.X := 40000; if Position.X < ClientWidth div 2 then Accelerarion.Y := Accelerarion.Y + 100000 * Random else Accelerarion.Y := Accelerarion.Y - 100000 * Random; end; if Position.Y < 6 then begin Position.Y := 6; Speed.Y := 0; Accelerarion.Y := 98000; if Position.Y < ClientHeight div 2 then Accelerarion.X := Accelerarion.X + 25000 * Random else Accelerarion.X := Accelerarion.X - 25000 * Random; end; if Position.X > ClientWidth - 6 then begin Position.X := ClientWidth - 6; Speed.X := 0; Accelerarion.X := -43000; if Position.X < ClientWidth div 2 then Accelerarion.Y := Accelerarion.Y + 100000 * Random else Accelerarion.Y := Accelerarion.Y - 100000 * Random; end; if Position.Y > ClientHeight - 6 then begin Position.Y := ClientHeight - 6; Speed.Y := 0; Accelerarion.Y := -98000; if Position.Y < ClientHeight div 2 then Accelerarion.X := Accelerarion.X + 25000 * Random else Accelerarion.X := Accelerarion.X - 25000 * Random; end; end; with balance, balance.Position, Canvas, Canvas.Pen do begin if balance.Link <> -1 then begin MoveTo(X, Y); LineTo(points[balance.Link].x, points[balance.Link].y); end; Arc(X - 5, Y - 5, X + 5, Y + 5, X, Y - 5, X, Y - 5); if Cycle then begin s := Color; Width := 1; Style := psDashDotDot; Color := clGreen; MoveTo(z.x, z.y); LineTo(X, Y); Color := clBlue; LineTo(v.x, v.y); Width := 2; Style := psSolid; Color := clPurple; MoveTo(w.x, w.y); LineTo(X, Y); Width := 1; Color := s; end; end; end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); begin with curpos.Position do begin X := _X; Y := _Y; curpos.Pressed := True; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Randomize; latest := Now; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; _X, _Y: Integer); begin with curpos.Position do if curpos.Pressed then begin X := _X; Y := _Y; end; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); begin curpos.Pressed := False; end; end. |
Сообщ.
#17
,
|
|
|
В Delphi
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TPointFlt = packed record X, Y: Double; end; type TPointAim = packed record Position: TPoint; Pressed: Boolean; end; TPointLink = packed record Position: TPoint; Speed: TPoint; Accelerarion: TPoint; Link: LongInt; Rope: LongInt; Cycle: Boolean; end; type TForm1 = class(TForm) Timer1: TTimer; procedure FormPaint(Sender: TObject); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; _X, _Y: Integer); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; balance: TPointLink = (Position: (X: 25; Y: 10); Speed: (X: 0; Y: 0); Accelerarion: (X: 0; Y: 98000); Link: -1; Rope: 0); points: array [0 .. 3] of TPoint = ((x:80; y:100), (x:150; y:120), (x:200; y:80), (x:250; y:110)); latest: TDateTime; curpos: TPointAim = (Position: (X: 0; Y: 0); Pressed: False); implementation {$R *.dfm} function dst(X, Y: LongInt): Double;overload; begin dst := sqrt(X * X + Y * Y); end; function dst(X, Y: Double): Double; overload; begin dst := sqrt(X * X + Y * Y); end; procedure TForm1.FormCreate(Sender: TObject); begin Randomize; latest := Now; end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); begin with curpos.Position do begin X := _X; Y := _Y; curpos.Pressed := True; end; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; _X, _Y: Integer); begin with curpos.Position do if curpos.Pressed then begin X := _X; Y := _Y; end; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); begin curpos.Pressed := False; end; procedure TForm1.FormPaint(Sender: TObject); var i: LongInt; t: TDateTime; c: Double; u: TPointFlt; z, v, w: TPoint; s: TColor; begin t := latest; latest := Now; t := (latest - t) * 86400; for i := Low(points) to High(points) do begin Canvas.Arc(points[i].x - 10, points[i].y - 10, points[i].x + 10, points[i].y + 10, points[i].x, points[i].y - 10, points[i].x, points[i].y - 10); with balance, curpos.Position do if (curpos.Pressed) and (dst(X - points[i].x, Y - points[i].y) < sqrt(800)) then begin Canvas.Arc(points[i].x - 7, points[i].y - 7, points[i].x + 7, points[i].y + 7, points[i].x, points[i].y - 7, points[i].x, points[i].y - 7); if (Link = -1) or (Link <> i) then begin Link := i; Rope := Round(dst(Position.X - points[i].x, Position.Y - points[i].y)); end; end else if not curpos.Pressed then balance.Link := -1; end; with balance do begin if Link <> -1 then begin u.X := Position.X + t * (Speed.X + t * Accelerarion.X / 2000); u.Y := Position.Y + t * (Speed.Y + t * Accelerarion.Y / 2000); c := dst(points[Link].x - u.X, points[Link].y - u.Y); if (not Cycle) and (c < Rope) then begin Position.X := Round(u.X); Position.Y := Round(u.Y); Speed.X := Round(Speed.X + t * Accelerarion.X / 1000); Speed.Y := Round(Speed.Y + t * Accelerarion.Y / 1000); end else begin Cycle := True; if c > Rope then begin Position.X := Round(u.X + (c - Rope) * (points[Link].x - Position.X) / Rope); Position.Y := Round(u.Y + (c - Rope) * (points[Link].y - Position.Y) / Rope); end; c := dst(Accelerarion.X / 1000, Accelerarion.Y / 1000); u.X := (c / Rope) * (points[Link].x - Position.X) + Accelerarion.X / 1000; u.Y := (c / Rope) * (points[Link].y - Position.Y) + Accelerarion.Y / 1000; inc(Position.X, Round(t * (Speed.X + t * u.X / 2))); inc(Position.Y, Round(t * (Speed.Y + t * u.Y / 2))); Speed.X := Round(Speed.X + t * u.X); Speed.Y := Round(Speed.Y + t * u.Y); z := Point(Round(Position.X + u.X - Accelerarion.X / 1000), Round(Position.Y + u.Y - Accelerarion.Y / 1000)); v := Point(Round(Position.X + Accelerarion.X / 1000), Round(Position.Y + Accelerarion.Y / 1000)); w := Point(Round(Position.X + u.X), Round(Position.Y + u.Y)); end; end else begin inc(Position.X, Round(t * (Speed.X + t * Accelerarion.X / 2000))); inc(Position.Y, Round(t * (Speed.Y + t * Accelerarion.Y / 2000))); Speed.X := Round(Speed.X + t * Accelerarion.X / 1000); Speed.Y := Round(Speed.Y + t * Accelerarion.Y / 1000); Cycle := False; end; if Position.X < 6 then begin Position.X := 6; Speed.X := 0; Accelerarion.X := 40000; if Position.X < ClientWidth div 2 then Accelerarion.Y := Round(Accelerarion.Y + 100000 * Random) else Accelerarion.Y := Round(Accelerarion.Y - 100000 * Random); end; if Position.Y < 6 then begin Position.Y := 6; Speed.Y := 0; Accelerarion.Y := 98000; if Position.Y < ClientHeight div 2 then Accelerarion.X := Round(Accelerarion.X + 25000 * Random) else Accelerarion.X := Round(Accelerarion.X - 25000 * Random); end; if Position.X > ClientWidth - 6 then begin Position.X := ClientWidth - 6; Speed.X := 0; Accelerarion.X := -43000; if Position.X < ClientWidth div 2 then Accelerarion.Y := Round(Accelerarion.Y + 100000 * Random) else Accelerarion.Y := Round(Accelerarion.Y - 100000 * Random); end; if Position.Y > ClientHeight - 6 then begin Position.Y := ClientHeight - 6; Speed.Y := 0; Accelerarion.Y := -98000; if Position.Y < ClientHeight div 2 then Accelerarion.X := Round(Accelerarion.X + 25000 * Random) else Accelerarion.X := Round(Accelerarion.X - 25000 * Random); end; end; with balance, balance.Position, Canvas, Canvas.Pen do begin if balance.Link <> -1 then begin MoveTo(X, Y); LineTo(points[balance.Link].x, points[balance.Link].y); end; Arc(X - 5, Y - 5, X + 5, Y + 5, X, Y - 5, X, Y - 5); if Cycle then begin s := Color; Width := 1; Style := psDashDotDot; Color := clGreen; MoveTo(z.x, z.y); LineTo(X, Y); Color := clBlue; LineTo(v.x, v.y); Width := 2; Style := psSolid; Color := clPurple; MoveTo(w.x, w.y); LineTo(X, Y); Width := 1; Color := s; end; end; end; procedure TForm1.Timer1Timer(Sender: TObject); begin Repaint; end; end. |
Сообщ.
#18
,
|
|
|
Только зачем скорость и ускорение сделали целочисленными? Так они будут работать не плавно. Хотя у ускорения и сохраняется 3 десятичных разрядов в дробной части (у Acceleration везде стоит деление на 1000), но у скорости этого нету.
Изначально у меня эти параметры тоже были целыми, но я изменил их на вещественные. Так стало работать более плавно. |
Сообщ.
#19
,
|
|
|
Цитата macomics @ Только зачем скорость и ускорение сделали целочисленными? Так они будут работать не плавно. ... else Accelerarion.X := Accelerarion.X - 25000 * Random; ... [Pascal Error] Unit1.pas(183): E2010 Incompatible types: 'Integer' and 'Extended' [Pascal Fatal Error] Project1.dpr(5): F2063 Could not compile used unit 'Unit1.pas' |
Сообщ.
#20
,
|
|
|
Так Acceleration как раз вещественного типа (Double) должно быть, а не Integer
Я же не написал inc(Acceleration.X, 10000 * Random); |
Сообщ.
#21
,
|
|
|
TPointLink = packed record Position: TPoint; Speed: TPoint; Accelerarion: TPoint; Link: LongInt; Rope: LongInt; Cycle: Boolean; end; type А Accelerarion: TPoint; Это А Tpoint в свою очередь -> Описание Кому лень ||||||||||||||||| VVVVVVVVVVVVVVVVVV Там по ссылке это описание ||||||||||||||||| VVVVVVVVVVVVVVVVVV TPoint ТипСодержит целочисленные значения X и YTypes unit type TPoint = packed record X: Longint; Y: Longint; end; Поэтому Incompatible types: 'Integer' and 'Extended' тогда вопрос как изменить -> "TPointLink = packed record" чтобы "TPoint;" был -> "Acceleration как раз вещественного типа (Double) должно быть" ? |
Сообщ.
#22
,
|
|
|
Я вам об этом и говорю, зачем вы вместо объявленной у меня структуры TPointFlt
TPointFlt = packed record X, Y: Double; end; |
Сообщ.
#23
,
|
|
|
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TPointFlt = packed record X, Y: Double; end; TPointAim = packed record Position: TPoint; Pressed: Boolean; end; TPointLink = packed record Position: TPoint; Speed: TPointFlt; Accelerarion: TPointFlt; Link: LongInt; Rope: LongInt; Cycle: Boolean; end; { TForm1 } TForm1 = class(TForm) Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; _X, _Y: Integer); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); procedure FormPaint(Sender: TObject); procedure Timer1Timer(Sender: TObject); private public end; var Form1: TForm1; balance: TPointLink = (Position: (X: 25; Y: 10); Speed: (X: 0; Y: 0); Accelerarion: (X: 0; Y: 98000); Link: -1; Rope: 0; Cycle: False); points: array [0 .. 3] of TPoint = ((x:80; y:100), (x:150; y:120), (x:200; y:80), (x:250; y:110)); curpos: TPointAim = (Position: (X: 0; Y: 0); Pressed: False); latest: TDateTime; implementation {$R *.dfm} { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject); begin Repaint; end; function dst(X, Y: LongInt): Double; overload; begin dst := sqrt(X * X + Y * Y); end; function dst(X, Y: Double): Double; overload; begin dst := sqrt(X * X + Y * Y); end; procedure TForm1.FormPaint(Sender: TObject); var i: LongInt; t: TDateTime; c: Double; u: TPointFlt; z, v, w: TPoint; s: TColor; begin t := latest; latest := Now; t := (latest - t) * 86400; for i := Low(points) to High(points) do begin Canvas.Arc(points[i].x - 10, points[i].y - 10, points[i].x + 10, points[i].y + 10, points[i].x, points[i].y - 10, points[i].x, points[i].y - 10); with balance, curpos.Position do if (curpos.Pressed) and (dst(X - points[i].x, Y - points[i].y) < sqrt(800)) then begin Canvas.Arc(points[i].x - 7, points[i].y - 7, points[i].x + 7, points[i].y + 7, points[i].x, points[i].y - 7, points[i].x, points[i].y - 7); if (Link = -1) or (Link <> i) then begin Link := i; Rope := Round(dst(Position.X - points[i].x, Position.Y - points[i].y)); end; end else if not curpos.Pressed then balance.Link := -1; end; with balance do begin if Link <> -1 then begin u.X := Position.X + t * (Speed.X + t * Accelerarion.X / 2000); u.Y := Position.Y + t * (Speed.Y + t * Accelerarion.Y / 2000); c := dst(points[Link].x - u.X, points[Link].y - u.Y); if (not Cycle) and (c < Rope) then begin Position.X := Round(u.X); Position.Y := Round(u.Y); Speed.X := Speed.X + t * Accelerarion.X / 1000; Speed.Y := Speed.Y + t * Accelerarion.Y / 1000; end else begin Cycle := True; if c > Rope then begin Position.X := Round(u.X + (c - Rope) * (points[Link].x - Position.X) / Rope); Position.Y := Round(u.Y + (c - Rope) * (points[Link].y - Position.Y) / Rope); end; c := dst(Accelerarion.X / 1000, Accelerarion.Y / 1000); u.X := (c / Rope) * (points[Link].x - Position.X) + Accelerarion.X / 1000; u.Y := (c / Rope) * (points[Link].y - Position.Y) + Accelerarion.Y / 1000; inc(Position.X, Round(t * (Speed.X + t * u.X / 2))); inc(Position.Y, Round(t * (Speed.Y + t * u.Y / 2))); Speed.X := Speed.X + t * u.X; Speed.Y := Speed.Y + t * u.Y; z := Point(Round(Position.X + u.X - Accelerarion.X / 1000), Round(Position.Y + u.Y - Accelerarion.Y / 1000)); v := Point(Round(Position.X + Accelerarion.X / 1000), Round(Position.Y + Accelerarion.Y / 1000)); w := Point(Round(Position.X + u.X), Round(Position.Y + u.Y)); end; end else begin inc(Position.X, Round(t * (Speed.X + t * Accelerarion.X / 2000))); inc(Position.Y, Round(t * (Speed.Y + t * Accelerarion.Y / 2000))); Speed.X := Speed.X + t * Accelerarion.X / 1000; Speed.Y := Speed.Y + t * Accelerarion.Y / 1000; Cycle := False; end; if Position.X < 6 then begin Position.X := 6; Speed.X := 0; Accelerarion.X := 40000; if Position.X < ClientWidth div 2 then Accelerarion.Y := Accelerarion.Y + 100000 * Random else Accelerarion.Y := Accelerarion.Y - 100000 * Random; end; if Position.Y < 6 then begin Position.Y := 6; Speed.Y := 0; Accelerarion.Y := 98000; if Position.Y < ClientHeight div 2 then Accelerarion.X := Accelerarion.X + 25000 * Random else Accelerarion.X := Accelerarion.X - 25000 * Random; end; if Position.X > ClientWidth - 6 then begin Position.X := ClientWidth - 6; Speed.X := 0; Accelerarion.X := -43000; if Position.X < ClientWidth div 2 then Accelerarion.Y := Accelerarion.Y + 100000 * Random else Accelerarion.Y := Accelerarion.Y - 100000 * Random; end; if Position.Y > ClientHeight - 6 then begin Position.Y := ClientHeight - 6; Speed.Y := 0; Accelerarion.Y := -98000; if Position.Y < ClientHeight div 2 then Accelerarion.X := Accelerarion.X + 25000 * Random else Accelerarion.X := Accelerarion.X - 25000 * Random; end; end; with balance, balance.Position, Canvas, Canvas.Pen do begin if balance.Link <> -1 then begin MoveTo(X, Y); LineTo(points[balance.Link].x, points[balance.Link].y); end; Arc(X - 5, Y - 5, X + 5, Y + 5, X, Y - 5, X, Y - 5); if Cycle then begin s := Color; Width := 1; Style := psDashDotDot; Color := clGreen; MoveTo(z.x, z.y); LineTo(X, Y); Color := clBlue; LineTo(v.x, v.y); Width := 2; Style := psSolid; Color := clPurple; MoveTo(w.x, w.y); LineTo(X, Y); Width := 1; Color := s; end; end; end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); begin with curpos.Position do begin X := _X; Y := _Y; curpos.Pressed := True; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Randomize; latest := Now; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; _X, _Y: Integer); begin with curpos.Position do if curpos.Pressed then begin X := _X; Y := _Y; end; end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; _X, _Y: Integer); begin curpos.Pressed := False; end; end. |