Problem:


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

Cross Points of a Circe and a Line

  For given a circle $c$ and a line $l$, print the coordinates of the cross points of them.

Input

  The input is given in the following format.

$cx\; cy\; r$

$q$

$Line_1$

$Line_2$

:

$Line_q$

  In the first line, the center coordinate of the circle and its radius are given by $cx$, $cy$ and $r$. In the second line, the number of queries $q$ is given.

  In the following $q$ lines, as queries, $Line_i$ are given ($1 \leq i \leq q$) in the following format.

$x_1\; y_1\; x_2\; y_2$

  Each line is represented by two points $p1$ and $p2$ which the line crosses. The coordinate of $p1$ and $p2$ are given by ($x1$, $y1$) and ($x2$, $y2$) respectively. All input values are given in integers.

Output

  For each query, print the coordinates of the cross points 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

$p1$ and $p2$ are different

The circle and line have at least one cross point

$1 \leq q \leq 1,000$

$-10,000 \leq cx, cy, x1, y1, x2, y2 \leq 10,000$

$1 \leq r \leq 10,000$

Sample Input and Output

Sample Input

2 1 1
2
0 1 4 1
3 0 3 3

Sample Output

1.00000000 1.00000000 3.00000000 1.00000000
3.00000000 1.00000000 3.00000000 1.00000000



Sample input:
2 1 1
2
0 1 4 1
3 0 3 3


Sample output:
1.00000000 1.00000000 3.00000000 1.00000000
3.00000000 1.00000000 3.00000000 1.00000000

Submission status: Runtime Error

Solution code:
# coding: utf-8
import math

def solve(a,b,c):
    # square equation ax**2 + 2*b*x + c == 0
    return (-b - math.sqrt(b**2 - a*c)) / a, (-b + math.sqrt(b**2 - a*c)) / a

x0, y0, r = map(float, raw_input().split())
q = int(raw_input())
for i in range(q):
    x1, y1, x2, y2 = map(float, raw_input().split())
    # line: y = mx + n
    # (x - x0)**2 + (y - y0)**2 == r**2
    # erase y
    # cross points A and B
    
    if x1 - x2 == 0: # x1 == x2
        XA = x1
        XB = x2
        YA = y0 + math.sqrt(r**2 - (x1 - x0)**2)
        YB = y0 - math.sqrt(r**2 - (x1 - x0)**2)

        if XA <= 1e-6: XA = 0
        if YA <= 1e-6: YA = 0
        if XB <= 1e-6: XB = 0
        if YB <= 1e-6: YB = 0
            
        print "%.6f %.6f %.6f %.6f" % (XA, YA, XB, YB)
    else:
        m = (y1 - y2) / (x1 - x2)
        n = (2*x1*y1 - x2*y1 - x1*y2) / (x1 - x2)

        a_solve = m**2 + 1
        b_solve = m*(n - y0) - x0
        c_solve = x0**2 + (n - y0)**2 - r**2
    
        XA, XB = solve(a_solve, b_solve, c_solve)
        YA = m * XA + n
        YB = m * XB + n
        if XA <= 1e-6: XA = 0
        if YA <= 1e-6: YA = 0
        if XB <= 1e-6: XB = 0
        if YB <= 1e-6: YB = 0
        
        if XA == XB and YA > YB:
            print "%.6f %.6f %.6f %.6f" % (XB, YB, XA, YA)
        else:
            print "%.6f %.6f %.6f %.6f" % (XA, YA, XB, YB)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import math

def solve(a,b,c):
    # square equation ax**2 + 2*b*x + c == 0
    return (-b - math.sqrt(b**2 - a*c)) / a, (-b + math.sqrt(b**2 - a*c)) / a

