Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Cross Points of Circles

For given two circles $c1$ and $c2$, print the coordinates of the cross points of them.

Input

The input is given in the following format.

$c1x\; c1y\; c1r$

$c2x\; c2y\; c2r$

$c1x$, $c1y$ and $c1r$ represent the coordinate and radius of the first circle.
$c2x$, $c2y$ and $c2r$ represent the coordinate and radius of the second circle.
All input values are given in integers.

Output

Print the coordinates ($x1$, $y1$) and ($x2$, $y2$) of the cross points $p1$ and $p2$ respectively in the following rules.

If there is one cross point, print two coordinates with the same values.

Print the coordinate with smaller $x$ first. In case of a tie, print the coordinate with smaller $y$ first.

The output values should be in a decimal fraction with an error less than 0.000001.

Constraints

The given circle have at least one cross point and have different center coordinates.

$-10,000 \leq c1x, c1y, c2x, c2y \leq 10,000$

$1 \leq c1r, c2r \leq 10,000$

Sample Input and Output

Sample Input 1

0 0 2
2 0 2

Sample Output 1

1.00000000 -1.73205080 1.00000000 1.73205080

Sample Input 2

0 0 2
0 3 1

Sample Output 2

0.00000000 2.00000000 0.00000000 2.00000000

Solution in Scala (judge verdict: Accepted)

import scala.io.StdIn

object Main {

  case class Point(x: Double = 0, y: Double = 0) {

    def +(p: Point): Point = Point(this.x + p.x, this.y + p.y)

    def -(p: Point): Point = Point(this.x - p.x, this.y - p.y)

    def *(a: Double): Point = Point(this.x * a, this.y * a)

    def /(a: Double): Point = {
      assert(a != 0)
      Point(this.x / a, this.y / a)
    }

    def norm(): Double = this.x * this.x + this.y * this.y

    def abs(): Double = scala.math.sqrt(norm())

    def <(p: Point): Boolean = this.x < p.x && this.y < p.y


    def ==(p: Point): Boolean = scala.math.abs(this.x - p.x) < Cgl0A.EPS &&
      scala.math.abs(this.y - p.y) < Cgl0A.EPS


  }

  case class Circle(center: Point, r: Int)

  case class Segment(p1: Point, p2: Point)

  case class Line(p1: Point, p2: Point)

  type GeoVector = Point

  object Cgl0A {
    val EPS: Double = scala.math.pow(10, -10)

    // 内積
    def dot(a: Point, b: Point): Double = a.x * b.x + a.y * b.y

    def isOrthogonal(a: GeoVector, b: GeoVector): Boolean = dot(a, b) == 0.0

    def isOrthogonal(a1: Point, a2: Point, b1: Point, b2: Point): Boolean = {
      isOrthogonal(a1 - a2, b1 - b2)
    }

    def isOrthogonal(s1: Segment, s2: Segment): Boolean = {
      dot(s1.p2 - s1.p1, s2.p2 - s2.p1) == 0.0
    }

    // 外積
    def cross(a: Point, b: Point): Double = a.x * b.y - a.y * b.x

    def isParallel(a: GeoVector, b: GeoVector): Boolean = cross(a, b) == 0.0

    def isParallel(a1: Point, a2: Point, b1: Point, b2: Point): Boolean = {
      isParallel(a1 - a2, b1 - b2)
    }

    def isParallel(s1: Segment, s2: Segment): Boolean = {
      cross(s1.p2 - s1.p1, s2.p2 - s2.p1) == 0.0
    }

    def project(s: Segment, p: Point): Point = {
      val base: GeoVector = s.p2 - s.p1
      val r: Double = Cgl0A.dot(p - s.p1, base) / base.norm()
      s.p1 + base * r
    }

    def reflect(s: Segment, p: Point): Point = {
      p + (project(s, p) - p) * 2.0
    }

    def getDistance(a: Point, b: Point): Double = (a - b).abs()

    def getDistanceLP(l: Line, a: Point): Double = {
      scala.math.abs(cross(l.p2 - l.p1, a - l.p1) / (l.p2 - l.p1).abs())
    }

    def getDistanceSP(s: Segment, a: Point): Double = {
      if (dot(s.p2 - s.p1, a - s.p1) < 0.0) (a - s.p1).abs()
      else if (dot(s.p1 - s.p2, a - s.p2) < 0.0) (a - s.p2).abs()
      else getDistanceLP(Line(s.p1, s.p2), a)
    }

    def getDistance(s1: Segment, s2: Segment): Double = {
      if (intersect(s1, s2)) {
        0.0
      }
      else {
        math.min(math.min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)),
          math.min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2)))
      }
    }

    val COUNTER_CLOCKWISE: Int = 1
    val CLOCKWISE: Int = -1
    val ONLINE_BACK: Int = 2
    val ONLINE_FRONT: Int = -2
    val ON_SEGMENT: Int = 0

    def ccw(p0: Point, p1: Point, p2: Point): Int = {
      val a: GeoVector = p1 - p0
      val b: GeoVector = p2 - p0

      if (cross(a, b) > EPS) COUNTER_CLOCKWISE
      else if (cross(a, b) < -1 * EPS) CLOCKWISE
      else if (dot(a, b) < -1 * EPS) ONLINE_BACK
      else if (a.norm() < b.norm()) ONLINE_FRONT
      else ON_SEGMENT

    }

    def intersect(p1: Point, p2: Point, p3: Point, p4: Point): Boolean = {
      Cgl0A.ccw(p1, p2, p3) * Cgl0A.ccw(p1, p2, p4) <= 0 &&
        Cgl0A.ccw(p3, p4, p1) * Cgl0A.ccw(p3, p4, p2) <= 0
    }

    def intersect(s1: Segment, s2: Segment): Boolean = {
      Cgl0A.intersect(s1.p1, s1.p2, s2.p1, s2.p2)
    }

    def getCrossPoint(s1: Segment, s2: Segment): Point = {
      val base: GeoVector = s2.p2 - s2.p1
      val d1 = scala.math.abs(cross(base, s1.p1 - s2.p2))
      val d2 = scala.math.abs(cross(base, s1.p2 - s2.p1))
      val t = d1 / (d1 + d2)
      s1.p1 + (s1.p2 - s1.p1) * t
    }

    def getCrossPoint(c: Circle, l: Line): Array[Point] = {
      val pr: GeoVector = project(Segment(l.p1, l.p2), c.center)
      val e: GeoVector = (l.p2 - l.p1) / (l.p2 - l.p1).abs()
      val base: Double = scala.math.sqrt(c.r * c.r - (pr - c.center).norm())
      Array(pr + e * base, pr - e * base).sortBy(p => (p.x, p.y))
    }

    // arq tangent
    def arg(p: GeoVector): Double = scala.math.atan2(p.y, p.x)

    // 
    def polar(a: Double, r: Double): GeoVector = {
      // x * 角度ラジアン
      Point(scala.math.cos(r) * a, scala.math.sin(r) * a)
    }

    def getCrossPoint(c1: Circle, c2: Circle): Array[Point] = {
      val d: Double = (c1.center - c2.center).abs()
      val cos: Double = (c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)
      val a: Double = scala.math.acos(cos) // radian
      
      val t: Double = arg(c2.center - c1.center) // radian
      Array(c1.center + polar(c1.r, t + a),
        c1.center + polar(c1.r, t - a)).sortBy(p => (p.x, p.y))

    }

  }


  def main(args: Array[String]): Unit = {
    val ci1 = StdIn.readLine().split(' ').map(_.toInt)
    val circle1 = Circle(Point(ci1(0), ci1(1)), ci1(2))
    val ci2 = StdIn.readLine().split(' ').map(_.toInt)
    val circle2 = Circle(Point(ci2(0), ci2(1)), ci2(2))
    val r = Cgl0A.getCrossPoint(circle1, circle2)

    println(f"${r(0).x}%.9f ${r(0).y}%.9f ${r(1).x}%.9f ${r(1).y}%.9f")

  }

}