* * *

Author Topic: double loop for a program with the pythagoras  (Read 4317 times)

bradyhartsfield

  • New member
  • *
  • Posts: 41
double loop for a program with the pythagoras
« on: January 11, 2018, 02:50:45 pm »
hey,
 
i am back with my well known problem! so this is how far i am right now: i created my own processes to make it more structured. one is called "switch" and one is called "pythagoras". in the "switch" process i want the two circles to exchange velocities so it looks like they are bouncing off of each other.

i created a double loop for so that no circle is left out, but it isnt working.. my program is starting without any errors but the circle wont bounce off of eachother.

Code: Pascal  [Select]
  1. procedure pythagoras;
  2.   var d : real;
  3.   var j,i,c,a,b : integer;
  4. begin
  5.   for j := 1 to 20 do
  6.     for i := 2 to 20 do begin
  7.     d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));
  8.     if d <= r[j]+r[i]+12 then begin
  9.     switch
  10.     switch
  11.   end;
  12.  end;
  13. end;
  sooo, this is currently my "pythagoras" process. after "switch" i need to add something i will come back to that later. soo that code i wrote up there i got from my teacher, but i already modified it, for example i added the "switch" process and changed the letters "j" and "i".
my questions now:
do i need to define vx, vy, mx, my and r new like this :
Code: Pascal  [Select]
  1.   for i :=  1 to 20 do begin
  2.     r[j] := 20;
  3.     mx[j] := random(image1.width);
  4.     my[j] := random(image1.height);
  5.     vx[j] := random(10);
  6.     vy[j] := random(10);
  7.   end;  
and the same with i?
why is there at the part with the two "r`s" summed up a "+12" at the end?
btw: after the two "switch" i want to add (vx[j], vx) and (vy[j], vx) but when i do that there comes the error
Quote
Illegal qualifier
and the j and the i is marked.

this is my switch process:
Code: Pascal  [Select]
  1. procedure switch (Var a,b:integer);
  2. var zwischenspeicher : integer;
  3.   begin
  4.     zwischenspeicher := a;
  5.     a := b;
  6.     b := zwischenspeicher;
  7.   end;  

questions:
do i need two of those? so switch1 and switch2?


summed up:
I need to create 20 ellipses, spawning randomly, bouncing off the wall and moving around [DONE]
I need the ellipses to bounce off of each other when they touch [not done]

this is my whole code atm:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Image1: TImage;
  19.     Timer1: TTimer;
  20.     Timer2: TTimer;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure FormShowHint(Sender: TObject; HintInfo: PHintInfo);
  25.     procedure Image1Click(Sender: TObject);
  26.     procedure Timer1Timer(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.   mx : array[1..20] of integer;
  36.   my : array[1..20] of integer;
  37.   vx : array[1..20] of integer;
  38.   vy : array[1..20] of integer;
  39.   r  : array[1..20] of integer;
  40.   zwischenspeicher1, zwischenspeicher2, c, c2,j,i,d : integer;
  41.  
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. { TForm1 }
  48.  
  49. procedure TForm1.Button1Click(Sender: TObject);
  50. begin
  51.   timer1.enabled := true;
  52. end;
  53.  
  54. procedure TForm1.Button2Click(Sender: TObject);
  55. begin
  56.   timer1.enabled := false;
  57. end;
  58.  
  59. procedure switch (Var a,b:integer);
  60. var zwischenspeicher : integer;
  61.   begin
  62.     zwischenspeicher := a;
  63.     a := b;
  64.     b := zwischenspeicher;
  65.   end;
  66.  
  67.  
  68. procedure pythagoras;
  69.   var d : real;
  70.   var j,i,vx,vy,c,a,b : integer;
  71. begin
  72.   for j := 1 to 20 do
  73.     for i := 2 to 20 do begin
  74.     d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));
  75.     if d <= r[j]+r[i]+12 then begin
  76.     switch
  77.     switch
  78.   end;
  79.  end;
  80. end;
  81.  
  82. procedure TForm1.FormCreate(Sender: TObject);
  83. begin
  84.   form1.color:=clpurple;
  85.   randomize;
  86.   doublebuffered := true;
  87.   timer1.enabled := false;
  88.   for c :=  1 to 20 do begin
  89.     r[c] := 20;
  90.     mx[c] := random(image1.width);
  91.     my[c] := random(image1.height);
  92.     vx[c] := random(10);
  93.     vy[c] := random(10);
  94.   end;
  95. end;
  96.  
  97. procedure TForm1.FormShowHint(Sender: TObject; HintInfo: PHintInfo);
  98. begin
  99.  
  100. end;
  101.  
  102. procedure TForm1.Image1Click(Sender: TObject);
  103. begin
  104.  
  105. end;
  106.  
  107. procedure TForm1.Timer1Timer(Sender: TObject);
  108. begin
  109.   with image1.canvas do begin
  110.     for c := 1 to 20 do begin
  111.       pen.color := clblack;
  112.       brush.color:=clblack;
  113.       ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
  114.       mx[c]:=mx[c]+vx[c];
  115.       my[c]:=my[c]+vy[c];
  116.       pen.color := clpurple;
  117.       brush.color := clpurple;
  118.       ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
  119.     end;
  120.   end;
  121.  
  122.  
  123.  
  124.   for c:= 1 to 20 do begin
  125.     if my[c]+r[c]>=image1.height
  126.     then vy[c]:= -vy[c];
  127.     if mx[c]+r[c]>=image1.width
  128.     then vx[c]:=-vx[c];
  129.     if my[c]-r[c]<0
  130.     then vy[c]:=-vy[c];
  131.     if mx[c]-r[c]<0
  132.     then vx[c]:=-vx[c];
  133.   end;
  134.  
  135.  
  136.  
  137.  
  138.  end;
  139.  
  140.  
  141.  
  142.  
  143. end.
  144.  

