{"id":4624,"date":"2022-08-23T16:26:11","date_gmt":"2022-08-23T10:56:11","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/"},"modified":"2022-08-23T16:26:11","modified_gmt":"2022-08-23T10:56:11","slug":"solved-delphi-how-to-make-a-shape-stop-moving","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/","title":{"rendered":"[Solved] Delphi, How to make a shape stop moving"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-16134521\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"16134521\" data-parentid=\"16134093\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Try the following code (assign <code>OnCreate<\/code> and <code>OnPaint<\/code> of the form and set the timer to 30 millisecond intervals):<\/p>\n<pre><code>unit Unit5;\n\ninterface\n\nuses\n  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,\n  Dialogs, ExtCtrls;\n\ntype\n  TVector = record\n    X, Y: real;\n  end;\n\n  TForm5 = class(TForm)\n    Timer1: TTimer;\n    procedure FormPaint(Sender: TObject);\n    procedure FormCreate(Sender: TObject);\n    procedure Timer1Timer(Sender: TObject);\n  private\n    { Private declarations }\n    FPosA, FPosB: TVector;\n    v: TVector;\n  public\n    { Public declarations }\n  end;\n\nvar\n  Form5: TForm5;\n\nimplementation\n\nuses Math;\n\n{$R *.dfm}\n\nconst RADIUS = 16;\n\nfunction RealPoint(X, Y: real): TVector;\nbegin\n  result.X := X;\n  result.Y := Y;\nend;\n\nfunction RoundPoint(P: TVector): TPoint;\nbegin\n  result.X := round(P.X);\n  result.Y := round(P.Y);\nend;\n\nprocedure TForm5.FormCreate(Sender: TObject);\nvar\n  DX, DY: real;\nbegin\n  FPosA := RealPoint(32, 32);\n  FPosB := RealPoint(500, 200);\n\n  DX := FPosB.X - FPosA.X;\n  DY := FPosB.Y - FPosA.Y;\n\n  v.X := DX \/ 100;\n  v.Y := DY \/ 100;\nend;\n\nfunction EllipseRectFromPoint(P: TVector): TRect;\nvar\n  ScreenPoint: TPoint;\nbegin\n  ScreenPoint := RoundPoint(P);\n  result.Left := ScreenPoint.X - RADIUS;\n  result.Right := ScreenPoint.X + RADIUS;\n  result.Top := ScreenPoint.Y - RADIUS;\n  result.Bottom := ScreenPoint.Y + RADIUS;\nend;\n\nprocedure TForm5.FormPaint(Sender: TObject);\nbegin\n\n  \/\/ Draw ball A\n  Canvas.Brush.Color := clSkyBlue;\n  Canvas.Ellipse(EllipseRectFromPoint(FPosA));\n\n  \/\/ Draw ball B\n  Canvas.Brush.Color := clMoneyGreen;\n  Canvas.Ellipse(EllipseRectFromPoint(FPosB));\n\nend;\n\nprocedure TForm5.Timer1Timer(Sender: TObject);\nbegin\n  FPosA.X := FPosA.X + V.X;\n  FPosA.Y := FPosA.Y + V.Y;\n  Invalidate;\n\n  if Hypot(FPosA.X - FPosB.X, FPosA.Y - FPosB.Y) &lt; 0.1 then\n  begin\n    Timer1.Enabled := false;\n    ShowMessage('We''re there!');\n  end;\nend;\n\nend.\n<\/code><\/pre>\n<p>Two balls:<\/p>\n<pre><code>unit Unit5;\n\ninterface\n\nuses\n  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,\n  Dialogs, ExtCtrls;\n\ntype\n  TVector = record\n    X, Y: real;\n  end;\n\n  TForm5 = class(TForm)\n    Timer1: TTimer;\n    procedure FormPaint(Sender: TObject);\n    procedure FormCreate(Sender: TObject);\n    procedure Timer1Timer(Sender: TObject);\n  private\n    { Private declarations }\n    AreWeThereYetA, AreWeThereYetB: boolean;\n    FPosA, FPosB, FPosC: TVector;\n    vA, vB: TVector;\n  public\n    { Public declarations }\n  end;\n\nvar\n  Form5: TForm5;\n\nimplementation\n\nuses Math;\n\n{$R *.dfm}\n\nconst RADIUS = 16;\n\nfunction RealPoint(X, Y: real): TVector;\nbegin\n  result.X := X;\n  result.Y := Y;\nend;\n\nfunction RoundPoint(P: TVector): TPoint;\nbegin\n  result.X := round(P.X);\n  result.Y := round(P.Y);\nend;\n\nprocedure TForm5.FormCreate(Sender: TObject);\nvar\n  DX, DY: real;\nbegin\n  FPosA := RealPoint(32, 32);\n  FPosB := RealPoint(132, 32);\n  FPosC := RealPoint(500, 200);\n\n  DX := FPosC.X - FPosA.X;\n  DY := FPosC.Y - FPosA.Y;\n  vA.X := DX \/ 100;\n  vA.Y := DY \/ 100;\n\n  DX := FPosC.X - FPosB.X;\n  DY := FPosC.Y - FPosB.Y;\n  vB.X := DX \/ 200;\n  vB.Y := DY \/ 200;\n\nend;\n\nfunction EllipseRectFromPoint(P: TVector): TRect;\nvar\n  ScreenPoint: TPoint;\nbegin\n  ScreenPoint := RoundPoint(P);\n  result.Left := ScreenPoint.X - RADIUS;\n  result.Right := ScreenPoint.X + RADIUS;\n  result.Top := ScreenPoint.Y - RADIUS;\n  result.Bottom := ScreenPoint.Y + RADIUS;\nend;\n\nprocedure TForm5.FormPaint(Sender: TObject);\nbegin\n\n  \/\/ Draw ball A\n  Canvas.Brush.Color := clSkyBlue;\n  Canvas.Ellipse(EllipseRectFromPoint(FPosA));\n\n  \/\/ Draw ball B\n  Canvas.Brush.Color := clMoneyGreen;\n  Canvas.Ellipse(EllipseRectFromPoint(FPosB));\n\n  \/\/ Draw ball C\n  Canvas.Brush.Color := clRed;\n  Canvas.Ellipse(EllipseRectFromPoint(FPosC));\n\nend;\n\nprocedure TForm5.Timer1Timer(Sender: TObject);\nbegin\n\n  if not AreWeThereYetA then\n  begin\n    FPosA.X := FPosA.X + VA.X;\n    FPosA.Y := FPosA.Y + VA.Y;\n  end;\n\n  if not AreWeThereYetB then\n  begin\n    FPosB.X := FPosB.X + VB.X;\n    FPosB.Y := FPosB.Y + VB.Y;\n  end;\n\n  Invalidate;\n\n  if Hypot(FPosA.X - FPosC.X, FPosA.Y - FPosC.Y) &lt; 0.1 then\n    AreWeThereYetA := true;\n\n  if Hypot(FPosB.X - FPosC.X, FPosB.Y - FPosC.Y) &lt; 0.1 then\n    AreWeThereYetB := true;\n\n  if AreWeThereYetA and AreWeThereYetB then\n  begin\n    Timer1.Enabled := false;\n    ShowMessage('We are there!');\n  end;\nend;\n\nend.\n<\/code><\/pre>\n<p>Using arrays and records, it would be easily to generalise to <code>N<\/code> balls with custom properties (colours, radii, etc.), even random ones. It would also be very easy to implement bouncing. In addition, a real vector type would be good here.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Delphi, How to make a shape stop moving <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Try the following code (assign OnCreate and OnPaint of the form and set the timer to 30 millisecond intervals): unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TVector = record X, Y: real; end; TForm5 = class(TForm) Timer1: TTimer; procedure FormPaint(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: &#8230; <a title=\"[Solved] Delphi, How to make a shape stop moving\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\" aria-label=\"More on [Solved] Delphi, How to make a shape stop moving\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[740,977,978],"class_list":["post-4624","post","type-post","status-publish","format-standard","hentry","category-solved","tag-delphi","tag-move","tag-shape"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Delphi, How to make a shape stop moving - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Delphi, How to make a shape stop moving - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Try the following code (assign OnCreate and OnPaint of the form and set the timer to 30 millisecond intervals): unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TVector = record X, Y: real; end; TForm5 = class(TForm) Timer1: TTimer; procedure FormPaint(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-23T10:56:11+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Delphi, How to make a shape stop moving\",\"datePublished\":\"2022-08-23T10:56:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\"},\"wordCount\":78,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"delphi\",\"move\",\"shape\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\",\"name\":\"[Solved] Delphi, How to make a shape stop moving - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-23T10:56:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Delphi, How to make a shape stop moving\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Delphi, How to make a shape stop moving - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Delphi, How to make a shape stop moving - JassWeb","og_description":"[ad_1] Try the following code (assign OnCreate and OnPaint of the form and set the timer to 30 millisecond intervals): unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; type TVector = record X, Y: real; end; TForm5 = class(TForm) Timer1: TTimer; procedure FormPaint(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/","og_site_name":"JassWeb","article_published_time":"2022-08-23T10:56:11+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Delphi, How to make a shape stop moving","datePublished":"2022-08-23T10:56:11+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/"},"wordCount":78,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["delphi","move","shape"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/","url":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/","name":"[Solved] Delphi, How to make a shape stop moving - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-23T10:56:11+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-delphi-how-to-make-a-shape-stop-moving\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Delphi, How to make a shape stop moving"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=4624"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4624\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}