Recent

Author Topic: For To STEP 10  (Read 1825 times)

coradi

  • Full Member
  • ***
  • Posts: 167
For To STEP 10
« on: October 04, 2023, 06:35:53 pm »
Hi,
How can I draw a Bar with segments?

I want draw a BAr with segemnts with a space from about 10pixel each
Is there any way, how I can do that=
Because there is no STEP command in my Pascal (Mikroe Pascal for ARM Controller)
Code: [Select]
x1:=100; y1:=10; x2:=150; y2:=20;

TFT_Rectangle(x1, y1, x2, y2);

For y1:=10 TO 200 DO
Begin
TFT_Rectangle(x1, (y1+20), x2, (y2+20));
end;
[code]
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

coradi

  • Full Member
  • ***
  • Posts: 167
Re: For To STEP 10
« Reply #1 on: October 04, 2023, 06:41:11 pm »
IS there a nicer way rhan
Code: [Select]
x1:=100; y1:=10; x2:=150; y2:=20;

TFT_Rectangle(x1, y1, x2, y2);

For nt:=10 TO 200 DO
Begin
y1:=y1+20;
y2:=y2+20;
TFT_Rectangle(x1, (y1), x2, (y2));
delay_ms(100);
end;
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: For To STEP 10
« Reply #2 on: October 05, 2023, 10:22:24 am »
Aircode to simulate a Step in a loop
Code: Pascal  [Select][+][-]
  1. nt:=10;
  2. Repeat
  3. //Do something
  4. Inc(nt,10);
  5. Until nt>200;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 6655
Re: For To STEP 10
« Reply #3 on: October 05, 2023, 10:45:39 am »
Or something like this:

Code: Pascal  [Select][+][-]
  1.   x1 := 100; y1 := 10;
  2.   x2 := 150; y2 := 20;
  3.   TFT_Rectangle(x1, y1, x2, y2);
  4.   for nt := 1 to 10 do
  5.   begin
  6.     TFT_Rectangle(x1, y1 + nt * 20, x2, y2 + nt * 20);
  7.     delay_ms(100);
  8.   end;

Zvoni

  • Hero Member
  • *****
  • Posts: 2818
Re: For To STEP 10
« Reply #4 on: October 05, 2023, 10:49:09 am »
Or something like this:

Code: Pascal  [Select][+][-]
  1.   x1 := 100; y1 := 10;
  2.   x2 := 150; y2 := 20;
  3.   TFT_Rectangle(x1, y1, x2, y2);
  4.   for nt := 1 to 10 do
  5.   begin
  6.     TFT_Rectangle(x1, y1 + nt * 20, x2, y2 + nt * 20);
  7.     delay_ms(100);
  8.   end;
Remove Line 3, and let the loop run from 0 to 10
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Warfley

  • Hero Member
  • *****
  • Posts: 1863
Re: For To STEP 10
« Reply #5 on: October 05, 2023, 08:05:01 pm »
If you are using step loops often, you may consider creating an enumerator type:
Code: Pascal  [Select][+][-]
  1. {$ModeSwitch AdvancedRecords}
  2.  
  3. interface
  4. [...]
  5.  
  6. type
  7.   TStepIterator = record
  8.     Start, Stop, Step: SizeInt;
  9.     property Current: SizeInt read Start;
  10.     function MoveNext: boolean; inline;
  11.     function GetEnumerator: TStepIterator; inline;
  12.   end;
  13.  
  14. [...]
  15.  
  16. function Steps(AStart, AStop, AStep: SizeInt): TStepIterator; inline;
  17. implementation
  18.  
  19.   { TStepIterator }
  20.  
  21.   function TStepIterator.MoveNext: boolean;
  22.   begin
  23.     Inc(Start, Step);
  24.     Result := Start <= Stop;
  25.   end;
  26.  
  27.   function TStepIterator.GetEnumerator: TStepIterator;
  28.   begin
  29.     Result := Self;
  30.   end;
  31.  
  32.   function Steps(AStart, AStop, AStep: SizeInt): TStepIterator;
  33.   begin
  34.     Result.Start := AStart - AStep;
  35.     Result.Step := AStep;
  36.     Result.Stop:= AStop;
  37.   end;
  38.  
Usage then
Code: Pascal  [Select][+][-]
  1. var
  2.   i: SizeInt;
  3. begin
  4.   for i in Steps(0, 100, 10) do // From 0 to 100 in steps of 10
  5.     WriteLn(i);
  6. end.
« Last Edit: October 05, 2023, 08:07:16 pm by Warfley »

