Circles of given radius through two points: Difference between revisions
Content deleted Content added
→{{header|Ruby}}: Added Ruby entry. |
Updated D entry |
||
Line 50: | Line 50: | ||
" infinite number of Circles"); |
" infinite number of Circles"); |
||
// Delta |
// Delta between points. |
||
immutable |
immutable d = V2(p2.x - p1.x, p2.y - p1.y); |
||
immutable dy = p2.y - p1.y; |
|||
// |
// Distance between points. |
||
immutable q = sqrt( |
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 |
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 |
immutable dm = sqrt(r ^^ 2 - (q / 2) ^^ 2); |
||
⚫ | |||
⚫ | |||
Circle(h.x - dm * d.y / q, h.y + dm * d.x / q, r.abs), |
|||
⚫ | |||
⚫ | |||
} |
} |
||
void main() { |
void main() { |
||
foreach (t; [ |
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 |
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 |
ERROR: radius of zero |
||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |