Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ / How to refactor this function
    primarykey
    data
    text
    <p>I have a map stored as a NxM matrix. In this map I want to add a mazed path storing as well the direction of the path and the type (line from left to right, corner from top to left, etc...) for graphical rendering issues.</p> <p>The path is constructed by sucesive calls to a function that adds an horizontal or vertical path (code posted below). This function has very similar pieces of code wich I think can be refactored to a more general case but I can´t see it. Do you know how can I do it?</p> <pre><code>void Map::addTraject(int initial_x, int initial_y, int final_x, int final_y){ //Check verticality of the path bool vertical; if (initial_x == final_x) vertical = false; else if (initial_y == final_y) vertical = true; else throw WrongCoordinatesException("Traject cannot be diagonal"); //Get the last road added to the path Road* last; if (route.size() == 0) { last = NULL; } else { last = route.at(route.size()-1); assert(initial_x == last-&gt;x &amp;&amp; initial_y == last-&gt;y); } if (last != NULL){ //Last road never can be a corner switch(last-&gt;type) { case LEFT_TOP: case LEFT_DOWN: case RIGHT_TOP: case RIGHT_DOWN: case TOP_LEFT: case TOP_RIGHT: case DOWN_LEFT: case DOWN_RIGHT: assert(false); } } if (vertical) { if (initial_x &lt; final_x) //LEFT-RIGHT path { //Change last road to a corner if needed if (last != NULL) { switch(last-&gt;type) { case LEFT_RIGHT: break; case RIGHT_LEFT: throw WrongCoordinatesException("Path cannot be overlapped"); case TOP_DOWN: last-&gt;type = TOP_RIGHT; break; case DOWN_TOP: last-&gt;type = DOWN_RIGHT; break; } initial_x++; //Start in the position at the right } for (int i = initial_x; i &lt;= final_x; i++){ route.push_back(new Road(i, initial_y, LEFT_RIGHT)); } }else { //RIGHT-LEFT path //Change last road to a corner if needed if (last != NULL) { switch(last-&gt;type) { case LEFT_RIGHT: throw WrongCoordinatesException("Path cannot be overlapped"); case RIGHT_LEFT: break; case TOP_DOWN: last-&gt;type = TOP_LEFT; break; case DOWN_TOP: last-&gt;type = DOWN_LEFT; break; } initial_x--; //Start in the position at the left } for (int i = initial_x; i &gt;= final_x; i--){ route.push_back(new Road(i, initial_y, RIGHT_LEFT)); } } } else { if (initial_y &lt; final_y){ //TOP-DOWN path //Change last road to a corner if needed if (last != NULL) { switch(last-&gt;type) { case LEFT_RIGHT: last-&gt;type = LEFT_DOWN; break; case RIGHT_LEFT: last-&gt;type = RIGHT_DOWN; break; case TOP_DOWN: break; case DOWN_TOP: throw WrongCoordinatesException("Path cannot be overlapped"); } initial_y++; //Start in the position at the bottom } for (int i = initial_y; i &lt;= final_y; i++){ route.push_back(new Road(initial_x, i, TOP_DOWN)); } } else { //DOWN-TOP path //Change last road to a corner if needed if (last != NULL) { switch(last-&gt;type) { case LEFT_RIGHT: last-&gt;type = LEFT_TOP; break; case RIGHT_LEFT: last-&gt;type = RIGHT_TOP; break; case TOP_DOWN: break; case DOWN_TOP: throw WrongCoordinatesException("Path cannot be overlapped"); } initial_y--; } for (int i = initial_y - 1; i &gt;= final_y; i--){ route.push_back(new Road(initial_x, i, DOWN_TOP)); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload