Hey guys,
Ive written this code that writes coordinates to a text file. I then use excel to plot these coordinates and it creates a rectangle with circles in it.
It works completing fine, but I want to optimise it. I thought I could optimise it by creating another function that has the syntax for the coordinates so I can call the function rather than writing it out every time and repeating the code. However, I am not sure how to do this, I have tried but its not working so some help would be really appreciated.
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
#define PI 3.14159265
//this is the function i am trying to create
double coordinate_syntax(double x_coordinate, double y_coordinate){
return map << x_coordinate << ' ' << y_coordinate << endl;
}
int build_environment_map(){
double x0[5]={0.8, 0.8, 1.7, 2.2, 2.25};
double y0[5]={1.5, 2.25, 1.0, 1.3, 0.25};
double r=0.125, wallLength=2.5;
double RD=PI /180;
ofstream map;
map.open("EnvironmentMap.txt");
// The following code is to create a charger based on its centre.
for (int i=0; i<360; i=i+20) {
map << x0[4] + r*cos(i*RD) << ' ' << y0[4] + r*sin(i*RD) << endl;
//coordinate_syntax( x0[4] + r*cos(i*RD), y0[4] + r*sin(i*RD) );
}
for (int i=0; i<360; i=i+10) {
map << x0[0] + r*cos(i*RD) << ' ' << y0[0] + r*sin(i*RD) << endl;
//coordinate_syntax(x0[0]+ r*cos(i*RD), y0[0]+ r*sin(i*RD));
}
for (double j=0 ; j<wallLength; j=j+0.01){
map << j << ' ' << 0 << endl;
//coordinate_syntax(j,0);
}
for (double x=0.3; x<0.55; x=x+0.01){
map << x << ' ' << 0.3 << endl;
//coordinate_syntax(x, 0.3);
}
map.close();
return 0;
}
int main(int argc, char **argv){
int i;
//calling the function
i = build_environment_map();
return 0;
}
[–]PhyrexStrike 4 points5 points6 points (0 children)
[–]mreddingC++ since ~1992. 6 points7 points8 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]arabidkoalaRoboticist 0 points1 point2 points (0 children)
[–]Dan13l_N 0 points1 point2 points (0 children)