Skip to content

Commit

Permalink
Merge pull request #236 from saxbophone/josh/235-walk-whole-lines-at-…
Browse files Browse the repository at this point in the history
…once

Allow walking whole lines at once
  • Loading branch information
saxbophone authored Feb 1, 2019
2 parents 7a2b706 + 498cfd5 commit a07237c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
7 changes: 6 additions & 1 deletion sxbp/refine_figure_shrink_from_end.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ static sxbp_result_t sxbp_figure_collides(
// set collided to false initially
*data.collided = false;
// begin walking the figure, use our callback function to handle points
sxbp_walk_figure(figure, 1, sxbp_figure_collides_callback, (void*)&data);
sxbp_walk_figure(
figure,
1,
false, // don't plot vertices only, we need all 1-unit sub-lines
sxbp_figure_collides_callback, (void*)&data
);
// free the memory allocated for the bitmap
sxbp_free_bitmap(&bitmap);
return SXBP_RESULT_OK;
Expand Down
2 changes: 2 additions & 0 deletions sxbp/render_figure_to_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ sxbp_result_t sxbp_render_figure_to_bitmap(
sxbp_walk_figure(
figure,
2,
// don't plot vertices only, we need 1-unit sub-lines too for bitmap
false,
sxbp_render_figure_to_bitmap_callback,
(void*)&data
);
Expand Down
8 changes: 6 additions & 2 deletions sxbp/render_figure_to_svg.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static sxbp_result_t sxbp_write_svg_body_origin_dot(
}

// private, callback function for sxbp_write_svg_body_figure_line()
static bool sxbp_render_figure_to_bitmap_callback(
static bool sxbp_write_svg_body_figure_line_callback(
sxbp_co_ord_t location,
void* callback_data
) {
Expand Down Expand Up @@ -290,7 +290,11 @@ static sxbp_result_t sxbp_write_svg_body_figure_line(
sxbp_walk_figure(
figure,
2,
sxbp_render_figure_to_bitmap_callback,
false, // TODO: change this to the commented-out line below:
/*
true, // plot vertices only, we don't need 1-unit long sub-lines for SVG
*/
sxbp_write_svg_body_figure_line_callback,
(void*)&data
);
// chop off the extra space at the end
Expand Down
20 changes: 16 additions & 4 deletions sxbp/sxbp_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ sxbp_co_ord_t sxbp_get_origin_from_bounds(const sxbp_bounds_t bounds) {
void sxbp_walk_figure(
const sxbp_figure_t* figure,
size_t scale,
bool plot_vertices_only,
bool( *plot_point_callback)(sxbp_co_ord_t location, void* callback_data),
void* callback_data
) {
Expand All @@ -126,17 +127,28 @@ void sxbp_walk_figure(
if (!plot_point_callback(location, callback_data)) {
return;
}
// for each line, plot separate points along their length
// for each line, plot one or more points along it (depending on plot mode)
for (sxbp_figure_size_t i = 0; i < figure->size; i++) {
sxbp_line_t line = figure->lines[i];
// scale the line's size
for (sxbp_figure_size_t l = 0; l < line.length * scale; l++) {
// move the location
sxbp_move_location(&location, line.direction, 1);
sxbp_length_t length = line.length * scale;
// if plotting vertices only, plot one point at the end of this line
if (plot_vertices_only) {
// move the location length amount of units
sxbp_move_location(&location, line.direction, length);
// plot a point, if callback returned false then exit
if (!plot_point_callback(location, callback_data)) {
return;
}
} else { // otherwise, plot one point along each one unit of line length
for (sxbp_length_t l = 0; l < length; l++) {
// move the location one unit
sxbp_move_location(&location, line.direction, 1);
// plot a point, if callback returned false then exit
if (!plot_point_callback(location, callback_data)) {
return;
}
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion sxbp/sxbp_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ sxbp_co_ord_t sxbp_get_origin_from_bounds(const sxbp_bounds_t bounds);
* private, walks the line of the figure, calling the callback with the
* coördinates of each point of space occupied by the line of the figure
* the scale of the shape produced can be increased with the scale parameter
* the shift parameter offsets the points produced
* parameter plot_vertices_only, if true will cause the callback to only be
* called every time a vetice of the figure's line is encountered, i.e. the
* interface of each line segment with another
* the callback should return false if it does not want the function to continue
* walking the line, otherwise it should return true.
*/
void sxbp_walk_figure(
const sxbp_figure_t* figure,
size_t scale,
bool plot_vertices_only,
bool( *plot_point_callback)(sxbp_co_ord_t location, void* callback_data),
void* callback_data
);
Expand Down

0 comments on commit a07237c

Please sign in to comment.