Forum > Beginners
double loop for a program with the pythagoras
bradyhartsfield:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure pythagoras; var d : real; var j,i,c,a,b : integer;begin for j := 1 to 20 do for i := 2 to 20 do begin d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i])); if d <= r[j]+r[i]+12 then begin switch switch end; end;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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- for i := 1 to 20 do begin r[j] := 20; mx[j] := random(image1.width); my[j] := random(image1.height); vx[j] := random(10); vy[j] := random(10); 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
--- End quote ---
and the j and the i is marked.
this is my switch process:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure switch (Var a,b:integer);var zwischenspeicher : integer; begin zwischenspeicher := a; a := b; b := zwischenspeicher; 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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Image1: TImage; Timer1: TTimer; Timer2: TTimer; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormShowHint(Sender: TObject; HintInfo: PHintInfo); procedure Image1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private public end; var Form1: TForm1; mx : array[1..20] of integer; my : array[1..20] of integer; vx : array[1..20] of integer; vy : array[1..20] of integer; r : array[1..20] of integer; zwischenspeicher1, zwischenspeicher2, c, c2,j,i,d : integer; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin timer1.enabled := true;end; procedure TForm1.Button2Click(Sender: TObject);begin timer1.enabled := false;end; procedure switch (Var a,b:integer);var zwischenspeicher : integer; begin zwischenspeicher := a; a := b; b := zwischenspeicher; end; procedure pythagoras; var d : real; var j,i,vx,vy,c,a,b : integer;begin for j := 1 to 20 do for i := 2 to 20 do begin d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i])); if d <= r[j]+r[i]+12 then begin switch switch end; end;end; procedure TForm1.FormCreate(Sender: TObject);begin form1.color:=clpurple; randomize; doublebuffered := true; timer1.enabled := false; for c := 1 to 20 do begin r[c] := 20; mx[c] := random(image1.width); my[c] := random(image1.height); vx[c] := random(10); vy[c] := random(10); end;end; procedure TForm1.FormShowHint(Sender: TObject; HintInfo: PHintInfo);begin end; procedure TForm1.Image1Click(Sender: TObject);begin end; procedure TForm1.Timer1Timer(Sender: TObject);begin with image1.canvas do begin for c := 1 to 20 do begin pen.color := clblack; brush.color:=clblack; ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]); mx[c]:=mx[c]+vx[c]; my[c]:=my[c]+vy[c]; pen.color := clpurple; brush.color := clpurple; ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]); end; end; for c:= 1 to 20 do begin if my[c]+r[c]>=image1.height then vy[c]:= -vy[c]; if mx[c]+r[c]>=image1.width then vx[c]:=-vx[c]; if my[c]-r[c]<0 then vy[c]:=-vy[c]; if mx[c]-r[c]<0 then vx[c]:=-vx[c]; end; end; end.
if you have any questions with the formulation of my question then feel free to ask!
regards
brady
Handoko:
I can't compile your code. I got this error:
molly:
--- Quote from: bradyhartsfield on January 11, 2018, 02:50:45 pm ---questions:
do i need two of those? so switch1 and switch2?
--- End quote ---
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:
hey,
--- Quote ---I can't compile your code. I got this error:
--- End quote ---
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Image1: TImage; Image2: TImage; Timer1: TTimer; Timer2: TTimer; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormShowHint(Sender: TObject; HintInfo: PHintInfo); procedure Image1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private public end; var Form1: TForm1; mx : array[1..20] of integer; my : array[1..20] of integer; vx : array[1..20] of integer; vy : array[1..20] of integer; r : array[1..20] of integer; zwischenspeicher1, zwischenspeicher2, c, c2,j,i,d : integer; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin timer1.enabled := true;end; procedure TForm1.Button2Click(Sender: TObject);begin timer1.enabled := false;end; procedure switch (Var a,b:integer);var zwischenspeicher : integer; begin zwischenspeicher := a; a := b; b := zwischenspeicher; end; procedure pythagoras; var d : real; var j,i, vx, vy : integer;begin for j := 1 to 20 do for i := 2 to 20 do begin d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i])); if d <= r[j]+r[i]+12 then begin switch(vx, vx); switch(vy, vy); end; end;end; procedure TForm1.FormCreate(Sender: TObject);begin form1.color:=clpurple; randomize; doublebuffered := true; timer1.enabled := false; for c := 1 to 20 do begin r[c] := 20; mx[c] := random(image1.width); my[c] := random(image1.height); vx[c] := random(10); vy[c] := random(10); end;end; procedure TForm1.FormShowHint(Sender: TObject; HintInfo: PHintInfo);begin end; procedure TForm1.Image1Click(Sender: TObject);begin end; procedure TForm1.Timer1Timer(Sender: TObject);begin with image1.canvas do begin for c := 1 to 20 do begin pen.color := clblack; brush.color:=clblack; ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]); mx[c]:=mx[c]+vx[c]; my[c]:=my[c]+vy[c]; pen.color := clpurple; brush.color := clpurple; ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]); end; end; for c:= 1 to 20 do begin if my[c]+r[c]>=image1.height then vy[c]:= -vy[c]; if mx[c]+r[c]>=image1.width then vx[c]:=-vx[c]; if my[c]-r[c]<0 then vy[c]:=-vy[c]; if mx[c]-r[c]<0 then vx[c]:=-vx[c]; end; end; end.
regards
brady
bradyhartsfield:
hey,
--- Quote from: molly on January 11, 2018, 03:10:03 pm ---
--- Quote from: bradyhartsfield on January 11, 2018, 02:50:45 pm ---questions:
do i need two of those? so switch1 and switch2?
--- End quote ---
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.
--- End quote ---
okay thanks.
regards
brady
Navigation
[0] Message Index
[#] Next page