if you have any questions with the formulation of my question then feel free to ask!

regards
brady

Handoko

  • Hero Member
  • *****
  • Posts: 2388
  • My goal: build my own game engine using Lazarus
Re: double loop for a program with the pythagoras
« Reply #1 on: January 11, 2018, 02:57:05 pm »
I can't compile your code. I got this error:

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: double loop for a program with the pythagoras
« Reply #2 on: January 11, 2018, 03:10:03 pm »
questions:
do i need two of those? so switch1 and switch2?
You need to 'switch' (better worded swap) the movement of the two circles. No idea what you would like to do in a hypothetical second switch.

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: double loop for a program with the pythagoras
« Reply #3 on: January 11, 2018, 03:11:40 pm »
hey,

Quote
I can't compile your code. I got this error:
that is because (x, x) and (y, y) is missing behind the both "switch".

here is the code u can execute but it isnt doing what i want:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Image1: TImage;
  19.     Image2: TImage;
  20.     Timer1: TTimer;
  21.     Timer2: TTimer;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure FormShowHint(Sender: TObject; HintInfo: PHintInfo);
  26.     procedure Image1Click(Sender: TObject);
  27.     procedure Timer1Timer(Sender: TObject);
  28.   private
  29.  
  30.   public
  31.  
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.   mx : array[1..20] of integer;
  37.   my : array[1..20] of integer;
  38.   vx : array[1..20] of integer;
  39.   vy : array[1..20] of integer;
  40.   r  : array[1..20] of integer;
  41.   zwischenspeicher1, zwischenspeicher2, c, c2,j,i,d : integer;
  42.  
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm1 }
  49.  
  50. procedure TForm1.Button1Click(Sender: TObject);
  51. begin
  52.   timer1.enabled := true;
  53. end;
  54.  
  55. procedure TForm1.Button2Click(Sender: TObject);
  56. begin
  57.   timer1.enabled := false;
  58. end;
  59.  
  60. procedure switch (Var a,b:integer);
  61. var zwischenspeicher : integer;
  62.   begin
  63.     zwischenspeicher := a;
  64.     a := b;
  65.     b := zwischenspeicher;
  66.   end;
  67.  
  68.  
  69. procedure pythagoras;
  70.   var d : real;
  71.   var j,i, vx, vy : integer;
  72. begin
  73.   for j := 1 to 20 do
  74.     for i := 2 to 20 do begin
  75.     d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));
  76.     if d <= r[j]+r[i]+12 then begin
  77.     switch(vx, vx);
  78.     switch(vy, vy);
  79.   end;
  80.  end;
  81. end;
  82.  
  83. procedure TForm1.FormCreate(Sender: TObject);
  84. begin
  85.  
  86.   form1.color:=clpurple;
  87.   randomize;
  88.   doublebuffered := true;
  89.   timer1.enabled := false;
  90.   for c :=  1 to 20 do begin
  91.     r[c] := 20;
  92.     mx[c] := random(image1.width);
  93.     my[c] := random(image1.height);
  94.     vx[c] := random(10);
  95.     vy[c] := random(10);
  96.   end;
  97. end;
  98.  
  99. procedure TForm1.FormShowHint(Sender: TObject; HintInfo: PHintInfo);
  100. begin
  101.  
  102. end;
  103.  
  104. procedure TForm1.Image1Click(Sender: TObject);
  105. begin
  106.  
  107. end;
  108.  
  109. procedure TForm1.Timer1Timer(Sender: TObject);
  110. begin
  111.   with image1.canvas do begin
  112.     for c := 1 to 20 do begin
  113.       pen.color := clblack;
  114.       brush.color:=clblack;
  115.       ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
  116.       mx[c]:=mx[c]+vx[c];
  117.       my[c]:=my[c]+vy[c];
  118.       pen.color := clpurple;
  119.       brush.color := clpurple;
  120.       ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
  121.     end;
  122.   end;
  123.  
  124.  
  125.  
  126.   for c:= 1 to 20 do begin
  127.     if my[c]+r[c]>=image1.height
  128.     then vy[c]:= -vy[c];
  129.     if mx[c]+r[c]>=image1.width
  130.     then vx[c]:=-vx[c];
  131.     if my[c]-r[c]<0
  132.     then vy[c]:=-vy[c];
  133.     if mx[c]-r[c]<0
  134.     then vx[c]:=-vx[c];
  135.   end;
  136.  
  137.  
  138.  
  139.  
  140.  end;
  141.  
  142.  
  143.  
  144.  
  145. end.
  146.  

