Yet Another Drawing Program

Filed under: Programming in C,Tutorials — by Ron on September 9, 2005 @ 9:53 am

Here is a slightly more complicated drawing program. It will scale a car. You get to choose a scale between 1 and 4. Most home ed families have a minivan. Your challenge is to see if you can change the pattern into a minivan.

There is a board in the forum (link on the top) which we set up to discuss these C Tutorials. Once you register you can post questions or discussion items there.

/* Draw a scaled car on the console or terminal window.
Takes input value of length of the arm of the X.
C Tutorials for the home ed student
Ron R.
September 2005

Consider an car which is 6 characters high and 19 characters wide

row column
0 1234567890123456789
1 *********
2 * * *
3 ******************
4 * *
5 *** ******** ****
6 ** **

Note that:
the top is 2 rows, the body is 3 rows and the wheels are 1 row
the length is 19 characters
therefore we can scale this by multiplying the dimensions by 1,2,3 or 4
and it will still fit on the screen
normally the front and rear windows are sloped
and the wheels are round
the remainder will be calculated as necessary
*/

#include

int main()
{
int scale; // entered scale of car and height of wheels
int top; // height of top
int body; // height of body
int row,col; // current row and column in printing
int length; // overall length of car
int pad; // how much to pad left of car
int topleft; // top left of top of car
int topright; // top right of top of car
int hood; // left of hood of car
int doorpost; // location of door post
int bottomleft; // left of left wheel
int bottomright; // left of right wheel
int wheel; // width of wheel

do
{
printf(“Enter the scale of the car (1-4, 0=Exit): “);
scanf(“%i”,&scale);
} while(scale < 0 || scale > 4);
// only draw if they have not entered a 0
if(scale > 0)
{
// this is a good example of the use of a switch
switch(scale)
{
case 1:
pad = 30;
break;
case 2:
pad = 21;
break;
case 3:
pad = 12;
break;
case 4:
pad = 2;
break;
}
// give it some spacing
printf(“\n”);
// calculate some values for car
hood = top = scale * 2;
bottomleft = body = scale * 3;
length = scale * 19;
topleft = scale * 6;
topright = scale * 14;
doorpost = scale * 10;
bottomright = scale * 13;
wheel = scale * 3 – 1;
// loop through each row of top
for(row=1;row< =top;row++)
{
// pad the row to center the car
for(col=1;col<=pad;col++)
{
printf(" ");
}
// this will be repeated for each row
for(col=1;col<=length;col++)
{
// if roof
if(row == 1)
{ // if between topleft and topright
if(col >= topleft && col < = topright)
{
printf("*");
}
else
{
printf(" ");
}
}
else
{
if((row + col) == (topleft + 1) || // front window
(topright + row) == (col + 1) || // rear window
col == doorpost)
{
printf("*");
}
else
{
printf(" ");
}
}
}
// goto a new line
printf("\n");
}
// loop through each row of body
for(row=1;row<=body;row++)
{
// pad the row to center the car
for(col=1;col<=pad;col++)
{
printf(" ");
}
// this will be repeated for each row
for(col=1;col<=length;col++)
{
if(row == 1) // top line of body
{
if(col >= hood) // round hood a bit
{
printf(“*”);
}
else
{
printf(” “);
}
}
else if(row == body) // bottom line of body
{
if(col < = bottomleft || // left of front wheel
(col > (bottomleft + wheel) &&
col < = bottomright) || // between wheels
col > (bottomright + wheel)) // right of rear wheel
{
printf(“*”);
}
else
{
printf(” “);
}
}
else if(row < = (((scale - 1) * 2) + 1)) // sloped front
{
if((col + row) == (hood + 1) || // front
col == length) // back
{
printf("*");
}
else
{
printf(" ");
}
}
else // square front
{
if(col == 1 || // front
col == length) // back
{
printf("*");
}
else
{
printf(" ");
}
}
}
// goto a new line
printf("\n");
}
// loop through each row of wheels
for(row=1;row<=scale;row++)
{
// pad the row to center the car
for(col=1;col<=pad;col++)
{
printf(" ");
}
// this will be repeated for each row
for(col=1;col<=length;col++)
{
if(row == scale) // bottom of wheel
{
if((col >= (bottomleft + scale) && // front wheel
col < = (bottomleft + wheel - scale + 1)) ||
(col >= (bottomright + scale) && // rear wheel
col <= (bottomright + wheel - scale + 1)))
{
printf("*");
}
else
{
printf(" ");
}
}
else // slope of wheel
{
if(col == (bottomleft + row) || // front left
(col + row) == (bottomleft + wheel + 1) || // front right
col == (bottomright + row) || // rear left
(col + row) == (bottomright + wheel + 1)) // rear right
{
printf("*");
}
else
{
printf(" ");
}
}
}
// goto a new line
printf("\n");
}
}
return 0;
}

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress MU Theme by Ron and Andrea.