Circles of given radius through two points: Difference between revisions

→‎{{header|Julia}}: A new entry for Julia
m (→‎{{header|BASIC}}: removed empty line)
(→‎{{header|Julia}}: A new entry for Julia)
Line 854:
[0.1234,0.9876,0.8765,0.2345,0.5] ───► points are too far from each other
[0.1234,0.9876,0.1234,0.9876,0] ───► [0.1234,0.9876]</lang>
 
=={{header|Julia}}==
This solution uses the package [https://github.com/timholy/AffineTransforms.jl AffineTransforms.jl] to introduce a coordinate system (u, v) centered on the midpoint between the two points and rotated so that these points are on the u-axis. In this system, solving for the circles' centers is trivial. The two points are cast as complex numbers to aid in determining the location of the midpoint and rotation angle.
 
'''Types and Functions'''
<lang Julia>
immutable Point{T<:FloatingPoint}
x::T
y::T
end
 
immutable Circle{T<:FloatingPoint}
c::Point{T}
r::T
end
Circle{T<:FloatingPoint}(a::Point{T}) = Circle(a, zero(T))
 
using AffineTransforms
 
function circlepoints{T<:FloatingPoint}(a::Point{T}, b::Point{T}, r::T)
cp = Circle{T}[]
r >= 0 || return (cp, "No Solution, Negative Radius")
if a == b
if abs(r) < 2eps(zero(T))
return (push!(cp, Circle(a)), "Point Solution, Zero Radius")
else
return (cp, "Infinite Solutions, Indefinite Center")
end
end
ca = Complex(a.x, a.y)
cb = Complex(b.x, b.y)
d = (ca + cb)/2
tfd = tformtranslate([real(d), imag(d)])
tfr = tformrotate(angle(cb-ca))
tfm = tfd*tfr
u = abs(cb-ca)/2
r-u > -5eps(r) || return(cp, "No Solution, Radius Too Small")
if r-u < 5eps(r)
push!(cp, Circle(apply(Point, tfm*[0.0, 0.0]), r))
return return (cp, "Single Solution, Degenerate Centers")
end
v = sqrt(r^2 - u^2)
for w in [v, -v]
push!(cp, Circle(apply(Point, tfm*[0.0, w]), r))
end
return (cp, "Two Solutions")
end
</lang>
 
'''Main'''
<lang Julia>
tp = [Point(0.1234, 0.9876),
Point(0.0000, 2.0000),
Point(0.1234, 0.9876),
Point(0.1234, 0.9876),
Point(0.1234, 0.9876)]
 
tq = [Point(0.8765, 0.2345),
Point(0.0000, 0.0000),
Point(0.1234, 0.9876),
Point(0.8765, 0.2345),
Point(0.1234, 0.9876)]
 
tr = [2.0, 1.0, 2.0, 0.5, 0.0]
 
println("Testing circlepoints:")
for i in 1:length(tp)
p = tp[i]
q = tq[i]
r = tr[i]
(cp, rstatus) = circlepoints(p, q, r)
println(@sprintf("(%.4f, %.4f), (%.4f, %.4f), %.4f => %s",
p.x, p.y, q.x, q.y, r, rstatus))
for c in cp
println(@sprintf(" (%.4f, %.4f), %.4f",
c.c.x, c.c.y, c.r))
end
end
</lang>
 
{{out}}
<pre>
Testing circlepoints:
(0.1234, 0.9876), (0.8765, 0.2345), 2.0000 => Two Solutions
(1.8631, 1.9742), 2.0000
(-0.8632, -0.7521), 2.0000
(0.0000, 2.0000), (0.0000, 0.0000), 1.0000 => Single Solution, Degenerate Centers
(0.0000, 1.0000), 1.0000
(0.1234, 0.9876), (0.1234, 0.9876), 2.0000 => Infinite Solutions, Indefinite Center
(0.1234, 0.9876), (0.8765, 0.2345), 0.5000 => No Solution, Radius Too Small
(0.1234, 0.9876), (0.1234, 0.9876), 0.0000 => Point Solution, Zero Radius
(0.1234, 0.9876), 0.0000
</pre>
 
=={{header|Liberty BASIC}}==