regards
brady


bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: double loop for a program with the pythagoras
« Reply #4 on: January 11, 2018, 03:12:28 pm »
hey,

questions:
do i need two of those? so switch1 and switch2?
You need to 'switch' (better worded swap) the movement of the two circles. No idea what you would like to do in a hypothetical second switch.

okay thanks.

regards
brady


molly

  • Hero Member
  • *****
  • Posts: 2345
Re: double loop for a program with the pythagoras
« Reply #5 on: January 11, 2018, 03:28:28 pm »
@bradyhartsfield:
I can this see going the wrong way because of interpretation difference.

Using two "switches" as your last code showed, does justify calling switch twice. However, you need to swap the movements inside your arrays.

Handoko

  • Hero Member
  • *****
  • Posts: 2388
  • My goal: build my own game engine using Lazarus
Re: double loop for a program with the pythagoras
« Reply #6 on: January 11, 2018, 03:55:51 pm »
I found many bugs in the code. Give me some time, I will write a detail bug fixes and suggestions.

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: double loop for a program with the pythagoras
« Reply #7 on: January 11, 2018, 03:59:45 pm »
I found many bugs in the code. Give me some time, I will write a detail bug fixes and suggestions.

okay thank you!

regards
brady

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: double loop for a program with the pythagoras
« Reply #8 on: January 11, 2018, 04:30:30 pm »
why is there at the part with the two "r`s" summed up a "+12" at the end?
That becomes clear when you remove the addition  ;)

A word of warning though. The whole idea of this assignment (besides applying the math and turning that into code) is that when you start out with implementing this (as something that appears easy to implement) that it becomes obvious that things are not quite work out as intended. There are minor flaws in the design/approach from the start. That is what the assignment is trying to teach you. You would need to apply more and better math to get rid of those flaws, and you can make that as complicated as you wish. The more accuracy you want the more code you would have to add and/or does make you realize that taking another approach might perhaps be better suited.

The latter is perhaps something that your teacher is going to address in class.

Handoko

  • Hero Member
  • *****
  • Posts: 2388
  • My goal: build my own game engine using Lazarus
Re: double loop for a program with the pythagoras
« Reply #9 on: January 11, 2018, 04:33:04 pm »
why is there at the part with the two "r`s" summed up a "+12" at the end?
That becomes clear when you remove the addition  ;)

Not enough, the code still buggy.

As usually I will write a detail step-by-step explanation.
Be patient.  :)

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: double loop for a program with the pythagoras
« Reply #10 on: January 11, 2018, 04:37:43 pm »
Not enough, the code still buggy.
Yes, i know  :). but TS is starting to get on the right track (seeing from where it all started). That TS is allowed to use events in a pascal program but not records... a choice/restriction i will never understand though as it makes things much easier to read/understand.

Quote
As usually I will write a detail step-by-step explanation.
Yes, thank you for that.

Quote
Be patient.  :)
I will. If not i would already have posted my (not 100% accurate and using record) code on the third or fourth post when TS started this topic in the other thread  :)
« Last Edit: January 11, 2018, 04:42:57 pm by molly »

taazz

  • Hero Member
  • *****
  • Posts: 5153
Re: double loop for a program with the pythagoras
« Reply #11 on: January 11, 2018, 04:44:17 pm »
Code: Pascal  [Select]
  1. procedure pythagoras;
  2.   var d : real;
  3.   var j,i, vx, vy : integer;
  4. begin
  5.   for j := 1 to 20 do
  6.     for i := 2 to 20 do begin
  7.     d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));
  8.     if d <= r[j]+r[i]+12 then begin
  9.     vx[j] := -vx[j];
  10.     vy[j] := -vy[j];
  11.   end;
  12.  end;
  13. end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: double loop for a program with the pythagoras
« Reply #12 on: January 11, 2018, 05:02:12 pm »
Code: Pascal  [Select]
  1.     vx[j] := -vx[j];
  2.     vy[j] := -vy[j];
  3.  
Very interesting attempt on elastic collision....

taazz

  • Hero Member
  • *****
  • Posts: 5153
Re: double loop for a program with the pythagoras
« Reply #13 on: January 11, 2018, 05:06:20 pm »
Code: Pascal  [Select]
  1.     vx[j] := -vx[j];
  2.     vy[j] := -vy[j];
  3.  
Very interesting attempt on elastic collision....
Not exactly correct I know but it shows the problem with the current approach.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: double loop for a program with the pythagoras
« Reply #14 on: January 11, 2018, 05:15:18 pm »
Not exactly correct I know but it shows the problem with the current approach.
Fair enough  :)

Indeed it shows what's going wrong. I have hard time grasping how it is possible not to note the difference.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus