Circles of given radius through two points: Difference between revisions

Content deleted Content added
Steenslag (talk | contribs)
→‎{{header|Ruby}}: Added Ruby entry.
Updated D entry
Line 50: Line 50:
" infinite number of Circles");
" infinite number of Circles");


// Delta x, delta y between points.
// Delta between points.
immutable dx = p2.x - p1.x;
immutable d = V2(p2.x - p1.x, p2.y - p1.y);
immutable dy = p2.y - p1.y;


// Dist between points.
// Distance between points.
immutable q = sqrt(dx ^^ 2 + dy ^^ 2);
immutable q = sqrt(d.x ^^ 2 + d.y ^^ 2);
if (q > 2.0 * r)
if (q > 2.0 * r)
throw new ValueException("separation of points > diameter");
throw new ValueException("separation of points > diameter");


// Halfway point.
// Halfway point.
immutable x3 = (p1.x + p2.x) / 2;
immutable h = V2((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
immutable y3 = (p1.y + p2.y) / 2;


// Distance along the mirror line.
// Distance along the mirror line.
immutable d = sqrt(r ^^ 2 - (q / 2) ^^ 2);
immutable dm = sqrt(r ^^ 2 - (q / 2) ^^ 2);


return typeof(return)(
immutable c1 = Circle(x3 - d * dy / q, y3 + d * dx / q, r.abs);
immutable c2 = Circle(x3 + d * dy / q, y3 - d * dx / q, r.abs);
Circle(h.x - dm * d.y / q, h.y + dm * d.x / q, r.abs),
Circle(h.x + dm * d.y / q, h.y - dm * d.x / q, r.abs));
return typeof(return)(c1, c2);
}
}


void main() {
void main() {
foreach (t; [tuple(V2(0.1234, 0.9876), V2(0.8765, 0.2345), 2.0),
foreach (immutable t; [
tuple(V2(0.1234, 0.9876), V2(0.8765, 0.2345), 2.0),
tuple(V2(0.0000, 2.0000), V2(0.0000, 0.0000), 1.0),
tuple(V2(0.0000, 2.0000), V2(0.0000, 0.0000), 1.0),
tuple(V2(0.1234, 0.9876), V2(0.1234, 0.9876), 2.0),
tuple(V2(0.1234, 0.9876), V2(0.1234, 0.9876), 2.0),
tuple(V2(0.1234, 0.9876), V2(0.8765, 0.2345), 0.5),
tuple(V2(0.1234, 0.9876), V2(0.8765, 0.2345), 0.5),
tuple(V2(0.1234, 0.9876), V2(0.1234, 0.9876), 0.0)]) {
tuple(V2(0.1234, 0.9876), V2(0.1234, 0.9876), 0.0)]) {
writefln("Through points:\n %s,\n %s\n and radius %f\n" ~
writefln("Through points:\n %s %s and radius %f\n" ~
"You can construct the following circles:", t[]);
"You can construct the following circles:", t[]);
try {
try {
Line 88: Line 87:
{{out}}
{{out}}
<pre>Through points:
<pre>Through points:
V2(0.1234, 0.9876),
immutable(V2)(0.1234, 0.9876) immutable(V2)(0.8765, 0.2345) and radius 2.000000
V2(0.8765, 0.2345)
and radius 2.000000
You can construct the following circles:
You can construct the following circles:
Circle(1.86311, 1.97421, 2)
Circle(1.86311, 1.97421, 2)
Line 96: Line 93:


Through points:
Through points:
immutable(V2)(0, 2) immutable(V2)(0, 0) and radius 1.000000
V2(0, 2),
V2(0, 0)
and radius 1.000000
You can construct the following circles:
You can construct the following circles:
Circle(0, 1, 1)
Circle(0, 1, 1)
Line 104: Line 99:


Through points:
Through points:
V2(0.1234, 0.9876),
immutable(V2)(0.1234, 0.9876) immutable(V2)(0.1234, 0.9876) and radius 2.000000
V2(0.1234, 0.9876)
and radius 2.000000
You can construct the following circles:
You can construct the following circles:
ERROR: coincident points give infinite number of Circles
ERROR: coincident points give infinite number of Circles


Through points:
Through points:
V2(0.1234, 0.9876),
immutable(V2)(0.1234, 0.9876) immutable(V2)(0.8765, 0.2345) and radius 0.500000
V2(0.8765, 0.2345)
and radius 0.500000
You can construct the following circles:
You can construct the following circles:
ERROR: separation of points > diameter
ERROR: separation of points > diameter


Through points:
Through points:
V2(0.1234, 0.9876),
immutable(V2)(0.1234, 0.9876) immutable(V2)(0.1234, 0.9876) and radius 0.000000
V2(0.1234, 0.9876)
and radius 0.000000
You can construct the following circles:
You can construct the following circles:
ERROR: radius of zero</pre>
ERROR: radius of zero
</pre>


=={{header|Go}}==
=={{header|Go}}==