Here is a snippet from my program:
for (unsigned i=0; i < data.measure_dist.size(); i++)Using with would make this much clearer:
if (data.measure_dist[i].active)
find_crosspoints(
data.measure_dist[i].point_1,
data.measure_dist[i].point_2,
outline,
data.measure_dist[i].crosspoints
);
for (unsigned i=0; i < data.measure_dist.size(); i++)Does anybody confused about this code? Does anybody don't know what above code do? I don't think so.
with (data.measure_dist[i]) {
if (active)
find_crosspoints(point_1, point_2, outline, crosspoints);
}
Lets continue. With could also accept more complex syntax and allow temporary rename different entities. This (inefficient!) code finds all intersection between two sets of segments:
double u, v; // parameters, lerp(A, B, u) = A + u*(B-A)Now compare with following code:
for (int i=0; i < data.points.inner.size() - 1; i++)
for (int j=0; j < data.points.outer.size() - 1; j++) {
bool b = intersection(
data.outline.inner[i],
data.outline.inner[i+1],
data.outline.outer[j],
data.outline.outer[j+1],
u, v);
if (b)
crosspoints.add(lerp(data.outline.inner[i], data.outline.inner[i+1], u));
}
double u, v;Code is easier, cleaner, compacted. In next posts I will show some other ideas.
with (data.points) {
with (inner as A, outer as B) {
for (int i=0; i < A.size() - 1; i++)
for (int j=0; j < B.size() - 1; j++)
if (intersection(A[i], A[i+1], B[i], B[i+1]))
crosspoints.add(lerp(A[i], A[i+1], u));
}
}