Ratio 12 segment

Hi gentlemen.

I'm sorry to start this thread, but I need help with this feature:

I divided the Circle into 12 segments and every 5 seconds I fill in one segment, a total of 60 seconds = 1 minute. The problem is that my last segment is not filling up. Please advise where there may be a mistake?

I enclose an example of a function, including an error.

Thank you for your help

function drawCircle(dc) {
  	    var handWidth = dc.getWidth();
    	var position = System.getClockTime().sec /5; RATIO;
    	dc.setPenWidth(handWidth/18.3);
    	
    	for (var i = 0; i < 12; ++i) {
    
        if (i < position)
    	{
    		
    		dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK); 
    	   
    	}
    	else
    	{
			
			   	
    	    dc.setColor(Graphics.COLOR_LT_GRAY, Graphics.COLOR_BLACK);
    	}		 
	
 
    	   var start = 360 - ( 4 + (30*i)); 
    	   var end = start - 21.1;
    	   dc.drawArc(handWidth/6.2, handWidth/2.47, handWidth/10.5, 1, start+90, end+90); 
  	    }
    
          }

  • BTW if any mods or Garmin employees are reading, I had to edit my comment multiple times before it would accepted by the forum without error. Pretty frustrating experience, especially as it recalls the days this would be an almost daily experience.

  • I only took a quick look at the code and didn't test it. But I noticed the incrementing in the loop ++i . That means the loop runs only 11 times.

    Try instead i++

    As OP mentioned, in this context, ++i vs i++ makes no difference.

    ++i means "i = i + 1, and the value of this expression is the new value of i"
    i++ means "i = i + 1, and the value of this expression is the original value of i"

    Consider this code:

    for (var i = 0; i < 12; ++i) {

    There are 3 expressions in the for loop:

    1) var i = 0; <=== init; executed first, and only once

    2) i < 12; <==== loop condition; evaluated before each loop iteration. if the expression evaluates to false, the loop terminates

    3) ++i; <=== update; executed after each loop iteration

    Since the value of the 3rd expression is not used at the time it's evaluated, it makes no difference whether you write ++i or i++. The only point of the 3rd expression is its side effect (where i is incremented by 1).

    OTOH:

    for (var i = 0; ++i < 12; ) {

    would be different from

    for (var i = 0; i++ < 12; ) {

    But nobody would write a for loop that way. (It's not even valid Monkey C, although it does work in other languages, like js)

  • I'm not sure what you're getting at here, especially since (as you pointed out), the comparison uses "<" and not "==".

    I meant the following:
    The if clause is executed on "i<position", so the else clause is executed on "i>position" or "i==position".

    var position = System.getClockTime().sec / 5.0;
    
    for (var i = 0; i < 12; ++i) {
        if (i < position) {
            dc.setColor(Graphics.COLOR_BLUE, Graphics.COLOR_BLACK); // fill in ith segment
        } else {
            dc.setColor(Graphics.COLOR_LT_GRAY, Graphics.COLOR_BLACK); // don't fill in ith segment
        }
        //...
        


    I've just tested it myself and found that it is possible in monkeyc to compare different variable types.

    var pos = 30 / 5.0;
    var i = 6;
    if (i == pos) {
    System.println("i = " + i + "  pos = " + pos);
    }
    
    
    output:
    i = 6  pos = 6.000000
    

  • As OP mentioned, in this context, ++i vs i++ makes no difference.

    It makes a difference in "if (i < position)", since i is incremented before.

  • The if clause is executed on "i<position", so the else clause is executed on "i>position" or "i==position".

    I've just tested it myself and found that it is possible in monkeyc to compare different variable types.

    Sure and it's also possible (to compare floats and ints) in python, C, java, etc.

    I do see your point where it may not be a good idea to compare (int) 3 directly to (float) 3.0, for example, but you'll notice the final version of my code doesn't have floats at all, anyway.

  • As OP mentioned, in this context, ++i vs i++ makes no difference.

    It makes a difference in "if (i < position)", since i is incremented before.

    No, it doesn't. As I tried to explain, the *value* of the 3rd expression in a for-loop isn't used; the only purpose of the expression is for its side effect (e.g. increment or decrement.)

    But don't take my word for it.

    Try it yourself:

    for (var i = 0; i < 12; i++) {
      System.println("i = " + i);
    }

    vs.

    for (var i = 0; i < 12; ++i) {
      System.println("i = " + i);
    }

    You'll see that they both print the same thing.

    Here's a different way of writing the above for loop.

    var i = 0;

    while (i < 12) {

      System.println("i = " + i);

       i++; // or ++i (makes no difference) (the value of this expression is thrown away)

    }

    As you can see, nobody actually looks at the value of the expression on the bolded line. By the time i < 12 is evaluated, i has already been incremented either way.

    What would make a difference is if you wrote something like this (in, say, javascript, since something like "i++ < 12" isn't even allowed in monkey c):

    var i = 0;

    while (i++ < 12) { // here it makes a difference if you use i++ or ++i because you are using the value of expression

      console.log("i = " + i);

    }

  • I'll try to explain it again, but in general, an expression like i++ or ++i does two things: it evaluates to a value (which can be used or ignored), and it has the side effect of incrementing i's value.

    In Monkey C, it doesn't even seem like i++ or ++i is usable as a value, probably to avoid confusion like this. (Which means that "++i" and "i++" would be identical in Monkey C, but not in other languages.)

    For example, this is valid javascript:

        var i = 0;
        var x = i++; // x = 0, i =1
        var y = ++i; // y = 2, i = 2

    But it won't compile in monkey c.

  • No, it doesn't. As I tried to explain, the *value* of the 3rd expression in a for-loop isn't used; the only purpose of the expression is for its side effect (e.g. increment or decrement.)

    Yes, you are right, thanks for the explanation ;)

    I just noticed that MonkeyC doesn't compile ++i or i++, even though they are accepted as increments in for loops. There are some specific behaviors with MonkeyC that I keep struggling with. In particular, I'm not a big fan of duck typing because of the possibility of unexpected behavior or results that are hard to locate.

    also @

    Now I've digged into OP's code a bit. The reason the last segment is not drawn seems to be the clause "if (i < position)", which I changed to "if (i <= position)", and now the last segment is drawn. To handle the inconsistency between 59s and 0s that you mentioned earlier, I shifted the seconds by -1. All the other code remains the same. Between 55s and 0s, all segments are filled. The behaviour is like your code and since the seconds are rounded to an int anyway, for me it seems to be correct.

    Have fun.

    function drawCircle(dc) {
      	    var handWidth = dc.getWidth();
        	var position = (System.getClockTime().sec -1) /5;
        	dc.setPenWidth(handWidth/18.3);
        	
        	for (var i = 0; i < 12; ++i) {
        
            if (i <= position)
        	{
        		
        		dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK); 
        	   
        	}
        	else
        	{
    			
    			   	
        	    dc.setColor(Graphics.COLOR_LT_GRAY, Graphics.COLOR_BLACK);
        	}		 
    	
     
        	   var start = 360 - ( 4 + (30*i)); 
        	   var end = start - 21.1;
        	   dc.drawArc(handWidth/6.2, handWidth/2.47, handWidth/10.5, 1, start+90, end+90); 
      	    }
        
              }

  • I just noticed that MonkeyC doesn't compile ++i or i++, even though they are accepted as increments in for loops.

    ++i and i++ can be used outside of for loops in Monkey C, they just can't be used as "rvalues" (values which could appear on the right-hand side of an assignment expression). IOW, you can get the side effect of i++ or ++i, you just can't use either one as a value in a larger expression.

    e.g. This is okay in Monkey C:

    var x = 0;
    x++;
    System.println("x = " + x);

    This is not okay in Monkey C:

    var x = 0;
    System.println("x = " + x++);

    Also not okay:

    var x = 0;
    var y = x++;

    Because of this, I'm pretty sure x++ and ++x are equivalent in Monkey C. I'm guessing this design was intentional, to avoid confusion that can arise when you use x++ and ++x as part of a larger expression.

    Same as Monkey C won't accept the following as valid syntax:

    if (x = 0) {
      y  = 1;
    }

    There are some specific behaviors with MonkeyC that I keep struggling with. In particular, I'm not a big fan of duck typing because of the possibility of unexpected behavior or results that are hard to locate.

    Yes, Monkey C can be frustrating, and there are quirks/unexpected behavior in both the language and the API.

    At least Garmin added Monkey Types to Monkey C.