Image Processing 2d grid help

I am stumped. I cannot get the image to display at all. I am suppose to interface with the class Draw, but I cannot change anything in it. So I had to make a class that inherited public from the draw class. From what I can see on my code, it should work out fine, I have the changes in xpos and ypos, along with having it place a '*' on the grid where it needs to. I'm pretty sure it has something to do with setup_screen(), process_picture(), and display_picture. However, I am not sure. I really need help with this. The instructions for the code are listed below along with the code I have so far. Please and thank you!

Btw, I've googled so many things related to this project, many of which are about OpenGL and vectors and such. I've tried 2d grids, arrays, and many other types of searches all to no avail.


Instructions:

Sometimes, storing an image is easier if you store the steps taken to draw the image rather than storing the image itself. In such a situation, a sequence of directed lines are concatenated, or attached to one another, to form the image. For example, a simple drawing program might allow eight simple directions of movement:
1
2
3
4
5
6
8	1	2

7		3


6	5	4

For this example, a “picture” composed of the sequence “1, 3, 4, 3” would draw a line segment up, to the right, down on an angle, then right again.

Develop a program to take an encoding sequence of numbers from the user and then create a drawing based on the sequence, using the arrow directions displayed in the first figure. Your program should use the console screen for the drawing surface and should allow for a figure up to 20 characters tall and 70 characters wide. Your drawings on this grid should be made with the asterisk (*) character or you can allow the user to enter a character. Each drawing must start in the lower-left corner and if a command would have the figure move “off the page,” the program must ignore that command and proceed on to the next command in the sequence.

Each encoded sequence entered will be a list of numbers, positive and/or negative, ending with a 0 as the indicator that the drawing is finished. If the number is negative, you should move in the direction you would if the value were positive, but you do not put an asterisk or user defined symbol in the position when done moving. Sequences may be of any length. After inputting the zero value, your program will display the final image.

Inherit from the supplied Draw class that I will provide to you. Add the necessary code in your inherited class but be sure to reuse as much code from my class as possible. Do not change or modify the class code that I provided to you. (You can add comments though.)
Get the source code to get you started on the final project from our class website. I will be in lab during finals week on Wednesday, May 21st from 5:30pm to 7:30pm if you need any last minute help on this project.
Sample Output:

Enter drawing sequence below.

1 1 1 2 2 4 4 5 5 7 7 7 0

Your picture:
1
2
3
4
5
  *
 * *
*   *
*   *
*****



My code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
 /*******************************************************************************
* Program Name: 
* Created Date: 
* Created By:   
* Purpose:      
*******************************************************************************/

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

//You can modify these numbers but don't delete these constants or this starting code will not work
const int YMAX = 20; // Array bound for screen height
const int XMAX = 70; // Array bound for screen length

// DO NOT ALTER OR DELETE THIS CODE (START)!!!!!!!!!!!!!!!!!!!!!!!!!!!
/********************************************************************
* Class: Draw
* Purpose: To process a list of numbers to draw a picture on the console.
********************************************************************/
class Draw {
protected:
	char display[XMAX][YMAX]; // 20 by 70 character array to hold the picture
	int input, xpos, ypos;
public:
	// Constructor to initialize object member data
	Draw() : input(0), xpos(0), ypos(YMAX - 1)
	{	}
	void greeting(); // Greeting screen to let the user know what this program is and to instruct the user on how to use the program
	void setup_screen(); // Setup the array 
	void process_picture(); // Process the user provided string and store into the array
	void display_picture(); // Print the picture onto the screen
};

void Draw::greeting() {
	cout << "Welcome to the picture string generator!" << endl
		<< "Please enter a string of numbers representing " << endl
		<< "a picture that you would like to display." << endl
		<< "Numbers print as the following:" << endl << endl
		<< "8   1   2" << endl
		<< " \\  |  /" << endl
		<< "7 --+-- 3" << endl
		<< " /  |  \\" << endl
		<< "6   5   4" << endl << endl
		<< "Positive numbers will draw an *." << endl
		<< "A negative number will move the cursor but not draw a *." << endl;
}

void Draw::setup_screen() {
}

void Draw::process_picture() {
}

void Draw::display_picture() {
}
// DO NOT ALTER OR DELETE THIS CODE (END)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//You can modify and change main()

class Inherit : public Draw
{
public:
	Draw image;
	

	void userinput()
	{

		
		cout << "\n\nEnter drawing sequence below. Add spaces between digits.\n\n";
		
		do
		{
			
			cin >> input;
			//POSITIVE
			if (input == 1) //UP
			{
				if (ypos - 1 > 0) //Make sure the user is in bounds
				{
					ypos--;
					display[xpos][ypos] = '*';
					image.process_picture();
				}
			
			
			}
			if (input == 2) //UP - RIGHT
			{
				if (xpos < XMAX && ypos - 1 > 0 ) //Make sure the user is in bounds
				{
					xpos++, ypos--;
					display[xpos][ypos] = '*';
					image.process_picture();
				}
			}
			if (input == 3) //RIGHT
			{
				if (xpos < XMAX) //Make sure the user is in bounds
				{
					xpos++;
					display[xpos][ypos] = '*';
				}
			}
			if (input == 4) //DOWN - RIGHT
			{
				if (xpos < XMAX && ypos -1 < YMAX) //Make sure the user is in bounds
				{
					xpos++, ypos++;
					display[xpos][ypos] = '*';
				}
			}
			if (input == 5) //DOWN
			{
				if (ypos - 1 < YMAX) //Make sure the user is in bounds
				{
					ypos++;
					display[xpos][ypos] = '*';
				}
			}
			if (input == 6) //DOWN - LEFT
			{
				if (xpos > 0 && ypos - 1 < YMAX) //Make sure the user is in bounds
				{
					xpos--, ypos++;
					display[xpos][ypos] = '*';
				}
			}
			if (input == 7) //LEFT
			{
				if (xpos > 0) //Make sure the user is in bounds
				{
					xpos--;
					display[xpos][ypos] = '*';
				}
			}
			if (input == 8) //UP - LEFT
			{
				if (xpos > 0 && ypos - 1 > 0) //Make sure the user is in bounds
				{
					xpos--, ypos--;
					display[xpos][ypos] = '*';
				}
			} 
			

			//NEGATIVE
			if (input == -1) //UP
			{
				if (ypos - 1 > 0) //Make sure the user is in bounds
				{
					ypos--;
				}

			}
			if (input == -2) //UP - RIGHT
			{
				if (xpos < XMAX && ypos - 1 > 0) //Make sure the user is in bounds
				{
					xpos++, ypos--;
				}
			}
			if (input == -3) //RIGHT
			{
				if (xpos < XMAX) //Make sure the user is in bounds
				{
					xpos++;
				}
			}
			if (input == -4) //DOWN - RIGHT
			{
				if (xpos < XMAX && ypos - 1 < YMAX) //Make sure the user is in bounds
				{
					xpos++, ypos++;
				}
			}
			if (input == -5) //DOWN
			{
				if (ypos - 1 < YMAX) //Make sure the user is in bounds
				{
					ypos++;
				}
			}
			if (input == -6) //DOWN - LEFT
			{
				if (xpos > 0 && ypos - 1 < YMAX) //Make sure the user is in bounds
				{
					xpos--, ypos++;
				}
			}
			if (input == -7) //LEFT
			{
				if (xpos > 0) //Make sure the user is in bounds
				{
					xpos--;
				}
			}
			if (input == -8) //UP - LEFT
			{
				if (xpos > 0 && ypos - 1 > 0) //Make sure the user is in bounds
				{
					xpos--, ypos--;
				}
			}
		
		}
		
		while (input != 0);
	
		image.process_picture();
	}



};
int main() 
{


	Draw image;
	image.greeting();
	image.setup_screen();


	Inherit data;
	data.userinput();

	/*system("cls");*/
	cout << "Your picture: \n\n";
	
	
	image.display_picture();
	

	system("pause");
	return 0;
}
Last edited on
I cleaned up your class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Inherit : public Draw
{
public:
	Draw image;

	void userinput()
	{
		cout << "\n\nEnter drawing sequence below. Add spaces between digits.\n\n";
		
		do
		{
			cin >> input;


			if ( input > 0 )
			{
				if ( input == 8 || input == 1 || input == 2 ) // UP
					if ( ypos - 1 > 0 ) // Make sure the user is in bounds
						--ypos;

				else if ( input == 4 || input == 5 || input == 6 ) // DOWN
					if ( ypos - 1 < YMAX ) // Make sure the user is in bounds
						++ypos;


				if ( input == 6 || input == 7 || input == 8 ) // LEFT
					if ( xpos > 0 ) // Make sure the user is in bounds
						--xpos;

				else if ( input == 4 || input == 5 || input == 6 ) // RIGHT
					if ( ypos - 1 < XMAX ) // Make sure the user is in bounds
						++xpos;


				display[xpos][ypos] = '*';

				image.process_picture ( );
			}

			else if ( input < 0 )
			{
				input *= -1; // make positive

				if ( input == 8 || input == 1 || input == 2 ) // UP
					if ( ypos - 1 > 0 ) // Make sure the user is in bounds
						--ypos;

				else if ( input == 4 || input == 5 || input == 6 ) // DOWN
					if ( ypos - 1 < YMAX ) // Make sure the user is in bounds
						++ypos;


				if ( input == 6 || input == 7 || input == 8 ) // LEFT
					if ( xpos > 0 ) // Make sure the user is in bounds
						--xpos;

				else if ( input == 4 || input == 5 || input == 6 ) // RIGHT
					if ( ypos - 1 < XMAX ) // Make sure the user is in bounds
						++xpos;
			}
		} while ( input );
	
		image.process_picture ( );
	}
};

