Skip to content

Commit

Permalink
Minor code tidy of variable offset example
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusJohnson committed Dec 19, 2023
1 parent 2d7564f commit 84b30f1
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions CPP/Examples/VariableOffset/VariableOffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ void test1() {

ClipperOffset co;
co.SetDeltaCallback([delta](const Path64& path,
const PathD& path_norms, size_t curr_idx, size_t prev_idx)
const PathD& path_norms, size_t curr_idx, size_t prev_idx)
{
// gradually scale down the offset to a minimum of 25% of delta
double high = static_cast<double>(path.size() - 1) * 1.25;
return (high - curr_idx) / high * delta;
// gradually scale down the offset to a minimum of 25% of delta
double high = static_cast<double>(path.size() - 1) * 1.25;
return (high - curr_idx) / high * delta;
});

Path64 ellipse = Ellipse(Rect64(0, 0, 200 * scale, 180 * scale));
size_t el_size = ellipse.size() * 0.9;
ellipse.resize(el_size);
Paths64 subject = { ellipse };
Paths64 subject{ Ellipse(Rect64(0, 0, 200 * scale, 180 * scale)) };
subject[0].resize(subject[0].size() * 0.9);

co.AddPaths(subject, JoinType::Miter, EndType::Round);
Paths64 solution;
Expand All @@ -53,17 +51,15 @@ void test2() {
double delta = 10 * scale;

ClipperOffset co;
co.SetDeltaCallback([delta](const Path64& path,
co.SetDeltaCallback([delta](const Path64& path,
const PathD& path_norms, size_t curr_idx, size_t prev_idx) {
// calculate offset based on distance from the middle of the path
double mid_idx = static_cast<double>(path.size()) / 2.0;
return delta * (1.0 - 0.70 * (std::fabs(curr_idx - mid_idx) / mid_idx));
// calculate offset based on distance from the middle of the path
double mid_idx = static_cast<double>(path.size()) / 2.0;
return delta * (1.0 - 0.70 * (std::fabs(curr_idx - mid_idx) / mid_idx));
});

Path64 ellipse = Ellipse(Rect64(0, 0, 200 * scale, 180 * scale));
size_t el_size = ellipse.size() * 0.9;
ellipse.resize(el_size);
Paths64 subject = { ellipse };
Paths64 subject{ Ellipse(Rect64(0, 0, 200 * scale, 180 * scale)) };
subject[0].resize(subject[0].size() * 0.9);

co.AddPaths(subject, JoinType::Miter, EndType::Round);
Paths64 solution;
Expand All @@ -85,12 +81,12 @@ void test3() {
ClipperOffset co;
co.AddPaths(subject, JoinType::Miter, EndType::Polygon);

co.SetDeltaCallback([radius](const Path64& path,
co.SetDeltaCallback([radius](const Path64& path,
const PathD& path_norms, size_t curr_idx, size_t prev_idx) {
// when multiplying the x & y of edge unit normal vectors, the value will be
// largest (0.5) when edges are at 45 deg. and least (-0.5) at negative 45 deg.
double delta = path_norms[curr_idx].y * path_norms[curr_idx].x;
return radius * 0.5 + radius * delta;
// when multiplying the x & y of edge unit normal vectors, the value will be
// largest (0.5) when edges are at 45 deg. and least (-0.5) at negative 45 deg.
double delta = path_norms[curr_idx].y * path_norms[curr_idx].x;
return radius * 0.5 + radius * delta;
});

// solution
Expand Down

0 comments on commit 84b30f1

Please sign in to comment.