-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlife_object.cpp
97 lines (80 loc) · 2.38 KB
/
life_object.cpp
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
#include "stdafx.h"
#include "life_object.h"
using namespace std;
extern int errorNum;
extern int debugFlag;
extern string outputFileName;
const int X_MAX = 1024;
string OBJECT_PATH_PREFIX ="./objects/";
void LifeObject::Set(string f, int xCentre, int yCentre, int p, int d){
Coordinate centre(xCentre, yCentre);
/* generating file name */
string inputFileName = genFileName(OBJECT_PATH_PREFIX, f, p, d);
/* opening input file */
FILE *inputFile;
inputFile = fopen( inputFileName.c_str(), "r");
/* checking the file */
if( inputFile == NULL){
cout << "Can't open " << inputFileName;
cout << "Try again." << endl;
cout << "Make sure that object files are in " << OBJECT_PATH_PREFIX << endl;
errorNum++;
}else{
if( debugFlag != 0) cout << inputFileName << "is successfully opened" << endl;
}
char tempString[X_MAX];
tempString[0] = '#';
while( tempString[0] == '#') fgets( tempString, sizeof(tempString), inputFile);
bool finished = false;
while(!finished){
int x, y;
sscanf( tempString, "%d %d", &x, &y);
Coordinate pos(x,y);
coordinates.push_back(pos - centre);
if( fgets( tempString, sizeof(tempString), inputFile) == NULL){
finished = true;
}
}
fclose( inputFile);
}
string LifeObject::genFileName(string path, string fileNameRoot, int phase, int direction){
string FileName;
FileName = path;
FileName += fileNameRoot + ".";
FileName += to_string(phase) + ".";
FileName += to_string(direction) + ".life";
return FileName;
}
void LifeObject::install(int shiftX, int shiftY){
Coordinate installationShift = Coordinate(shiftX, shiftY);
LifeObject::install(installationShift);
}
void LifeObject::install(Coordinate shiftVec) {
for(Coordinate c: coordinates) {
Coordinate p = c + shiftVec -shift;
p.y *= yFlag;
outputDots.push_back(p);
}
}
void LifeObject::write() {
string output = "#Life 1.06\n";
sort(outputDots.begin(), outputDots.end());
for( auto dot : outputDots ){
output.append(dot.to_str());
}
/* opening output file */
FILE *outputFile;
outputFile = fopen( outputFileName.c_str(), "w");
/* checking the file */
if( outputFile == NULL){
cout << "Can't open " << outputFileName << endl;
errorNum++;
}else{
if( debugFlag != 0) cout << outputFileName << "is successfully opened" << endl;
}
fprintf( outputFile, "%s", output.c_str() );
fclose( outputFile);
}
void LifeObject::addCoordinate(Coordinate c) {
outputDots.push_back(c);
}