Author Topic: Why am I getting this result? [question about wrong calculations]  (Read 2722 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
For some strange reason, that I can not see, I am getting a result that shouldn't be possible.
my little terrain generator should start the left side at 225 +\- 25. And there fore should not start higher than 200, but if you look at the screen cap below that left side is a heck of a lot higher than 200px. 
Can anyone see what I am missing as to why it would do this? there is not a lot of code, probably more comment than actual code.

Code: QB64: [Select]
  1. CONST TRUE = -1, FALSE = NOT TRUE
  2.  
  3.  CLS
  4.  DrawTerrain 225
  5.  _DELAY .7
  6.  
  7. SUB DrawTerrain (BaseHeight%)
  8.  StartY% = BaseHeight% + INT(RND * 50) - 25
  9.  CurrentHeight% = StartY% 'keep track of the current Y of the land
  10.  PSET (0, StartY%), 2
  11.  DO
  12.   RunLength% = INT(RND * 50) + 25 'how far does this lay run?
  13.   HeightChange% = INT(RND * 100) - 50 'where the elevation go?
  14.   CurrentHeight% = CurrentHeight% + HeightChange% 'monitor the change in the land's Y
  15.   'if the land goes to low then reverse the height change+25
  16.   IF CurrentHeight% > 320 THEN CurrentHeight% = CurrentHeight% - (HeightChange% * 2) - 25: HeightChange% = -(HeightChange% + 25)
  17.   'now check that the land does not go to high
  18.   IF CurrentHeight% < 50 THEN CurrentHeight% = CurrentHeight% + ABS(HeightChange% * 2) + 25: HeightChange% = ABS(HeightChange% + 25)
  19.  
  20.   LINE -STEP(RunLength% - 1, HeightChange%), 2 'run this section
  21.   totalrun% = totalrun% + RunLength% 'track how far along it is
  22.   IF totalrun% > 645 THEN ScreenEnd%% = TRUE 'terrain is now done.
  23.  LOOP UNTIL ScreenEnd%%
  24.  PAINT (320, 340), 2, 2 'ahh lovely green grass land!
  25.  IF POINT(1, 1) = 2 THEN CLS: DrawTerrain (basehieght%) 'whole screen is green so try again(why though?)
  26.  
« Last Edit: August 21, 2021, 06:30:14 pm by odin »
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result?
« Reply #1 on: August 21, 2021, 03:55:59 pm »
Typo on line 28

What is leaking that causes the flood of green is the real question.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Why am I getting this result?
« Reply #2 on: August 21, 2021, 04:13:59 pm »
Typo on line 28

What is leaking that causes the flood of green is the real question.

Ahh! another case of staring at the code for too long. Thanks for the second set of eyes.

But yeah, not sure on the flood either, the IF\THEN that makes sure the ground doesn't go too low should prevent the PAINT from happening on the wrong side of the line. and it starts at 0 and goes to atleast 645, which should be off screen. so how the paint floods the screen is mystifying!
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result?
« Reply #3 on: August 21, 2021, 04:24:51 pm »
Here's one that wont flood:
Code: [Select]
Const TRUE = -1, FALSE = Not TRUE
Screen _NewImage(640, 350, 9): Cls: Randomize Timer

Do
    Cls
    DrawTerrain 225, 10
    Sleep 5
Loop Until InKey$ = Chr$(27)


Sub DrawTerrain (h, modN) ' modN for ruggedness the higher the less smooth
    For x = 0 To _Width
        If x Mod modN = 0 Then ' adjust mod number for ruggedness the higher the number the more jagged
            If h < 350 - modN And h > 50 + modN Then
                dy = Rnd * 20 - 10
            ElseIf h >= 350 - modN Then
                dy = Rnd * -10
            ElseIf h <= 50 + modN Then
                dy = Rnd * 10
            End If
        End If
        h = h + .1 * dy
        Line (x, _Height)-(x, h), 2
    Next
End Sub


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result?
« Reply #4 on: August 21, 2021, 04:32:52 pm »
Code: QB64: [Select]
  1. Screen _NewImage(640, 350, 32)
  2. Color , _RGB32(200, 200, 250)
  3.     Cls
  4.     DrawTerrain 100, 50, &HFF336688
  5.     DrawTerrain 150, 25, &HFF448888
  6.     DrawTerrain 200, 12, &HFF55AA88
  7.     DrawTerrain 250, 6, &HFF66CC88
  8.     DrawTerrain 300, 3, &HFF77EE88
  9.     Sleep 5
  10.  
  11. Sub DrawTerrain (h, modN, c As _Unsigned Long) ' modN for ruggedness the higher the less smooth
  12.     For x = 0 To _Width
  13.         If x Mod modN = 0 Then ' adjust mod number for ruggedness the higher the number the more jagged
  14.             If h < 350 - modN And h > 50 + modN Then
  15.                 dy = Rnd * 20 - 10
  16.             ElseIf h >= 350 - modN Then
  17.                 dy = Rnd * -10
  18.             ElseIf h <= 50 + modN Then
  19.                 dy = Rnd * 10
  20.             End If
  21.         End If
  22.         h = h + .1 * dy
  23.         Line (x, _Height)-(x, h), c
  24.     Next
  25.  
  26.  
  27.  
  28.  
« Last Edit: August 21, 2021, 04:35:54 pm by bplus »

FellippeHeitor

  • Guest
Re: Why am I getting this result? [question about wrong calculations]
« Reply #5 on: August 21, 2021, 06:30:51 pm »
Option _Explicit. Time saver.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Why am I getting this result? [question about wrong calculations]
« Reply #6 on: August 21, 2021, 07:17:14 pm »
Option _Explicit is your friend
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #7 on: August 21, 2021, 08:29:18 pm »
Back to why the paint was flooding, it was because the line wasn't always getting drawn all the way to the edge of the screen:

You can see it here
Code: QB64: [Select]
  1. Const TRUE = -1, FALSE = Not TRUE
  2. 'check something
  3. 'Print _Width, _Height
  4. 'End
  5.  
  6.     Cls
  7.     DrawTerrain 225
  8.     Sleep 5
  9.  
  10. Sub DrawTerrain (BaseHeight%)
  11.     StartY% = BaseHeight% + Int(Rnd * 50) - 25
  12.     CurrentHeight% = StartY% 'keep track of the current Y of the land
  13.     PSet (0, StartY%), 2
  14.     Do
  15.         RunLength% = Int(Rnd * 50) + 25 'how far does this lay run?
  16.         HeightChange% = Int(Rnd * 100) - 50 'where the elevation go?
  17.         CurrentHeight% = CurrentHeight% + HeightChange% 'monitor the change in the land's Y
  18.         'if the land goes to low then reverse the height change+25
  19.         If CurrentHeight% > 320 Then CurrentHeight% = CurrentHeight% - (HeightChange% * 2) - 25: HeightChange% = -(HeightChange% + 25)
  20.         'now check that the land does not go to high
  21.         If CurrentHeight% < 50 Then CurrentHeight% = CurrentHeight% + Abs(HeightChange% * 2) + 25: HeightChange% = Abs(HeightChange% + 25)
  22.  
  23.         Line -Step(RunLength% - 1, HeightChange%), 2 'run this section
  24.         totalrun% = totalrun% + RunLength% 'track how far along it is
  25.         If totalrun% > 645 Then ScreenEnd%% = TRUE 'terrain is now done.
  26.     Loop Until ScreenEnd%%
  27.  
  28.     'box in the paint area, nope that wasn't it
  29.     Line (0, StartY%)-(0, _Height), 2
  30.     Line (0, _Height)-(_Width, _Height), 2
  31.     Line (_Width, 0)-(_Width, _Height), 2
  32.  
  33.  
  34.     Sleep 5 'but look before you paint
  35.  
  36.     Paint (320, 340), 2, 2 'ahh lovely green grass land!
  37.     'screw this
  38.     '''''''''If Point(1, 1) = 2 Then Cls: DrawTerrain (basehieght%) 'whole screen is green so try again(why though?)
  39.  
  40.  
  41.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #8 on: August 21, 2021, 08:42:38 pm »
It was coming up short because of line:

Code: QB64: [Select]
  1.         LINE -STEP(RunLength% - 1, HeightChange%), 2 'run this section

the -1 was making it too short.

Code: QB64: [Select]
  1. Const TRUE = -1, FALSE = Not TRUE
  2. 'check something
  3. 'Print _Width, _Height
  4. 'End
  5.  
  6.     Cls
  7.     DrawTerrain 225
  8.     _Limit 2
  9.  
  10. Sub DrawTerrain (BaseHeight%)
  11.     StartY% = BaseHeight% + Int(Rnd * 50) - 25
  12.     CurrentHeight% = StartY% 'keep track of the current Y of the land
  13.     PSet (0, StartY%), 2
  14.     Do
  15.         RunLength% = Int(Rnd * 50) + 25 'how far does this lay run?
  16.         HeightChange% = Int(Rnd * 100) - 50 'where the elevation go?
  17.         CurrentHeight% = CurrentHeight% + HeightChange% 'monitor the change in the land's Y
  18.         'if the land goes to low then reverse the height change+25
  19.         If CurrentHeight% > 320 Then CurrentHeight% = CurrentHeight% - (HeightChange% * 2) - 25: HeightChange% = -(HeightChange% + 25)
  20.         'now check that the land does not go to high
  21.         If CurrentHeight% < 50 Then CurrentHeight% = CurrentHeight% + Abs(HeightChange% * 2) + 25: HeightChange% = Abs(HeightChange% + 25)
  22.  
  23.         Line -Step(RunLength%, HeightChange%), 2 '>>>>>>>>>>>>>>>>>>>>>> this line fixed!!!!!!!!111
  24.         totalrun% = totalrun% + RunLength% 'track how far along it is
  25.         If totalrun% > 645 Then ScreenEnd%% = TRUE 'terrain is now done.
  26.     Loop Until ScreenEnd%%
  27.  
  28.     'box in the paint area
  29.     'Line (0, StartY%)-(0, _Height), 2
  30.     'Line (0, _Height)-(_Width, _Height), 2
  31.     'Line (_Width, 0)-(_Width, _Height), 2
  32.     'Sleep 5
  33.     Paint (320, 340), 2, 2 'ahh lovely green grass land!
  34.     'screw this
  35.     '''''''''If Point(1, 1) = 2 Then Cls: DrawTerrain (basehieght%) 'whole screen is green so try again(why though?)
  36.  
  37.  
  38.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #9 on: August 21, 2021, 09:52:07 pm »
Option explicit and Option explictiArray are 2 dear friends that help to avoid to crash own head on some kind of troubles...
So if you don't use an external IDE (Notepad++ with RhoSigma package, Dav Ide)
spent that few seconds to type Option explicitArray (if you use array) and Option Explicit as the first 2 lines of code.


Yes I can agree that you can have the sensation to code in Pascal but don't mind, you'll gain in amount of time dedicated to the family or whateveryouwant.

PS Bplus has nailed the origin of the problem a Typo error.
« Last Edit: August 21, 2021, 09:53:44 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #10 on: August 22, 2021, 10:26:53 am »
@TempodiBasic

You might have missed the point. The typo on the line added was to try and fix the flooding problem caused by PAINT. That line would not have been needed if the PAINT floodings were fixed.

So why was the PAINT flooding? because Cobalt was subtracting 1 in his line commands meanwhile his accumulator for total distance across was getting 1 ahead each time he drew a line, so when he used more than 6 lines to go from one end to the other the PAINT leaked through right side because of gap at the edge.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #11 on: August 22, 2021, 05:23:20 pm »
@bplus
Yeah, I have verified to understand.
So the
Code: QB64: [Select]
  1.  Line -Step(RunLength%-1, HeightChange%), 2
was the origin of the error
and its solution is
Code: QB64: [Select]
  1.  Line -Step(RunLength%, HeightChange%), 2

while di typo error was in the fixing code Basehiegth% in the place of BaseHeight%.
Thanks to point this.
Programming isn't difficult, only it's  consuming time and coffee

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #12 on: August 22, 2021, 06:30:33 pm »
It was coming up short because of line:

Code: QB64: [Select]
  1.         LINE -STEP(RunLength% - 1, HeightChange%), 2 'run this section

the -1 was making it too short.

Wonder why it didn't fail every time?
Not that I remember why it was -1..... doesn't need to be... hmmmmm? Left overs perhaps?



Option _Explicit is your friend
Option _Explicit. Time saver.

I'll Flirt with Disaster...
I'll Court the Maidens of Doom...
But I will never sleep with the Devil. XD



"All my kingdom for some smilies!"
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #13 on: August 22, 2021, 07:13:37 pm »
Quote
Wonder why it didn't fail every time?

Hi Colbalt,

Remember you added 5 to width so if the number of lines drawn was 6 or less you were covered (645-6 = 639)  but if number of lines exceeded 6 then you'd end up short of the edge.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why am I getting this result? [question about wrong calculations]
« Reply #14 on: August 22, 2021, 07:24:43 pm »
BTW I picked up on the typo because BaseHeight% was not capitalized here:
Code: QB64: [Select]
  1. If Point(1, 1) = 2 Then Cls: DrawTerrain (basehieght%)
  2.  

It sure wasn't from spell checking :)