Thread: Printing out an ASCII tire based on two radiuses

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Printing out an ASCII tire based on two radiuses

    I need to print out a rough picture of a tire based on two radiuses. We need to include the axel (the +'s), the hub cap (the $'s), and then the tire portion (the *'s). But all I can get printed is the axel +'s portion. Any ideas on why the other's won't print out?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
    	
    	int r1, r2, x, y;
    	char grid [50][50];
    	
    	printf("Enter the outer radius of your wheel?\n");
    	scanf("%d", &r1);
    	printf("Enter the inner radius of your wheel?\n");
    	scanf("%d", &r2);
    	
    	for(y = 0; y < 49; y++) {
    		for(x = 0; x < 49; x++) {
    			grid[x][y] = ' ';
    		}
    	}
    	
    	for(y = 0; y < 50; y++) {
    		for(x = 0; x < 49; x++) {
    			if(x > 24 + r1 && x < 24 - r1 && y > 24 + r1 && y < 24 - r1) {
    				grid[x][y] = '*';
    			}
    		}
    	}
    	
    	for(y = 0; y < 49; y++) {
    		for(x = 0; x < 49; x++) {
    			if(x > 24 + r2 && x < 24 - r2 && y > 24 + r2 && y < 24 - r2) {
    				grid[x][y] = '$';
    			}
    		}
    	}
    	
    	grid[23][24] = '+', grid[24][23] = '+', grid[24][24] = '+', grid[24][25] = '+', grid[25][24] = '+';
    	
    	for(y = 0; y < 49; y++) {
    		for(x = 0; x < 49; x++) {
    			printf("%c", grid[x][y]);
    		}
    		printf("\n");
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That code won't work, because it's from this thread:
    Grid problem

    and it's crap. (to be blunt about it).

    Swarvy, myself, and others, have mentioned that you HAVE to use an equation to find out what the distance is from the point of the tire, to the center of the tire.

    That's a MUST.



    All that code did was explicitly draw an axle, since the axle is very small. Try that with the whole tire, and it will be a joke you don't like being the brunt of. The best way forward, is to READ what we're posting, and take that current program and run (don't walk), to the nearest trash can, and leave it there.

    You might recall Pythagoras's Theorem for right angle triangles - finding the long side (hypotenuse):

    Hypotenuse² = Base² + Height² (for all right angle triangles, which this is)

    That means:

    1) Use have to include math.h, as well as stdio.h
    2) You only need to do your calculations one time, through the array. Then you can (if you want), print the tire, in another pair of loops, or even print it right after you make the calculations.

    You should go through the whole array, just one or two times - not 3 or 4.

    Tell me when you've thrown that piece of garbage away, and are ready to proceed.

    Last edited by Adak; 10-14-2010 at 06:00 PM.

  3. #3
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Adak View Post
    That code won't work, because it's from this thread:
    Grid problem

    and it's crap. (to be blunt about it).

    Swarvy, myself, and others, have mentioned that you HAVE to use an equation to find out what the distance is from the point of the tire, to the center of the tire.
    Well thanks Adak, you just saved me time. I assumed there was a problem with the code,
    not that it lacked an equation. It seemed like an interesting problem.

    I guess I should start viewing 10 days worth of posts before attempting to help.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe this will explain it better than words:

    Code:
    The idea for using the equation is this:
    
              * <--Point being calculated 
              |\
              | \
              |  \ 
              |   \ 
    Height--> |    \ <-- Distance line is our triangle's hypotenuse
              |     \
              =======O <--Center of the tire
               ^Base^
    
    
    Pythagorean Theorem:
    (Hypotenuse * Hypotenuse) = (Base * Base) + (Height * Height)
    Your row and column in your array, gives you the center point of the tire, and the row and column numbers (coordinates), of the point being calculated.

    Now we need to do a little subtraction, to give us the length of the base:

    Base length = (Center[column] - Point[column]) Which can not be negative, so if it is, we multiply it by -1, to make sure it's positive. Or we use the ABS (absolute value) function, in math.h.

    and the length of the Height:

    Height length = (Center[row] - Point[row]). Again, we need only the positive distance, just like for the Base length.

    After calculating the lengths of the Base and Height, we're ready to calculate with the Pythagorean Theorem

    Every point (square) in the array[row][col], needs to have this calculation done for it, to determine what char should be assigned to that array square, and printed.
    Last edited by Adak; 10-15-2010 at 01:47 PM.

  5. #5
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    This is what i've gotten so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
    	
    	int r1, r2, x, y;
    	char grid [50][50];
    	
    	printf("Enter the outer radius of your wheel?\n");
    	scanf("%d", &r1);
    	printf("Enter the inner radius of your wheel?\n");
    	scanf("%d", &r2);
    	
    	for(x = -50; x < 50; x++) {
    		for (y = -50; y < 50; y++) {
    			if(sqrt(x * x + y * y) > r1 * 2)
    			   printf(" ");
    			else if(sqrt(x * x + y * y) < r1 * 2)
    			   printf("*");
    			else if(sqrt(x * x + y * y) < r2 * 2);
    					}
    		printf("\n");
    	}
    	return 0;
    }
    and it makes a decent circle of *'s (it doesn't have to be a perfect circle)

    but when I go to continue on with the $'s and I do:

    Code:
    else if(sqrt(x * x + y * y) < r1 * 2)
    			   printf("*");
    			else if(sqrt(x * x + y * y) < r2 * 2)
                               printf("$");
    it just gives me a whole jumbled mess of $'s. I'm assuming I did something wrong with the math involving the inner radius?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Now you're on the right path, but X should be starting at 0, and so should y. You can't have negative indeces in your array:

    array[negative number], is not legal in C

    As for your math, it's better (much better, if you do it like I showed it - one calculation at a time, instead of putting it together. It seems better to just put it together, but in reality, it isn't. Very hard to debug it, that way.

    Code:
    int main() {
      long int i, base, height, hyp, r, c;
      char w[ROW][COL];
      for(i=0;i<5;i++)
        printf("\n\n\n\n\n\n\n\n\n\n");  //clears the screen
    
      for(r=0;r<ROW;r++) {
        for(c=0;c<COL;c++) {
          base =abs(c - Cx);
          height = abs(r - Cy) * 2.6; //2.6-2.7 was the best for my screen
          i = (base*base) + (height*height);
          hyp = sqrt(i);
          if(hyp < 0) putchar('8');
          if(hyp > Tread || hyp <= Inside) w[r][c]=' ';
          else if(hyp <= Tread && hyp > Scruff) w[r][c]='$';
          else if(hyp <= Scruff && hyp > Sidewall) w[r][c]='+';
          else if(hyp <= Sidewall && hyp > Inside) w[r][c]='*';
        }
      }


    was what I did. Tread, Scruff, Sidewall and Inside, are #defined values, in my version, but you could just set values explicitly, if you wanted.
    Last edited by Adak; 10-14-2010 at 06:43 PM.

  7. #7
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    When I change the -50's to 0's I get a quarter of the circle:
    Code:
    $$$*****************************                 
    $$$*****************************                  
    $$$*****************************                  
    $$$*****************************                  
    $$$*****************************                  
    $$******************************                  
    $$******************************                  
    $$******************************                  
    $******************************                   
    *******************************                   
    *******************************                   
    *******************************                   
    ******************************                    
    ******************************                    
    *****************************                     
    *****************************                     
    ****************************                      
    ****************************                      
    ***************************                       
    **************************                        
    *************************                         
    *************************                         
    ************************                          
    ***********************                           
    **********************                            
    ********************                              
    *******************                               
    ******************                                
    ****************                                  
    **************                                    
    ************                                      
    ********
    but when I leave it as -50 this is what I get:
    Code:
    ***************                                          
                                           ***********************                                      
                                         ***************************                                    
                                       *******************************                                  
                                     ***********************************                                
                                    *************************************                               
                                   ***************************************                              
                                 *******************************************                            
                                *********************************************                           
                               ***********************************************                          
                              *************************************************                         
                              *************************************************                         
                             ***************************************************                        
                            *****************************************************                       
                           *******************************************************                      
                           *******************************************************                      
                          *********************************************************                     
                          *********************************************************                     
                         ***********************************************************                    
                         ***********************************************************                    
                        *************************************************************                   
                        *************************************************************                   
                        *************************************************************                   
                        ******************************$******************************                   
                       ******************************$$$******************************                  
                       ******************************$$$******************************                  
                       ******************************$$$******************************                  
                       *****************************$$$$$*****************************                  
                       *****************************$$$$$*****************************                  
                       *****************************$$$$$*****************************                  
                       *****************************$$$$$*****************************                  
                      *****************************$$$$$*****************************                 
                       *****************************$$$$$*****************************                  
                       *****************************$$$$$*****************************                  
                       *****************************$$$$$*****************************                  
                       *****************************$$$$$*****************************                  
                       ******************************$$$******************************                  
                       ******************************$$$******************************                  
                       ******************************$$$******************************                  
                        ******************************$******************************                   
                        *************************************************************                   
                        *************************************************************                   
                        *************************************************************                   
                         ***********************************************************                    
                         ***********************************************************                    
                          *********************************************************                     
                          *********************************************************                     
                           *******************************************************                      
                           *******************************************************                      
                            *****************************************************                       
                             ***************************************************                        
                              *************************************************                         
                              *************************************************                         
                               ***********************************************                          
                                *********************************************                           
                                 *******************************************                            
                                   ***************************************                              
                                    *************************************                               
                                     ***********************************                                
                                       *******************************                                  
                                         ***************************                                    
                                           ***********************                                      
                                               ***************

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Trust me. I can draw the tire.

    You would draw a whole circle, but you have your range of x at 100 rows!! Make it about 24 or so, so it fits on the page.

    Y is your column,, I used about 60. More than 70 will goof up your tire dimensions. More than 80 will wreck the whole print out.
    Last edited by Adak; 10-14-2010 at 06:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. printing ASCII
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2002, 09:13 PM
  3. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 08:04 PM
  4. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM
  5. Printing characters not in the ASCII table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 01:47 PM