Are these functions in the class you were given supposed to do something? Cause that might be your problem.
1
2
3
4
5
6
7
8
void Draw::setup_screen() {
}

void Draw::process_picture() {
}

void Draw::display_picture() {
}
I'm not sure, however, that's how it came. To be honest, I couldn't makes head's or tails about it =/ I pretty much figured that it had something to do with Draw(), the voids under it, along with the protected char and int. And since I couldn't edit it, I assumed it worked as is.

Also, thank you for cleaning up my code! My formatting is still iffy >_<

Edit:
It's due in a few hours, I've been pounding my head against the wall for MANY (over 40) hours trying to make this program work. I've even asked the professor. I am missing something very important and I just can't figure it out.
Last edited on
I wanted see this work, so I went ahead and made those functions do something. Except process_picture(). I'm not sure what that's supposed to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
* Program Name: Teroniz's Project
* Created Date: May 24, 2014
* Created By: Yay295
* Purpose: To draw stuff.
*******************************************************************************/

#include <iostream>
#include <string>

// You can modify these numbers but don't delete them or the starting code will not work.
const int YMAX = 20; // Array bound for screen height
const int XMAX = 70; // Array bound for screen length


/********************************************************************
* Class: Draw
* Purpose: To process a list of numbers to draw a picture on the console.
********************************************************************/

class Draw
{
protected:
	char display[XMAX][YMAX]; // 20 by 70 character array to hold the picture
	int input, xpos, ypos;

public:
	// Constructor to initialize object member data
	Draw ( ) : input(0), xpos(0), ypos(0) { }

	void greeting ( ); // Greeting screen to let the user know what this program is and to instruct the user on how to use the program.
	void setup_screen ( ); // Setup the array 
	void process_picture ( ); // Process the user provided string and store into the array.
	void display_picture ( ); // Print the picture onto the screen.
};

void Draw::greeting ( ) {
	std::cout << "Welcome to the picture string generator!\n"
		"Please enter a string of numbers representing \n"
		"a picture that you would like to display.\n"
		"Numbers print as the following:\n\n"
		"8   1   2\n"
		" \\  |  /\n"
		"7 --+-- 3\n"
		" /  |  \\\n"
		"6   5   4\n\n"
		"Positive numbers will draw an *.\n"
		"A negative number will move the cursor but not draw a *.\n";
}

