static void walk(Chaincode *cc, int start_node, unsigned char **pixels, int dx, int dy) { int x, y; int allocated = 20; int count = 0; Rope *rope; char *steps; assert(0 <= start_node && start_node < cc->node_count); x = cc->nodes[start_node].x; y = cc->nodes[start_node].y; /* Check that there's indeed a road in the given direction. */ if (!pixels[y + dy][x + dx]) return; /* Check that we haven't passed here before. */ if (passed_edge(pixels[y][x], dx, dy)) return; cc->nodes[start_node].rope_indices[count_passed_edges(pixels[y][x])] = cc->rope_count; steps = MALLOC(char, allocated); mark_edge(pixels, x, y, dx, dy); x += dx; y += dy; *append_step(&steps, &count, &allocated) = chaincode_char(dx, dy); if (pixels[y][x] == 1) { pixels[y][x] = 0; determine_direction_first_time(pixels, x, y, &dx, &dy); while (1) { x += dx; y += dy; *append_step(&steps, &count, &allocated) = chaincode_char(dx, dy); if (pixels[y][x] != 1) break; pixels[y][x] = 0; determine_direction(pixels, x, y, &dx, &dy); } } /* We've arrived at a hot point. Mark the entrance. */ assert(!passed_edge(pixels[y][x], -dx, -dy)); mark_edge(pixels, x, y, -dx, -dy); /* Add the rope. */ rope = chaincode_append_rope(cc); rope->start = start_node; rope->end = find_node(cc, x, y); rope->steps = REALLOC(char, steps, count); rope->length = count; cc->nodes[rope->end].rope_indices[count_passed_edges(pixels[y][x]) - 1] = cc->rope_count - 1; }
/* Same, but we might have a black pixel where we came from. */ static void determine_direction_first_time(unsigned char **pixels, int x, int y, int *pdx, int *pdy) { unsigned char save_pixel = pixels[y - *pdy][x - *pdx]; int new_dx, new_dy; pixels[y - *pdy][x - *pdx] = 0; /* reduce the task to previous */ determine_direction(pixels, x, y, &new_dx, &new_dy); pixels[y - *pdy][x - *pdx] = save_pixel; *pdx = new_dx; *pdy = new_dy; }
int main(int argc, char **argv) { int column = 1; int row = 1; int direction = 66; printf("Begining tests of determine_direction\n"); direction = determine_direction(column,row,column+1,row); assert(direction == right); direction = determine_direction(column,row,column-1,row); assert(direction == left); direction = determine_direction(column,row,column,row-1); assert(direction == up); direction = determine_direction(column,row,column,row+1); assert(direction == down); direction = determine_direction(column,row,column+2,row); assert(direction == invalid); printf("All tests done\n"); }
Vector::Vector(double x1, double y1, double x2, double y2) { x_component_ = std::abs(x2 - x1); y_component_ = std::abs(y2 - y1); int quadrant = determine_quadrant(x1, y1, x2, y2); magnitude_ = sqrt(pow(x_component_, 2) + pow(y_component_, 2)); double theta = radians_to_degrees(acos(x_component_ / magnitude_)); direction_ = determine_direction(quadrant, theta); set_component_signs(direction_); Logger::write(Logger::string_stream << "creating vector:"); Logger::write(Logger::string_stream << "\t\tmagnitude: " << magnitude_); Logger::write(Logger::string_stream << "\t\tdirection: " << direction_); Logger::write(Logger::string_stream << "\t\tx_component: " << x_component_); Logger::write(Logger::string_stream << "\t\ty_component: " << y_component_); Logger::write(Logger::string_stream << "\t\tquadrant: " << quadrant); Logger::write(Logger::string_stream << "\t\t(" << x1 << "," << y1 << ") & (" << x2 << "," << y2 << ")"); }