x0, y0, r = map(float, raw_input().split())
q = int(raw_input())
for i in range(q):
    x1, y1, x2, y2 = map(float, raw_input().split())
    # line: y = mx + n
    # (x - x0)**2 + (y - y0)**2 == r**2
    # erase y
    # cross points A and B
    
    if x1 - x2 == 0: # x1 == x2
        XA = x1
        XB = x2
        YA = y0 + math.sqrt(r**2 - (x1 - x0)**2)
        YB = y0 - math.sqrt(r**2 - (x2 - x0)**2)

        if XA <= 1e-6: XA = 0
        if YA <= 1e-6: YA = 0
        if XB <= 1e-6: XB = 0
        if YB <= 1e-6: YB = 0
            
        print "%.6f %.6f %.6f %.6f" % (XA, YA, XB, YB)
    else:
        m = (y1 - y2) / (x1 - x2)
        n = (2*x1*y1 - x2*y1 - x1*y2) / (x1 - x2)

        a_solve = m**2 + 1
        b_solve = m*(n - y0) - x0
        c_solve = x0**2 + (n - y0)**2 - r**2
    
        XA, XB = solve(a_solve, b_solve, c_solve)
        YA = m * XA + n
        YB = m * XB + n
        if XA <= 1e-6: XA = 0
        if YA <= 1e-6: YA = 0
        if XB <= 1e-6: XB = 0
        if YB <= 1e-6: YB = 0
        
        if XA == XB and YA > YB:
            print "%.6f %.6f %.6f %.6f" % (XB, YB, XA, YA)
        else:
            print "%.6f %.6f %.6f %.6f" % (XA, YA, XB, YB)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import collections
import math


class Vector2(collections.namedtuple("Vector2", ["x",  "y"])):

    def __add__(self, other):
        return Vector2(self.x + other.x, self.y + other.y)

    def __sub__(self, other):
        return Vector2(self.x - other.x, self.y - other.y)

    def __mul__(self, scalar):
        return Vector2(self.x * scalar, self.y * scalar)

    def __neg__(self):
        return Vector2(-self.x, -self.y)

    def __pos__(self):
        return Vector2(+self.x, +self.y)

    def __abs__(self):  # norm
        return math.sqrt(float(self.x * self.x + self.y * self.y))

    def __truediv__(self, scalar):
        return Vector2(self.x/scalar, self.y/scalar)

    def abs2(self):
        return float(self.x * self.x + self.y * self.y)

    def dot(self, other):  # dot product
        return self.x * other.x + self.y * other.y

    def cross(self, other):  # cross product
        return self.x * other.y - self.y * other.x


def getDistanceSP(segment, point):
    p = point
    p1, p2 = segment
    if (p2 - p1).dot(p - p1) < 0:
        return abs(p - p1)
    if (p1 - p2).dot(p - p2) < 0:
        return abs(p - p2)
    return abs((p2 - p1).cross(p - p1)) / abs(p2 - p1)


def getDistance(s1, s2):
    a, b = s1
    c, d = s2
    if intersect(s1, s2):  # intersect
        return 0
    return min(getDistanceSP(s1, c), getDistanceSP(s1, d), getDistanceSP(s2, a), getDistanceSP(s2, b))


def ccw(p0, p1, p2):
    a = p1 - p0
    b = p2 - p0
    if a.cross(b) > 0:
        return 1  # COUNTER_CLOCKWISE
    elif a.cross(b) < 0:
        return -1  # CLOCKWISE
    elif a.dot(b) < 0:
        return 2  # ONLINE_BACK
    elif abs(a) < abs(b):
        return -2  # ONLINE_FRONT
    else:
        return 0  # ON_SEGMENT


def intersect(s1, s2):
    a, b = s1
    c, d = s2
    return ccw(a, b, c) * ccw(a, b, d) <= 0 and ccw(c, d, a) * ccw(c, d, b) <= 0


def project(l, p):
    p1, p2 = l
    base = p2 - p1
    hypo = p - p1
    return p1 + base * (hypo.dot(base) / abs(base)**2)


class Circle():
    def __init__(self, c, r):
        self.c = c
        self.r = r


def getCrossPoints(c, l):
    pr = project(l, c.c)
    p1, p2 = l
    e = (p2 - p1) / abs(p2 - p1))
    base = math.sqrt(c.r*c.r - (pr - c.c).abs2())
    return [pr + e * base, pr - e * base]

if __name__ == '__main__':

    a, b, r = map(int, input().split())
    c = Circle(Vector2(a, b), r)
    n = int(input())
    for _ in range(n):
        ps = list(map(int, input().split()))
        l = [Vector2(ps[0], ps[1]), Vector2(ps[2], ps[3])]

        ans = getCrossPoints(c, l)
        ans = sorted(ans, key=lambda x: (x.x, x.y))
        print(ans[0].x, ans[0].y, ans[1].x, ans[1].y)