void Draw::setup_screen ( )
{
	for ( int x = 0; x < XMAX; ++x )
	{
		for ( int y = 0; y < YMAX; ++y )
		{
			display[x][y] = '\0';
		}
	}
}

void Draw::process_picture ( )
{
}

void Draw::display_picture ( )
{
	for ( int y = YMAX - 1; y >= 0; --y )
	{
		for ( int x = 0; x < XMAX; ++x )
		{
			std::cout << display[x][y];
		}

		std::cout << '\n';
	}
}


class Inherit : public Draw
{
public:
	void userinput ( )
	{
		std::cout << "\n\nEnter drawing sequence below. Your cursor starts in the bottom left corner\n"
			"of a rectangle " << XMAX << " spaces wide and " << YMAX << " spaces tall. Press enter between digits.\n\n";

		display[xpos][ypos] = '*'; // set the first space as used
		
		do
		{
			std::cin >> input;


			if ( input > 0 )
			{
				if ( input == 8 || input == 1 || input == 2 ) // UP
				{
					if ( ypos + 1 < YMAX ) // Make sure the user is in bounds
						++ypos;
				}

				else if ( input == 4 || input == 5 || input == 6 ) // DOWN
				{
					if ( ypos - 1 >= 0 ) // Make sure the user is in bounds
						--ypos;
				}


				if ( input == 6 || input == 7 || input == 8 ) // LEFT
				{
					if ( xpos - 1 >= 0 ) // Make sure the user is in bounds
						--xpos;
				}

				else if ( input == 2 || input == 3 || input == 4 ) // RIGHT
				{
					if ( xpos + 1 < XMAX ) // Make sure the user is in bounds
						++xpos;
				}


				display[xpos][ypos] = '*';
			}

			else if ( input < 0 )
			{
				input *= -1; // make positive

				if ( input == 8 || input == 1 || input == 2 ) // UP
				{
					if ( ypos + 1 < YMAX ) // Make sure the user is in bounds
						++ypos;
				}

				else if ( input == 4 || input == 5 || input == 6 ) // DOWN
				{
					if ( ypos - 1 >= 0 ) // Make sure the user is in bounds
						--ypos;
				}


				if ( input == 6 || input == 7 || input == 8 ) // LEFT
				{
					if ( xpos - 1 >= 0 ) // Make sure the user is in bounds
						--xpos;
				}

				else if ( input == 2 || input == 3 || input == 4 ) // RIGHT
				{
					if ( xpos + 1 < XMAX ) // Make sure the user is in bounds
						++xpos;
				}
			}
		} while ( input );
	}
};


int main ( ) 
{
	Inherit image;

	image.greeting ( );
	image.setup_screen ( );
	image.userinput ( );


	std::cout << "\nYour picture:\n\n";
	
	image.display_picture ( );

	std::cout << '\n';
}
Last edited on
I just realized something...
We don't have to use the functions in Draw...
We could just create our own functions that do what those functions are supposed to do...

*facepalm*
Geh, I guess that is true. I've been so focused on getting those void functions in the Draw class to work that I just overlooked(didn't even think of) that... GRAH. Btw, HUGE freaking help... you are a life saver!

Edit: Hmm, although the way you changed it makes it so it doesn't start at the bottom left.

Edit: Made the void functions in my own class. Getting the image upside down with the grid being filled in. Currently trying to fix it xD The reason being, I don't want to touch the Draw class.

http://gyazo.com/34e735908ed849152f6ba6513fbdbc06.png
Last edited on
Ok, the image turns out perfect. BUT, it's upside down... need a bit more help xD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
* Program Name: 
* Created Date:
* Created By:  
* Purpose:      
*******************************************************************************/

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

//You can modify these numbers but don't delete these constants or this starting code will not work
const int YMAX = 20; // Array bound for screen height
const int XMAX = 70; // Array bound for screen length

