Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 RGB Colours.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jekate




PostPosted: Wed Oct 26, 2005 7:58 pm   Post subject: RGB Colours.

I know there are already threads on RGB colours but there is something that is really bugging me. I've looked over this many times and so have my friends but we can't figure it out. Here is the code :

code:


var col1 : real
var colchange : int

col1 := 0

loop
    if col1 = 1 then
        colchange := 1
    elsif col1 = 0 then
        colchange := 0
    end if
    if colchange = 1 then
        col1 := col1 - 0.01
    elsif colchange = 0 then
        col1 := col1 + 0.01
    end if
    RGB.SetColor (1, col1, 0, 0)
    drawfillbox (0, 0, 100, 100, 1)
    delay (10)
end loop




The program is suppose to draw the box so it turns from black to red slowly. It does that properly but then it is suppose to do the same back to black.

It is probably some stupid mistake. Thnx.
Sponsor
Sponsor
Sponsor
sponsor
beard0




PostPosted: Wed Oct 26, 2005 8:25 pm   Post subject: (No subject)

I have not fixed your code, but simply added two lines that will show you what your problem is so that you can fix your code yourself. The technique I have used here is a very useful one that you should start using yourself to debug your programs.

code:
var col1 : real
var colchange : int

col1 := 0

loop
    if col1 = 1 then
        colchange := 1
    elsif col1 = 0 then
        colchange := 0
    end if
    if colchange = 1 then
        col1 := col1 - 0.01
    elsif colchange = 0 then
        col1 := col1 + 0.01
    end if
    RGB.SetColor (1, col1, 0, 0)
    drawfillbox (0, 0, 100, 100, 1)
    delay (10)
    locate (1, 1)                    %My debugger
    put "col1: ", col1               %   lines
end loop
Jekate




PostPosted: Wed Oct 26, 2005 8:33 pm   Post subject: (No subject)

I get what your showing but the program still isn't working as it should. I have:

code:

if col1 = 1 then
colchange := 1
end if


and then

code:

if colchange = 1 then
col := col - 0.01
end if


This should make col1 decrease once it reaches 1 but it is not for some reason. I think that it is skipping 1 for unknown causes. I fixed the program though by adding >= and <= to it. Here it is:

code:

var col1 : real
var colchange : int

col1 := 0

loop
    if col1 >= 1 then
        colchange := 1
    elsif col1 <= 0 then
        colchange := 0
    end if
    if colchange = 1 then
        col1 := col1 - 0.01
    elsif colchange = 0 then
        col1 := col1 + 0.01
    end if
    RGB.SetColor (1, col1, 0, 0)
    drawfillbox (0, 0, 100, 100, 1)
    delay (10)
end loop
beard0




PostPosted: Wed Oct 26, 2005 9:13 pm   Post subject: (No subject)

The problem you were having comes from the fact that 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + .... + 0.01 does not equal 1 in Turing, and many other languages, as it is stored in binary, and so there are actually roudning errors: 0.01 base ten in binary is 0.000000101000111101011100001010001111010111000010100011110101... This was actually already discussed here in detail. The proper way to check if two real numbers are "equal" is to use
code:
abs(a-b)<tolerance
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: