Grid problem

This is a discussion on Grid problem within the C Programming forums, part of the General Programming Boards category; Have a problem with my C course. The problem asks for a user defined grid where the user enter an ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    Grid problem

    Have a problem with my C course. The problem asks for a user defined grid where the user enter an inner and outer radius for a tire and the program creates an "image" of the tire using the characters * $ and +. My problem is I don't know what kind of logic to use in order to create the tire. Please help, and expediency is appreciated cause it's due tonight.

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Show us an attempt and we will help you. I am presuming that your program will be console based since you want to display one of three symbols at each point. On that basis, I will give you a hint: Think about your the console 'grid' as your coordinate system. You can test where a point is in/near the circle using Pythagoras' theorem.

    edit: By the way, this recent thread might prove to be helpful: drawing a circle in C
    Last edited by Swarvy; 10-13-2010 at 03:06 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Yeah, That's pretty much exactly what I'm trying (more or less) But I don't remember the prof going over anything like that, his site is down, and I have no clue how to start. I know how to do output in C, but only directly defined output, not grid based, and have no clue how pythagoras could help me here. No I seem a bit clueless, but I'm also panicking so i could be clouding my own mind. It happens. And for clarification, it's a console window using ascII graphics.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Start with what you know you'll need - he grid (aka 2D array[][]). Your data type will be type char, and what angle are you drawing this wheel/tire from - side, top, or what? I would choose side, but whatever your prof wants.

    ++ looks like tread, and $$$ looks like where the tread meets the sidewall. Maybe use * for the sidewall itself. I'm just throwing out ideas here.

    As for the wheel itself, you'll put every char onto the grid, based on it's distance from the center. That's where the Pythagorean theorem comes in.

    Say I want to know what char goes at row3, column (col) 1, and the center is at 10, 10. So I work out my equation to find that distance from that square to the center:

    distance≤ (hypotenuse) = height≤ + base≤, so

    d≤ = (10-3)≤ + (10-1)≤ (Note: always a positive number for height and base)
    d≤ = 49 + 81
    Distance = 11.4 and round it down to 11, since char's can't be less than whole integers

    So if I want the center to the end of the start of the sidewall to be 12, then it gets the char for the inside portion of the wheel or tire, not the char for the sidewall.l
    Last edited by Adak; 10-13-2010 at 04:15 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I didn't mention the absolute value part of the math for it:

    Where "base" is the int length of the base of the triangle, "height" is the height, and i is just a temporary long int. "hyp" is the hypotenuse.

    Cx = x value of the center point of the wheel (horizontal position on screen)
    Cy = y value of the center point of the wheel (vertical position on the screen)


    Without the " * 2.7", my tires look more like eggs The right number here, depends on what font size
    you use, in your console window.

    Code:
          base =abs(c - Cx);
          height = abs(r - Cy) * 2.7; //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]='*';
    Tread is a define for the most outward portion of the tire, then Scruff, then Sidewall, and finally Inside.

    You have to include <math.h> for abs() and sqrt().
    Last edited by Adak; 10-13-2010 at 08:18 PM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Ok, I appreciate the help, but I'm not getting it. I take in two inputs for radius'. The program then draws two circles around a central hub. I don't know how to establish the grid to plot the coordinates, or to establish the origin. I know, for instance, the central hub is supposed to be 5 +'s. so it would be points (1,0), (-1,0), (0,1), (0,-1), and (0,0). And if the radius is 5, the other circle would be all points fro there to (0,5) and so on. these points can be plotted with simple for loops, but how do you establish the grid. Can someone please write like, a loop for the hub just so I can see how it woiks. Don't wanna cheat, but having severe issues here

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Ok, I was being dense, and I've started to sift through it. Got my grid setup, but I'm having trouble defining the circles. Here's the code.
    Code:
    #include "stdio.h"
    #include "math.h"
    
    main()
    {
          int r1, r2, x, y ;
          char grid [50] [50] ;
          
          printf("Enter the outer radius of your wheel? ") ;
          scanf("%d", &r1) ;
          printf("Enter the inner radius of your wheel? ") ;
          scanf("%d", &r2) ;
          
          for(y = 0; y < 50 ; y++)
          {
               for(x = 0; x < 50 ; x++)
               {
                     grid[x][y] = ' ' ;
               }
               x = 0 ;
          }
          
          for(y = 0; y < 50 ; y++)
          {
               for(x = 0; x < 50 ; x++)
               {
                     if(x > 24 + r1 || x < 24 - r1 && y > 24 + r1 || y < 24 - r1)
                     {
                          grid[x][y] = '*' ;
                     }
               }
               x = 0 ;
          }
          
          grid [23] [24] = '+', grid [24] [24] = '+', grid [25] [24] = '+',
             grid [24] [23] = '+', grid [24] [25] = '+' ;
          
          for(y = 0; y < 50 ; y++)
          {
               for(x = 0; x < 50 ; x++)
               {
                     printf("%c", grid[x] [y]) ;
               }
               printf("\n") ;
               x = 0 ;
          }
          system("pause") ;
          
    }

  8. #8
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    You have include an 'x = 0;' statement in your for loops for some reason. Given where it is, this is unnecessary - it does nothing.

    Anyway, don't let that put you off, you are definitely making progress but you should do something like this for your if statements:
    Code:
    c_squared = pow(x - 24, 2) + pow( y - 24, 2);
    
    if( c_squared > r1*r1 )
    {
        /* Draw some stuff here */
    }
    By the way, have you considered what will happen if the values of r1 and r2 are greater than 50?

    Edit: General equation for a circle in Cartesian coordinates is:

    r^2 = (x - x0)^2 + (y - y0)^2

    Where x0 and y0 are the coordinates of the centre.

    Further Edit: If you want your wheel to appear central in your grid it might be worth making your grid an odd size. E.g. Instead of doing a 50 by 50 grid, do a 51 by 51 or 49 by 49 grid, because that way the centre point will appear 'even'. If you don't know what I mean, you will when you start drawing stuff.
    Last edited by Swarvy; 10-13-2010 at 09:32 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The grid is just:

    Code:
    Your #includes up here: stdio.h and math.h
    
    #defines down here: I used Cx (x value of the center point), and 
    Cy (vertical value of the center point. 
    
    Several others for Tread, Sidewall, Inside, etc., parts of the tire. 
    And ROWS and COLS for the array itself. 
    
    I moved the center of the tire from 10,10 to 12, 30 so it
    could be better centered, and let me draw a bigger tire.
    
    I used long int's for i, c, base, height, hyp.
    
    
    char w[ROWS][COLS];  //declare the w(heel) array
    
    for(r=0; r < ROWS;r++)  {
      for(c=0; c<COLS; c++)  {
          //arithmetic in here
          if(hyp > Tread) w[r][c] = ' ';  //a blank space for outside the tire
          else if(hyp <= Tread and > Sidewall) w[r][c]='$';
          else if(hyp .... etc. like that. Step down through every part of the tire.
    
       }
      printf("\n");
    }
    
    All calculations are done, now draw it.
    for(r=0; r< ROWS;r++)
      for(c=0; c<COLS;c++) 
        printf("%c", w[r][c]);
    That's really all there is to it. Hard part is getting the math to work out, since the fonts are usually NOT square. (as noted in the previous post).
    Last edited by Adak; 10-13-2010 at 09:36 PM.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Ok, keepin it simple, this should work, I think, but it's asking me to include a filename, so something is messed up. If someone could please help me look it over, I'm a bit to close to it.
    Code:
    #include stdio.h
    
    main()
    {
          int r1, r2, x, y ;
          char grid[50][50] ;
          
          printf("Enter the outer radius of your wheel: ") ;
          scanf("%d", &r1) ;
          printf("Enter the inner radius of your wheel: ") ;
          scanf("%d", &r2) ;//User input
          
          for(y = 0 ; y < 49 ; y++)
          {
                for(x = 0 ; x < 49 ; x++)
                {
                      grid[x][y] = ' ' ;
                }
          }//first loop initializes a blank grid
          
          for(y = 0 ; y < 49 ; y++)
          {
                for(x = 0 ; x < 49 ; x++)
                {
                      if(x > 24 + r1 && x < 24 - r1 && y > 24 + r1 && y < 24 - r1)
                      {
                           grid[x][y] = '*' ;
                      }
                }
          }//forms outer ring
          
          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] = '$' ;
                      }
                }
          }//forms inner ring
          
          grid[23][24] = '+', grid[24][24] = '+', grid[25][24] = '+', grid[24][23] = '+', grid[24][25] = '+' ;
          //central hub
          for(y = 0; y < 49 ; y++)
          {
                for(x = 0; x < 49 ; x++)
                {
                      printf("%c", grid[x][y]) ;
                }
                printf("\n") ;
          }//displays wheel
          
          system("pause") ;
    }

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    1
    Hey man, did you get this figured out? I can get it to run but all it prints is the axle?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No distance to the center equations? I don't get that. IMO, you *have* to have a distance equation from the center of the wheel, to the square on the grid.

    How could that program ask you for a filename?

    Let's see what it draws. That's not what you want, imo.

    I think what they were going for was something like this: The "font factor" needs to be bumped up a bit, since it's still a bit egg-shaped, but it's easily fixed.
    Attached Images Attached Images  
    Last edited by Adak; 10-14-2010 at 03:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21