// DO NOT ALTER OR DELETE THIS CODE (START)!!!!!!!!!!!!!!!!!!!!!!!!!!!
/********************************************************************
* Class: Draw
* Purpose: To process a list of numbers to draw a picture on the console.
********************************************************************/
class Draw {
protected:
	char display[XMAX][YMAX]; // 20 by 70 character array to hold the picture
	int input, xpos, ypos;
public:
	// Constructor to initialize object member data
	Draw() : input(0), xpos(0), ypos(YMAX - 1)
	{	}
	void greeting(); // Greeting screen to let the user know what this program is and to instruct the user on how to use the program
	void setup_screen(); // Setup the array 
	void process_picture(); // Process the user provided string and store into the array
	void display_picture(); // Print the picture onto the screen
};

void Draw::greeting() {
	cout << "Welcome to the picture string generator!" << endl
		<< "Please enter a string of numbers representing " << endl
		<< "a picture that you would like to display." << endl
		<< "Numbers print as the following:" << endl << endl
		<< "8   1   2" << endl
		<< " \\  |  /" << endl
		<< "7 --+-- 3" << endl
		<< " /  |  \\" << endl
		<< "6   5   4" << endl << endl
		<< "Positive numbers will draw an *." << endl
		<< "A negative number will move the cursor but not draw a *." << endl;
}

void Draw::setup_screen() {
}

void Draw::process_picture() {
}

void Draw::display_picture() {
}
// DO NOT ALTER OR DELETE THIS CODE (END)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//You can modify and change main()

class Inherit : public Draw
{
public:
	
	void userinput()
	{
		cout << "\n\nEnter drawing sequence below. Press space between digits.\n\n";

		display[xpos][ypos] = '*'; // set the first space as used

		do
		{
			cin >> input;


			if (input > 0)
			{
				if (input == 8 || input == 1 || input == 2) // UP
				{
					if (ypos -1 > 0) // Make sure the user is in bounds
						--ypos;
				}

				else if (input == 4 || input == 5 || input == 6) // DOWN
				{
					if (ypos - 1 < YMAX) // Make sure the user is in bounds
						++ypos;
				}


				if (input == 6 || input == 7 || input == 8) // LEFT
				{
					if (xpos > 0) // Make sure the user is in bounds
						--xpos;
				}

				else if (input == 2 || input == 3 || input == 4) // RIGHT
				{
					if (xpos < XMAX) // Make sure the user is in bounds
						++xpos;
				}


				display[xpos][ypos] = '*';
			}

			else if (input < 0)
			{
				input *= -1; // make positive

				if (input == 8 || input == 1 || input == 2) // UP
				{
					if (ypos - 1 > 0) // Make sure the user is in bounds
						--ypos;
				}

				else if (input == 4 || input == 5 || input == 6) // DOWN
				{
					if (ypos - 1 < YMAX) // Make sure the user is in bounds
						++ypos;
				}


				if (input == 6 || input == 7 || input == 8) // LEFT
				{
					if (xpos > 0) // Make sure the user is in bounds
						--xpos;
				}

				else if (input == 2 || input == 3 || input == 4) // RIGHT
				{
					if (xpos < XMAX) // Make sure the user is in bounds
						++xpos;
				}
			}
		} while (input);
	}

	void setup()
	{
		for (int x = 0; x < XMAX; ++x)
		{
			for (int y = 0; y < YMAX; ++y)
			{
				display[x][y] = '\0';
			}
		}
	}
	void picture()
	{
		for (int y = YMAX - 1; y > 0; --y)
		{
			for (int x = 0; x < XMAX; ++x)
			{
				cout << display[x][y];
			}

			cout << endl;
		}


	}
	
};
int main() 
{


	Draw image;
	Inherit data;

	image.greeting();
	data.setup();
	data.userinput();

	cout << "Your picture: \n\n";

	data.picture();
	cout << endl;

	system("pause");
	return 0;
}
Last edited on
Finished! Thank you Yay295!
Topic archived. No new replies allowed.