Bézier curves/Intersections: Difference between revisions

→‎{{header|D}}: Updated with duplicates check.
(Added Go)
(→‎{{header|D}}: Updated with duplicates check.)
Line 329:
This program subdivides both curves by de Casteljau's algorithm, until only very small subdivisions with overlapping control polygons remain.
 
:'''Update: I have added a crude check against accidentally detecting the same intersection twice, similar to the check in the [[#Modula-2|Modula-2]] program. I also changed the value of <code>tol</code>, so that the check sometimes comes out positive.'''
(A real-life program probably should take greater care that, for instance, intersections at subdivision boundaries are not accidentally counted twice.)
 
<syntaxhighlight lang="D">
Line 344:
import std.algorithm;
import std.container.slist;
import std.math;
import std.range;
import std.stdio;
Line 438 ⟶ 439:
}
}
}
 
bool
seems_to_be_a_duplicate (point[] intersections, point xy,
double spacing)
{
bool seems_to_be_dup = false;
int i = 0;
while (!seems_to_be_dup && i != intersections.length)
{
immutable pt = intersections[i];
seems_to_be_dup =
fabs (pt.x - xy.x) < spacing && fabs (pt.y - xy.y) < spacing;
i += 1;
}
return seems_to_be_dup;
}
 
point[]
find_intersections (quadratic_curve p, quadratic_curve q, double tol)
double tol, double spacing)
{
point[] intersections;
Line 469 ⟶ 487:
if (accept)
{
// This is a crude way to avoid detecting the same
intersections.length = num_intersections + 1;
intersections[num_intersections] =// intersection; twice: require some space between
// intersections. For, say, font design work, this method
num_intersections += 1;
// should be adequate.
if (!seems_to_be_a_duplicate (intersections,
intersection, spacing))
{
intersections.length = num_intersections + 1;
intersections[num_intersections] = intersection;
num_intersections += 1;
}
}
else if (!exclude)
Line 497 ⟶ 523:
q.y.c0 = 1.0; q.y.c1 = 2.0; q.y.c2 = 3.0;
 
immutable tol = 0.0000001;
auto intersections = find_intersections (p, q, 0.000001);
immutable spacing = 10 * tol;
 
auto intersections = find_intersections (p, q, 0.000001tol, spacing);
for (int i = 0; i != intersections.length; i += 1)
printf("(%f, %f)\n", intersections[i].x, intersections[i].y);
Line 506 ⟶ 535:
 
{{out}}
<pre>(0.654983, 2.854984854983)
(0.881025, 1.118975)
(-0.681025, 2.681025)
(-0.854984854983, 1.345017)</pre>
 
=={{header|Go}}==
1,448

edits