Line circle intersection: Difference between revisions

m
syntax highlighting fixup automation
(Added Rust solution)
m (syntax highlighting fixup automation)
Line 24:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">data := [[[3, -5], 3, [-10, 11], [10, -9], 0]
, [[3, -5], 3, [-10, 11], [-11, 12], 1]
, [[3, -5], 3, [3, -2], [7, -2], 1]
Line 75:
sgn(x){
return x<0?-1:1
}</langsyntaxhighlight>
{{out}}
<pre>Center Rad P1 P2 Segment intersect 1 Intersect 2
Line 88:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdbool.h>
#include <stdio.h>
Line 252:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The intersection points (if any) between:
Line 275:
=={{header|C++}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <utility>
#include <vector>
Line 447:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The intersection points (if any) between:
Line 470:
=={{header|C#}}==
{{trans|C++}}
<langsyntaxhighlight Csharplang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 617:
public override string ToString() => $"{{ C:{Center}, R:{Radius} }}";
}
}</langsyntaxhighlight>
{{out}}
<pre>Circle: { C:(3, -5), R:3 }
Line 642:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.format;
import std.math;
import std.stdio;
Line 826:
writeln(" a segment starting at ", p1, " and ending at ", p2, " is/are:");
writeln(" ", intersects(p1, p2, cp, r, true));
}</langsyntaxhighlight>
{{out}}
<pre>The intersection points (if any) between:
Line 848:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 977:
fmt.Println("\n a segment starting at (7, 4) and ending at (11, 8) is/are:")
fmt.Println(" ", intersects(point{7, 4}, point{11, 8}, cp, r, true))
}</langsyntaxhighlight>
 
{{out}}
Line 1,012:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Tuple.Curry
 
main :: IO ()
Line 1,086:
sgn x
| 0 > x = -1
| otherwise = 1</langsyntaxhighlight>
{{out}}
<pre>Intersection: Circle (3.0,-5.0) 3.0 and Line (-10.0,11.0) (10.0,-9.0): [(3.0,-2.0),(6.0,-5.0)]
Line 1,110:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
import java.awt.geom.*;
 
Line 1,216:
return str.toString();
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,252:
=={{header|Julia}}==
Uses the circles and points from the Go example.
<langsyntaxhighlight lang="julia">using Luxor
 
const centers = [Point(3, -5), Point(0, 0), Point(4, 2)]
Line 1,272:
length(v) == 2 ? rpad(v[1], 18) * string(v[2]) : v[1])
end
</langsyntaxhighlight>{{out}}
<pre>
Center Radius Line P1 Line P2 Segment? Intersect 1 Intersect 2
Line 1,286:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">import kotlin.math.absoluteValue
import kotlin.math.sqrt
 
Line 1,441:
println(" a segment starting at $p1 and ending at $p2 is/are:")
println(" ${intersects(p1, p2, cp, r, true)}")
}</langsyntaxhighlight>
{{out}}
<pre>The intersection points (if any) between:
Line 1,464:
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">EPS = 1e-14
 
function pts(p)
Line 1,621:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>The intersection points (if any) between:
Line 1,643:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">LineCircleIntersections[p1_, p2_, c_, r_, type_] := RegionIntersection[Circle[c, r], type[{p1, p2}]]
LineCircleIntersections[{-1, 1}, {1, 1}, {0, 0}, 1, Line]
LineCircleIntersections[{-1, 0}, {2, 0.4}, {0, 0}, 1, Line]
LineCircleIntersections[{-1.5, 0}, {-2, 0.4}, {0, 0}, 1, Line]
LineCircleIntersections[{-1.5, 0}, {-2, 0.4}, {0, 0}, 1, InfiniteLine]</langsyntaxhighlight>
{{out}}
<pre>Point[{{0,1}}]
Line 1,656:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
const Eps = 1e-14
Line 1,760:
echo " ", intersects((6.0, 3.0), (10.0, 7.0), cp, r, false)
echo "\n a segment starting at (7, 4) and ending at (11, 8) is/are:"
echo " ", intersects((7.0, 4.0), (11.0, 8.0), cp, r, true)</langsyntaxhighlight>
 
{{out}}
Line 1,793:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,833:
say 'Solutions: ' . (@solution > 1 ? join ', ', map { '('. join(',', rnd @$_) .')' } @solution : 'None');
say '';
}</langsyntaxhighlight>
{{out}}
<pre>For input: (-10,11), (10,-9), (3,-5), 3
Line 1,859:
{{trans|Go}}
{{trans|zkl}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">epsilon</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e-14</span> <span style="color: #000080;font-style:italic;">-- say</span>
Line 1,957:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,972:
(formerly Perl 6)
Extend solution space to 3D. Reference: this [https://stackoverflow.com/questions/1073336/ SO question and answers]
<syntaxhighlight lang="raku" perl6line>sub LineCircularOBJintersection(@P1, @P2, @Centre, \Radius) {
my @d = @P2 »-« @P1 ; # d
my @f = @P1 »-« @Centre ; # c
Line 2,008:
say "For data set: ", $_;
say "Solution(s) is/are: ", @solution.Bool ?? @solution !! "None";
}</langsyntaxhighlight>
{{out}}
<pre>For data set: [(-10 11) (10 -9) (3 -5) 3]
Line 2,031:
The formulae used for this REXX version were taken from the MathWorld
webpage: &nbsp; [https://mathworld.wolfram.com/Circle-LineIntersection.html circle line intersection].
<langsyntaxhighlight lang="rexx">/*REXX program calculates where (or if) a line intersects (or tengents) a cirle. */
/*───────────────────────────────────── line= x1,y1 x2,y2; circle is at 0,0, radius=r*/
parse arg x1 y1 x2 y2 cx cy r . /*obtain optional arguments from the CL*/
Line 2,073:
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ %2
do j=0 while h>9; m.j= h; h= h%2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g= (g+x/g) *.5; end /*k*/; return g</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,083:
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">EPS = 1e-14
 
def sq(x)
Line 2,222:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>The intersection points (if any) between:
Line 2,245:
=={{header|Rust}}==
{{trans|C++}}
<langsyntaxhighlight lang="rust">
use assert_approx_eq::assert_approx_eq;
 
Line 2,422:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,434:
=={{header|Swift}}==
{{trans|Java}}
<langsyntaxhighlight lang="swift">import Foundation
import CoreGraphics
 
Line 2,522:
center: center, radius: radius,
segment: true)
print(" \(toString(points: points))")</langsyntaxhighlight>
 
{{out}}
Line 2,558:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Structure Point
Line 2,736:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Circle: { C:(3, -5), R:3 }
Line 2,761:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Tuple
 
var Point = Tuple.create("Point", ["x", "y"])
Line 2,893:
p2 = Point.new(11, 8)
System.print(" a segment starting at %(p1) and ending at %(p2) is/are:")
System.print(" %(intersects.call(p1, p2, cp, r, true))")</langsyntaxhighlight>
 
{{out}}
Line 2,919:
=={{header|zkl}}==
{{trans|Go}}
<langsyntaxhighlight lang="zkl">const EPS=1e-14; // a close-ness to zero
// p1,p2 are (x,y), circle is ( (x,y),r )
fcn intersectLineCircle(p1,p2, circle, segment=False) // assume line
Line 2,967:
if(d==0) return( T( T(ux,uy) ) );
return( T(ux,uy), T(vx,vy) )
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">circle:=T( T(3,-5),3 ); p1,p2 := T(-10,11), T( 10,-9);
println("Circle @ ",circle); lcpp(p1,p2,circle);
p2:=T(-11,12); lcpp(p1,p2,circle,True);
Line 2,987:
.fmt(segment and "Segment" or "Line ",
p1,p2,intersectLineCircle(p1,p2, circle,segment)));
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits