Line circle intersection: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
mNo edit summary
Line 284: Line 284:
Intersection: Circle (4.0,2.0) 5.0 and Line (7.0,4.0) (11.0,18.0): [(7.46,5.61),(5.03,-2.89)]
Intersection: Circle (4.0,2.0) 5.0 and Line (7.0,4.0) (11.0,18.0): [(7.46,5.61),(5.03,-2.89)]
Intersection: Circle (4.0,2.0) 5.0 and Segment (7.0,4.0) (11.0,18.0): [(7.46,5.61)]</pre>
Intersection: Circle (4.0,2.0) 5.0 and Segment (7.0,4.0) (11.0,18.0): [(7.46,5.61)]</pre>


=={{header|Julia}}==
Uses the circles and points from the Go example.
<lang julia>using Luxor

const centers = [Point(3, -5), Point(0, 0), Point(4, 2)]
const rads = [3, 4, 5]
const lins = [
[Point(-10, 11), Point(10, -9)], [Point(-10, 11), Point(-11, 12)],
[Point(3, -2), Point(7, -2)], [Point(0, -3), Point(0, 6)],
[Point(6, 3), Point(10, 7)], [Point(7, 4), Point(11, 8)],
]

println("Center", " "^9, "Radius", " "^4, "Line P1", " "^14, "Line P2", " "^7,
"Segment? Intersect 1 Intersect 2")
for (cr, l, extended) in [(1, 1, true), (1, 2, false), (1, 3, false),
(2, 4, true), (2, 4, false), (3, 5, true), (3, 6, false)]
tup = intersectionlinecircle(lins[l][1], lins[l][2], centers[cr], rads[cr])
v = [p for p in tup[2:end] if extended || ispointonline(p, lins[l][1], lins[l][2])]
println(rpad(centers[cr], 17), rads[cr], " "^3, rpad(lins[l][1], 21),
rpad(lins[l][2], 19), rpad(!extended, 8), isempty(v) ? "" :
length(v) == 2 ? rpad(v[1], 18) * string(v[2]) : v[1])
end
</lang>{{out}}
<pre>
Center Radius Line P1 Line P2 Segment? Intersect 1 Intersect 2
Point(3.0, -5.0) 3 Point(-10.0, 11.0) Point(10.0, -9.0) false Point(6.0, -5.0) Point(3.0, -2.0)
Point(3.0, -5.0) 3 Point(-10.0, 11.0) Point(-11.0, 12.0) true
Point(3.0, -5.0) 3 Point(3.0, -2.0) Point(7.0, -2.0) true Point(3.0, -2.0)
Point(0.0, 0.0) 4 Point(0.0, -3.0) Point(0.0, 6.0) false Point(0.0, 4.0) Point(0.0, -4.0)
Point(0.0, 0.0) 4 Point(0.0, -3.0) Point(0.0, 6.0) true Point(0.0, 4.0)
Point(4.0, 2.0) 5 Point(6.0, 3.0) Point(10.0, 7.0) false Point(8.0, 5.0) Point(1.0, -2.0)
Point(4.0, 2.0) 5 Point(7.0, 4.0) Point(11.0, 8.0) true Point(8.0, 5.0)
</pre>


=={{header|Perl}}==
=={{header|Perl}}==