TRon

  • Hero Member
  • *****
  • Posts: 3923
Re: For To STEP 10
« Reply #6 on: October 06, 2023, 01:48:34 am »
@Warfley:
Cool. I didn't know you could do /that/.

for TS in case wanting to use Warfley's solution (as it might perhaps not be that obvious):
Code: Pascal  [Select][+][-]
  1. procedure do_warfley;
  2. const
  3.   step = 20;
  4. var
  5.   x1 : integer = 100;
  6.   y1 : integer =  10;
  7.   x2 : integer = 150;
  8.   y2 : integer;
  9. begin
  10.   for y1 in steps(10, 200, step) do
  11.   begin
  12.     y2:=y1+10;
  13.     TFT_Rectangle(x1, y1, x2, y2);
  14.   end;
  15. end;
  16.  
Ofc the y2 assignment can be omitted by directly passing the calculation (y1+10) to TFT_Rectangle.


Seems like the obvious one is (still) missing (though Zvoni hinted at it)

Code: Pascal  [Select][+][-]
  1. procedure do_while;
  2. const
  3.   step = 20;
  4. var
  5.   x1 : integer = 100;
  6.   y1 : integer =  10;
  7.   x2 : integer = 150;
  8.   y2 : integer;
  9. begin
  10.   while y1 <= 200 do
  11.   begin
  12.     y2:=y1+10;
  13.     TFT_Rectangle(x1, y1, x2, y2);
  14.     inc(y1, step);
  15.   end;
  16. end;
  17.  
Also here the y2 assignment can be omitted.
I do not have to remember anything anymore thanks to total-recall.

Warfley

  • Hero Member
  • *****
  • Posts: 1863
Re: For To STEP 10
« Reply #7 on: October 06, 2023, 10:13:14 am »
@Warfley:
Cool. I didn't know you could do /that/.
Yes Enumerators/Iterators are very underutilized in Pascal. Whats also interesting for this, for-in does autmatic memory management, so when your iterator is a class, it will be automatically be freed. This allows things like the following:
Code: Pascal  [Select][+][-]
  1. type
  2.   TFindFilesEnumerator = class
  3.   private
  4.     FirstMove: Boolean;
  5.     SearchRec: TSearchRec;
  6.     Path: String;
  7.   public
  8.     constructor Create(Directory: String; Pattern: String);
  9.     destructor Destroy; override;
  10.  
  11.     function MoveNext: Boolean;
  12.     function GetEnumerator: TFindFilesEnumerator;
  13.  
  14.     property Current: String read SearchRec.Name;
  15.   end;
  16.  
  17. constructor TFindFilesEnumerator.Create(Directory: String; Pattern: String);
  18. begin
  19.   Path:=IncludeTrailingPathDelimiter(Path)+Pattern;
  20.   FirstMove:=True;
  21. end;
  22.  
  23. destructor TFindFilesEnumerator.Destroy;
  24. begin
  25.   FindClose(SearchRec);
  26.   inherited Destroy;
  27. end;
  28.  
  29. function TFindFilesEnumerator.MoveNext: Boolean;
  30. begin
  31.   if FirstMove then
  32.   begin
  33.     FirstMove:=False;
  34.     Exit(FindFirst(Path, faAnyFile, SearchRec)=0);
  35.   end;
  36.   Result:=FindNext(SearchRec)=0;
  37. end;
  38.  
  39. function TFindFilesEnumerator.GetEnumerator: TFindFilesEnumerator;
  40. begin
  41.   Result:=Self;
  42. end;
  43.  
  44. var
  45.   s: String;
  46. begin
  47.   for s in TFindFilesEnumerator.Create('.','*.*') do
  48.     WriteLn(s);
  49. end.

Especially a lot of such "base" iterators are missing in the RTL/FCL, so you always have to write them yourselves

dseligo

  • Hero Member
  • *****
  • Posts: 1458
Re: For To STEP 10
« Reply #8 on: October 06, 2023, 10:21:15 am »
Yes Enumerators/Iterators are very underutilized in Pascal. Whats also interesting for this, for-in does autmatic memory management, so when your iterator is a class, it will be automatically be freed. This allows things like the following:

Line 19 should probably be:
Code: Pascal  [Select][+][-]
  1.   Path:=IncludeTrailingPathDelimiter(Directory)+Pattern;

Warfley

  • Hero Member
  • *****
  • Posts: 1863
Re: For To STEP 10
« Reply #9 on: October 06, 2023, 10:30:48 am »
Yes

 

TinyPortal © 2005-2018