Professional Documents
Culture Documents
But
But
• The code loads terrain data from “Veri.csv” (or Veri.csv.txt). This data shows
the heights of a
51x71 area.
• In the main function, there is a character array named "MyName". The letters of
MyName are
placed on a circle covering 75% of the terrain at equal intervals from the center
of the terrain.
• In the given code, the letters slide towards the lower terrain sections next to
them, starting
from where they are placed.
• However, they cannot slide to a point where another letter exists
Your Tasks:
• Replace the content of "MyName" with the letters of your name.
• Your task is to modify the "void Terrain::Simulate()" function responsible for
the sliding of
letters.
• In the new function, you will calculate the middle point, which is the arithmetic
average of
the positions of all letters. (Name it Point A)
• All letters want to move towards Point A.
• The terrain affects the direction of the letters. (See Terrain Movement Stages)
• Each letter moves at the same time, and Point A will be recalculated at the end
of each step.
• The simulation will stop when the letters can no longer move.
Assignments will contain only the updated version of the "void Terrain::Simulate()"
function
and will be executable when added to the main code. No other part of the code
should be
submitted, and no additional libraries should be required.
MAIN CODE:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <list>
#include <math.h>
#include <windows.h>
#define MaxX 71
#define MaxY 51
#define PI 3.14159
struct point
{
int x, y;
};
class Letter
{
public:
char TheLetter;
point first;
point current;
list<point> history;
Letter(char l) { TheLetter = l; }
bool stopmoving;
};
class Terrain
{
public:
list<Letter> letters;
int terrain[MaxY][MaxX];
char objects[MaxY][MaxX];
bool readTerrainData(char* filename);
void PrintTerrain();
void RepositionLetters();
void WriteToTemplate();
void PrintObjects();
void Simulate();
};
int x = 0, y = 0;
char* seperator = ";";
while( fgets(line, 1024, fp) ) // while 1: Read the lines from file
{
char* token = strtok(line, seperator);
objects[y][x] = 32; // space
terrain[y][x++] = atoi(token);
return true;
}
void Terrain::PrintTerrain()
{
for(int y=0; y<MaxY; y++)
{
for(int x=0; x<MaxX; x++)
{
cout << terrain[y][x] << " ";
if(x==MaxX/2) cout << "..." << endl << "...";
}
void Terrain::PrintLetters()
{
list<Letter>::iterator it;
for(it = letters.begin(); it != letters.end(); ++it)
cout << it->TheLetter << " ";
cout << endl;
}
void Terrain::RepositionLetters()
{
point center;
center.x = MaxX / 2;
center.y = MaxY / 2;
int n = letters.size();
double r = min(center.x, center.y) * 0.75;
double dtheta = 2 * PI / n;
int i = 0;
list<Letter>::iterator it;
for(it = letters.begin(); it != letters.end(); ++it)
{
it->first.x = center.x + (int)( r * cos(dtheta * i) );
it->first.y = center.y + (int)( r * sin(dtheta * i) );
it->current.x = it->first.x;
it->current.y = it->first.y;
it->history.push_back(it->current);
//cout << it->TheLetter << it->first.x << " " << it->first.y << endl;
i++;
}
}
void Terrain::WriteToTemplate()
{
// Write to template
list<Letter>::iterator it;
for(it = letters.begin(); it != letters.end(); ++it)
{
Letter let = *it;
objects[let.current.y][let.current.x] = let.TheLetter;
}
}
void Terrain::PrintObjects()
{
// write the template to screen
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 3);
cout << endl;
for(int y=0; y<MaxY; y++)
{
cout << '|';
//cout << objects[y] << endl;
for(int x=0; x<MaxX; x++)
cout << objects[y][x];
cout << '|' << endl;
}
SetConsoleTextAttribute(hConsole, 7);
}
void Terrain::Simulate()
{
bool AnyLetterMoved = true;
while(AnyLetterMoved)
{
AnyLetterMoved = false;
list<Letter>::iterator it;
for(it=letters.begin(); it != letters.end(); ++it)
{
if(it->stopmoving) continue;
int minheight = terrain[it->current.y][it->current.x];
list<point> minneighbor;
for(int y = max(0, it->current.y-1); y <= min(MaxY-1, it-
>current.y+1); y++ )
{
for(int x = max(0, it->current.x-1); x <= min(MaxX-1, it-
>current.x+1); x++ )
{
list<Letter>::iterator it2;
bool found = false;
for(it2=letters.begin(); it2 != letters.end(); ++it2)
{
if( ( it2->current.x == x ) && ( it2->current.y
== y ) )
{
if(it->TheLetter == it2->TheLetter)
continue;
found = true;
cout << "collution " << it->TheLetter <<
" - ";
cout << it2->TheLetter << endl;
break;
}
}
if (found) continue;
if ( terrain[y][x] < minheight )
{
minheight = terrain[y][x];
minneighbor.clear();
point p; p.x = x; p.y = y;
minneighbor.push_back(p);
}
else if ( terrain[y][x] == minheight )
{
point p; p.x = x; p.y = y;
minneighbor.push_back(p);
}
} // for x
} // for y
} // for it=letters
} // while(AnyLetterMoved)
}
ter.CreateLetters(MyName);
ter.PrintLetters();
ter.RepositionLetters();
ter.WriteToTemplate();
ter.Simulate();
ter.PrintObjects();
return 0;
}
DATA:
38;39;39;40;41;42;43;44;44;45;45;45;44;43;42;42;43;45;47;48;48;48;48;48;47;46;45;43
;42;41;41;42;43;43;44;44;44;43;43;42;41;40;39;39;40;42;43;44;45;45;45;45;45;45;45;4
6;46;47;48;48;48;47;45;43;42;42;43;44;46;47;48
41;42;42;42;43;44;46;46;47;47;47;46;45;45;44;45;47;49;51;51;51;51;50;49;48;47;46;45
;45;44;44;43;44;44;44;45;45;45;44;43;42;41;41;41;42;44;46;47;48;48;48;47;47;47;47;4
8;49;50;51;52;51;50;47;45;44;43;44;45;47;48;48
44;45;45;45;46;47;48;48;49;49;48;47;46;46;46;47;49;51;53;53;52;51;50;49;48;48;47;46
;46;45;45;44;44;44;44;45;45;45;45;45;44;43;42;42;43;45;47;48;49;49;49;48;47;47;48;4
9;51;53;54;54;54;52;49;47;45;45;45;46;47;48;49
46;47;47;47;48;48;49;49;49;49;48;47;46;46;47;48;50;52;53;53;53;51;50;49;48;48;47;47
;47;47;46;45;44;44;44;44;45;46;46;46;45;44;44;43;44;45;47;48;49;49;48;47;46;46;47;4
9;52;54;56;56;56;54;51;49;48;47;47;48;48;49;49
48;50;50;49;49;49;49;49;49;49;48;47;46;46;47;49;50;52;53;53;52;51;49;49;48;48;48;48
;48;47;47;46;45;44;44;44;46;46;47;47;46;45;44;43;44;44;46;47;48;48;47;46;45;46;47;5
0;53;56;57;58;57;56;54;52;51;50;50;50;50;50;50
50;51;51;50;49;48;48;48;48;48;48;47;46;46;47;48;49;50;51;51;50;50;49;49;48;48;48;48
;48;48;47;46;45;44;45;45;46;47;47;46;45;44;43;42;42;43;44;46;46;46;45;44;44;45;48;5
1;54;57;58;59;58;57;56;54;54;53;53;52;51;51;50
50;51;51;50;48;47;46;47;47;47;47;47;46;46;47;47;48;49;49;49;49;49;49;49;48;48;47;47
;47;47;47;46;45;45;46;47;47;47;47;45;44;43;42;41;41;42;43;44;45;44;44;44;44;46;49;5
2;55;58;59;59;59;58;57;56;56;55;54;53;52;51;51
49;51;50;49;47;46;45;45;46;47;47;47;46;46;46;46;46;47;47;48;48;48;48;48;48;47;47;46
;47;47;46;46;45;46;47;47;48;47;46;44;43;41;40;40;40;41;42;43;44;44;44;44;46;48;51;5
4;56;58;59;59;58;58;57;57;56;56;55;54;53;52;52
49;50;49;48;46;45;44;44;45;46;47;47;46;45;45;45;45;45;46;47;47;48;48;48;48;47;46;46
;46;46;46;46;46;47;47;48;48;47;45;43;42;41;40;40;41;42;43;44;44;44;45;46;47;50;52;5
5;57;58;58;58;57;57;56;56;56;55;55;54;54;54;53
48;49;48;47;45;44;43;43;44;46;46;46;46;45;45;45;45;45;46;47;47;48;48;48;48;47;47;46
;46;47;47;47;47;48;48;49;48;47;45;43;42;41;40;41;42;43;45;46;46;46;47;48;50;52;54;5
6;57;58;57;57;56;55;54;54;54;54;54;54;55;55;54
46;48;47;47;45;44;43;43;44;45;46;46;46;45;45;45;45;46;46;47;48;48;49;49;49;48;48;48
;48;48;48;48;49;49;49;49;49;47;45;44;43;42;41;42;43;45;46;47;48;49;50;51;52;54;55;5
7;57;57;56;55;54;52;52;52;52;53;53;54;55;56;55
46;47;47;47;45;44;43;43;44;44;45;46;46;46;47;47;47;48;48;49;49;49;50;50;50;50;50;50
;50;50;50;50;50;51;51;51;50;48;47;45;44;43;43;43;44;46;47;49;50;51;52;53;54;55;56;5
7;57;56;55;53;51;50;49;49;50;51;53;54;55;56;56
45;47;47;47;46;45;44;44;44;44;45;46;47;48;49;49;50;50;50;51;51;51;51;52;52;52;52;51
;51;51;51;51;51;52;52;52;51;50;49;48;46;45;45;45;46;47;48;50;51;53;54;55;55;56;57;5
7;56;55;54;51;49;47;46;46;47;49;52;53;55;56;56
45;47;48;48;47;46;45;45;45;45;45;46;48;49;50;51;52;52;53;53;52;52;53;53;53;53;53;52
;51;51;51;51;52;52;53;53;52;52;51;50;48;47;46;46;47;48;49;51;52;54;55;55;56;56;56;5
6;55;54;52;50;47;45;44;44;46;48;50;52;54;55;55
45;47;48;49;48;47;47;46;46;46;46;47;48;50;51;53;54;54;54;54;54;54;54;54;54;54;52;51
;51;50;50;51;52;52;53;53;53;53;52;51;50;49;48;48;48;49;50;51;53;53;54;55;55;55;55;5
4;54;52;50;48;46;44;43;43;45;47;49;51;52;53;53
45;48;49;50;49;49;48;48;48;47;47;48;49;50;52;53;55;55;55;55;55;55;55;55;54;53;52;50
;50;49;50;50;51;51;52;52;53;53;53;52;51;50;49;49;50;50;51;52;53;53;53;54;54;54;53;5
3;52;51;49;47;45;43;42;43;44;46;48;49;50;50;50
45;48;50;50;50;50;49;49;48;48;48;48;49;50;52;53;55;55;55;55;55;55;55;55;54;53;51;50
;49;49;49;50;50;50;51;51;52;52;53;53;52;51;51;51;51;52;52;53;53;53;53;53;53;53;52;5
2;51;50;49;47;45;43;43;43;44;45;46;47;48;48;47
46;49;50;51;50;50;49;49;49;49;49;49;49;50;51;53;54;54;55;54;55;55;55;54;54;52;51;50
;49;48;49;49;49;49;49;49;50;52;52;53;52;52;51;52;52;53;54;54;54;53;53;52;52;52;52;5
1;51;50;48;47;45;44;44;44;44;45;46;46;46;46;45
47;49;50;50;50;49;48;48;48;49;49;49;49;49;50;51;52;53;53;53;54;54;54;54;53;52;51;50
;49;49;49;49;48;48;48;48;49;51;52;52;52;51;51;52;53;53;54;54;54;53;53;52;52;52;51;5
1;50;49;48;47;46;45;45;45;45;46;46;46;45;45;44
47;49;50;49;48;47;47;47;47;48;49;49;49;48;49;50;50;51;51;52;52;53;53;53;53;52;51;50
;49;49;49;49;49;48;47;48;49;51;52;52;51;51;50;51;52;53;54;55;55;54;53;53;52;52;51;5
0;50;49;48;48;47;47;47;47;47;47;47;47;46;46;45
47;48;49;48;47;46;45;46;47;48;49;49;48;48;48;48;49;49;49;50;51;52;52;53;53;52;51;50
;50;50;51;50;49;48;48;48;49;51;51;51;50;49;49;50;51;53;54;55;55;54;54;54;53;52;51;5
0;49;49;48;48;49;49;49;48;48;48;48;48;48;48;47
46;48;48;47;45;45;44;45;47;48;49;48;48;47;47;47;48;48;48;49;49;50;51;52;52;51;51;50
;51;51;51;51;50;49;48;49;50;51;51;51;49;48;48;49;50;52;53;54;55;55;55;54;53;52;51;4
9;49;49;49;49;50;50;50;49;49;49;50;50;50;51;50
46;47;47;46;45;44;44;45;46;48;48;48;47;47;47;47;47;47;47;48;48;49;50;51;51;51;50;50
;51;52;52;52;51;50;49;50;51;52;52;51;49;48;47;48;49;50;52;53;54;55;55;55;54;52;51;5
0;49;49;50;50;50;50;50;50;51;51;52;53;53;54;53
46;47;47;46;45;44;44;45;46;47;47;47;47;47;47;47;47;47;47;47;48;49;50;50;50;49;49;49
;50;51;52;52;51;50;50;51;51;52;52;51;49;48;47;47;47;48;50;52;53;54;55;55;54;52;51;5
1;50;50;50;50;50;50;50;51;52;53;54;55;56;56;55
46;48;47;46;45;44;44;45;45;46;47;47;47;47;47;47;47;47;47;48;48;49;50;50;49;48;48;49
;50;51;52;52;51;51;50;51;52;52;52;51;50;48;46;46;46;46;48;50;52;54;54;54;53;52;52;5
1;51;51;51;50;50;50;51;52;53;55;56;57;57;57;55
47;48;48;47;45;45;44;45;45;46;47;47;48;48;48;48;47;47;48;49;49;50;50;50;49;48;47;48
;49;51;51;51;50;50;50;50;51;52;52;51;50;48;46;45;44;44;46;48;51;52;53;53;52;51;51;5
1;51;51;50;49;49;49;51;52;54;56;57;58;58;57;55
46;48;48;47;46;46;45;45;45;46;47;48;48;48;48;47;47;48;49;50;51;51;51;50;49;48;47;48
;49;50;50;50;49;49;49;49;50;51;52;51;50;48;46;44;43;43;45;47;49;50;51;51;50;50;50;5
0;51;50;49;49;48;49;50;52;54;55;57;57;57;56;54
46;48;49;48;47;47;46;46;46;46;47;48;48;48;48;47;47;48;50;51;52;52;52;51;49;48;47;48
;49;49;49;49;48;48;48;49;50;51;52;52;51;49;47;45;43;43;44;45;47;48;49;49;48;48;49;5
0;50;49;49;48;48;48;49;51;53;54;56;56;55;54;53
46;48;49;48;48;47;47;46;46;46;47;47;48;47;47;47;47;49;50;51;52;53;53;52;50;49;48;48
;49;49;49;48;48;47;48;49;50;52;53;53;53;51;49;47;45;44;44;45;45;46;47;47;47;47;48;4
9;49;49;48;47;47;47;48;50;52;53;54;54;53;53;52
46;48;49;49;48;48;47;47;46;46;46;46;47;46;46;46;47;49;50;51;52;53;53;52;51;50;49;49
;49;49;49;48;48;48;49;50;52;54;55;55;55;53;51;49;47;46;45;45;45;45;46;46;47;47;48;4
9;49;49;48;47;47;47;48;49;51;52;53;53;52;52;51
45;47;48;49;49;48;48;47;46;46;46;46;46;46;46;47;48;49;50;51;52;53;53;52;51;51;50;50
;50;50;50;49;49;49;50;52;54;56;57;57;57;55;54;52;50;48;47;46;45;45;46;47;47;48;49;4
9;50;49;48;47;47;47;48;49;50;51;52;52;51;51;51
44;47;48;49;49;49;48;48;47;47;47;47;47;47;47;48;49;49;50;51;51;52;52;52;52;52;51;52
;52;51;51;50;50;51;52;53;55;57;58;58;57;56;55;53;51;49;48;47;46;46;47;47;48;49;50;5
0;50;50;49;48;47;47;48;49;50;51;52;52;51;51;50
44;46;48;49;49;49;49;49;49;49;48;49;49;49;49;49;50;50;50;51;51;52;52;52;53;53;53;53
;53;52;52;52;51;52;53;54;55;56;57;57;56;55;54;52;51;50;49;48;47;47;47;48;49;50;51;5
1;51;50;49;48;48;47;48;49;50;52;53;53;52;51;50
44;46;48;49;50;50;50;50;50;50;50;50;50;50;51;51;51;51;51;51;51;52;52;53;53;53;53;54
;53;53;53;53;53;53;53;53;54;54;55;54;54;53;52;51;50;49;49;48;48;48;48;49;50;51;51;5
1;51;50;49;48;48;47;48;49;51;52;53;54;53;52;50
44;47;48;50;50;51;51;52;52;52;52;52;52;51;52;52;52;52;51;51;51;52;52;53;53;53;54;54
;54;54;54;54;54;53;52;52;52;52;52;52;51;51;50;49;49;49;49;48;48;48;48;49;50;51;51;5
1;50;49;48;47;47;47;48;49;51;53;54;54;54;52;50
45;47;49;50;51;52;52;53;53;53;52;52;52;52;52;53;53;52;52;51;51;51;52;52;52;53;53;53
;54;54;54;54;54;53;52;51;51;51;51;50;50;49;49;49;49;49;49;48;48;47;47;48;49;50;50;5
0;49;48;47;47;47;47;48;50;52;53;54;55;54;52;50
46;49;50;52;52;53;53;53;53;52;52;51;51;52;53;53;53;52;51;51;51;51;51;51;51;51;52;52
;53;53;53;53;53;52;51;50;50;50;50;50;49;49;48;48;49;49;49;48;47;47;46;47;48;49;49;4
9;48;47;46;46;46;47;48;50;52;54;55;55;54;53;50
47;50;52;53;54;54;54;53;52;51;50;50;50;51;52;53;53;52;51;50;50;50;50;50;50;50;51;52
;52;52;52;51;51;50;50;49;50;50;50;49;49;49;49;49;49;49;48;48;47;46;45;46;47;48;48;4
8;47;46;46;46;47;48;49;51;53;54;55;55;55;53;51
48;51;53;54;55;55;54;53;52;50;49;48;49;50;52;52;52;51;51;50;50;50;49;49;49;50;51;51
;51;50;50;49;49;49;49;49;50;50;50;49;49;49;49;50;50;49;48;47;46;46;45;45;46;47;47;4
7;46;46;46;46;48;49;50;52;53;55;56;56;55;54;51
49;52;54;55;56;55;54;53;50;48;47;47;48;50;51;51;51;51;50;50;50;49;49;49;49;50;50;51
;50;49;48;47;47;48;49;50;50;50;50;49;49;49;50;50;50;49;48;48;47;46;46;46;46;47;47;4
7;46;46;47;48;49;50;51;52;54;55;56;56;55;53;52
49;52;54;55;55;55;53;51;49;47;46;46;47;49;50;50;50;50;50;50;49;49;48;48;49;50;51;51
;50;49;47;47;47;48;49;49;49;49;49;49;49;49;49;50;49;49;48;48;47;47;46;46;46;47;47;4
7;47;47;48;49;50;50;51;52;54;55;55;55;54;53;51
48;51;53;54;53;53;51;49;47;45;45;45;46;48;49;49;49;49;49;49;48;48;48;48;49;50;51;51
;50;48;47;46;47;47;48;48;48;48;48;48;48;48;49;48;48;47;47;47;47;47;47;46;46;46;47;4
7;47;48;49;49;50;50;50;51;52;53;54;53;53;52;50
46;49;51;51;51;50;48;46;45;44;44;45;46;47;47;47;47;47;48;47;47;47;47;47;48;49;50;50
;49;48;46;46;46;46;46;46;46;46;46;46;47;47;47;47;46;46;46;46;46;46;46;45;45;45;46;4
7;47;48;48;49;49;49;49;49;50;51;51;51;50;49;48
46;48;49;49;48;47;45;44;43;43;43;44;45;46;46;46;46;46;46;46;46;45;46;47;48;49;49;49
;48;47;46;45;45;44;44;44;44;44;45;45;46;46;45;45;44;44;44;45;45;45;44;44;43;44;45;4
5;46;47;47;48;47;47;47;47;48;48;49;48;48;47;47
46;48;48;47;46;44;43;42;42;43;43;44;45;45;45;45;46;45;45;45;44;44;45;46;48;49;49;48
;47;46;45;44;44;43;43;42;43;43;44;44;45;45;44;44;43;43;43;44;44;43;43;42;42;42;43;4
4;45;45;46;46;46;46;46;47;47;47;47;47;46;46;46
48;48;48;47;45;43;42;41;42;43;44;45;45;45;45;46;46;45;45;44;44;44;45;47;48;49;49;48
;46;45;45;44;44;43;43;43;43;44;44;44;44;44;43;43;43;43;43;43;43;42;41;41;41;41;42;4
3;43;44;44;45;46;46;47;48;48;48;47;47;46;45;46
50;49;48;47;45;43;42;41;43;44;45;46;46;46;47;47;47;46;45;44;44;44;46;48;50;50;49;48
;46;45;45;45;44;44;44;44;45;45;45;44;44;43;43;43;43;44;44;44;43;42;41;40;40;40;41;4
2;42;43;44;45;46;48;50;51;51;50;49;48;47;46;46
51;50;49;48;45;43;42;43;44;46;47;47;48;48;48;48;48;47;46;44;44;45;47;50;51;51;50;48
;47;46;46;46;46;46;47;47;47;47;46;45;44;43;43;44;44;45;45;45;43;42;40;40;40;40;41;4
1;42;42;44;46;48;51;53;54;54;53;52;49;48;47;47
51;51;50;48;46;45;44;44;46;47;48;49;50;50;50;50;49;48;46;45;45;46;49;51;52;52;51;49
;47;47;46;47;47;48;49;49;49;48;46;45;44;44;44;45;46;46;47;46;44;42;41;40;40;41;41;4
1;42;43;44;47;50;53;56;57;57;56;54;51;49;48;47
50;51;50;49;48;46;46;46;48;49;50;51;51;52;53;52;51;49;48;46;46;48;50;51;52;52;51;50
;48;47;47;47;48;49;50;50;50;48;47;46;45;45;46;46;47;48;48;48;46;44;42;41;41;41;42;4
2;43;43;45;48;51;54;57;58;59;57;55;52;49;48;47
50;51;51;50;49;48;47;48;50;51;52;52;53;54;55;54;53;51;49;48;48;49;50;51;52;52;51;50
;48;47;47;47;48;49;50;50;49;48;47;46;46;46;47;48;49;50;50;50;48;46;44;42;42;42;43;4
3;44;45;46;48;51;54;57;58;58;57;55;52;50;48;47