Problem:


Reflection

For given three points p1, p2, p, find the reflection point x of p onto p1p2.

Input

xp1 yp1 xp2 yp2
q
xp0 yp0
xp1 yp1
...
xpq&minus;1 ypq&minus;1

In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p.

Output

xt0 yt0
xt1 yt1
...
xtq-1 ytq-1

-->

For each query, print the coordinate of the reflection point x. The output values should be in a decimal fraction with an error less than 0.00000001.

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xi, yi ≤ 10000

p1 and p2 are not identical.

Sample Input 1

0 0 2 0
3
-1 1
0 1
1 1

Sample Output 1

-1.0000000000 -1.0000000000
0.0000000000 -1.0000000000
1.0000000000 -1.0000000000

Sample Input 2

0 0 3 4
3
2 5
1 4
0 3

Sample Output 2

4.2400000000 3.3200000000
3.5600000000 2.0800000000
2.8800000000 0.8400000000



Sample input:
0 0 2 0
3
-1 1
0 1
1 1


Sample output:
-1.0000000000 -1.0000000000
0.0000000000 -1.0000000000
1.0000000000 -1.0000000000

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# CGL_1_B: Points/Vectors - Reflection


def dprod(v1, v2):
    x1, y1 = v1
    x2, y2 = v2
    return x1 * x2 + y1 * y2


def proj(p1, p2, p):
    x1, y1 = p1
    x2, y2 = p2
    x, y = p

    v1 = (x2-x1, y2-y1)
    v2 = (x-x1, y-y1)
    r = dprod(v1, v2) / dprod(v1, v1)

    return (x1 + r*v1[0], y1 + r*v1[1])


def refl(p1, p2, p):
    px, py = p
    hx, hy = proj(p1, p2, p)
    return (px + 2.0*(hx - px), py + 2.0*(hy - py))


def run():
    x1, y1, x2, y2 = [int(i) for i in input().split()]
    q = int(input())

    for _ in range(q):
        x, y = [int(i) for i in input().split()]
        print("{:.10f} {:.10f}".format(*refl((x1, y1), (x2, y2), (x, y))))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
import math
from typing import Union


class Point(object):
    __slots__ = ['x', 'y']

    def __init__(self, x, y):
        self.x = x
        self.y = y

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

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

    def __mul__(self, other: Union[int, float]):
        return Point(self.x * other, self.y * other)

    def __repr__(self):
        return f"({self.x},{self.y})"


class Segment(object):
    __slots__ = ['pt1', 'pt2', 'vector']

    def __init__(self, x: Point, y: Point):
        self.pt1 = x
        self.pt2 = y
        self.vector = self.pt2 - self.pt1

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

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

    def norm(self):
        return pow(self.vector.x, 2) + pow(self.vector.y, 2)

    def abs(self):
        return math.sqrt(self.norm())

    def projection(self, pt: Point)-> Point:
        t = self.dot(Segment(self.pt1, pt)) / pow(self.abs(), 2)
        return Point(self.pt1.x + t * self.vector.x, self.pt1.y + t * self.vector.y)

    def reflection(self, pt: Point) -> Point:
        return self.projection(pt) * 2 - pt

    def __repr__(self):
        return f"{self.pt1},{self.pt2},{self.vector}"


def main():
    p0_x, p0_y, p1_x, p1_y = map(int, input().split())
    seg = Segment(Point(p0_x, p0_y), Point(p1_x, p1_y))
    num_query = int(input())
    for i in range(num_query):
        pt_x, pt_y = map(int, input().split())
        reflection = seg.reflection(Point(pt_x, pt_y))
        print("{:.10f} {:.10f}".format(reflection.x, reflection.y))
    return


main()

Submission status: Accepted

Solution code:
sx, sy, ex, ey = (int(n) for n in input().split(" "))
trial = int(input())
for _ in range(trial):
    x, y = (int(n) for n in input().split(" "))
    if sx == ex:
        print(-x, y)
    elif sy == ey:
        print(x, -y)
    else:
        grad = (ey - sy) / (ex - sx)
        y_seg = sy - (grad * sx)
        per_grad = -1 / grad
        per_y_seg = y - (per_grad * x)
        ans_x = (y_seg - per_y_seg) / (-1 * (grad - per_grad))
        ans_y = per_grad * ans_x + per_y_seg
        diff_x = ans_x - x
        diff_y = diff_x * per_grad
        print(ans_x + diff_x, ans_y + diff_y)

Submission status: Accepted

Solution code:
#○要するに共役複素数とは，それぞれの複素数（a+bi）の実部（a）を変えずに虚部（b）の符号だけを変えたものです

a, b,c,d = map(float,input().split())
a = complex(a,b)
b = complex(c,d)

for _ in [0] *int(input()):
  c = complex(*map(float, input().split()))
  q = b-a
  c -= a
  p = a+q*(c/q).conjugate()
  print(p.real, p.imag)

Submission status: Accepted

Solution code:
import math

def equals(a, b):
    return abs(a - b) < 1e-10


class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __add__(self, p):
        return Point(self.x + p.x, self.y + p.y)

    def __sub__(self, p):
        return Point(self.x - p.x, self.y - p.y)

    def __mul__(self, a):
        return Point(self.x * a, self.y * a)

    def __rmul__(self, a):
        return self * a

    def __truediv__(self, a):
        return Point(self.x / a, self.y / a)

    def norm(self):
        return self.x * self.x + self.y * self.y

    def abs(self):
        return math.sqrt(self.norm())

    def __lt__(self, p):
        if self.x != p.x:
            return self. x < p.x
        else:
            return self.y < p.y

    def __eq__(self, p):
        return equals(self.x, p.x) and equals(self.y, p.y)


class Segment:
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2


def dot(a, b):
    return a.x * b.x + a.y * b.y


def cross(a, b):
    return a.x * b.y - a.y * b.x


def project(s, p):
    base = s.p2 - s.p1
    r = dot(p - s.p1, base) / base.norm()
    return s.p1 + base * r


def reflect(s, p):
    return p + (project(s, p) - p) * 2


if __name__ == '__main__':
    x1, y1, x2, y2 = [int(v) for v in input().split()]
    s = Segment(Point(x1, y1), Point(x2, y2))
    q = int(input())
    ans = []
    for i in range(q):
        x, y = [int(v) for v in input().split()]
        p = Point(x, y)
        ans.append(reflect(s, p))

    for v in ans:
        print('{0:.10f} {1:.10f}'.format(v.x, v.y))

Submission status: Runtime Error

Solution code:
"""
    p1p2: (y-y1)(x2-x1)=(x-x1)(y2-y1)
    p vertical: (y2-y1)(y-py)+(x2-x1)(x-px)=0
    y=(x-x1)(y2-y1)/(x2-x1)-y1
    (y2-y1)((x-x1)(y2-y1)/(x2-x1)-y1-py)+(x2-x1)(x-px)=0
    (x-x1)(y2-y1)^2/(x2-x1)+(y2-y1)(y1+py)+(x2-x1)x-(x2-x1)px=0
    x((y2-y1)^2/(x2-x1)+(x2-x1))-x1((y2-y1)^2/(x2-x1)-(x2-x1)px=0
    x=((x2-x1)px+x1((y2-y1)^2/(x2-x1))/((y2-y1)^2/(x2-x1)+(x2-x1))
"""

points=[]
points=int(input().split())
p1,q1,p2,q2=points[0:3]

def search_x(x,y):
    return ((q2-q1)*x+q1*((q2-q1)*(q2-q1))/(p2-p1))/((q2-q1)*(q2-q1)/(p2-p1)+(p2-p1))

n=int(input())
for i in range(n):
    point=[]
    point=int(input().split())
    x=search_x(point[0],point[1])
    y=(x-q1)*(q2-q1)/(p2-p1)-q1
    print('{} {}'.format(x,y))

Submission status: Runtime Error

Solution code:
# coding=utf-8
import numpy as np
from math import sqrt


def vector_abs(vect):
    return sqrt(sum([element**2 for element in vect]))


def direction_unit_vector(p_from, p_to):
    d_vector = p_to - p_from
    d_u_vector = d_vector/vector_abs(d_vector)
    return d_u_vector


def projection(origin, line_from, line_to):
    direction_unit = direction_unit_vector(line_from, line_to)
    origin_d_vector = origin - line_from
    inject_dist = np.inner(direction_unit, origin_d_vector)
    on_line_vect = inject_dist*direction_unit
    return on_line_vect + line_from


def reflection(origin, line_from, line_to):
    mid_point = projection(origin, line_from, line_to)
    reflected = origin + 2 * (mid_point - origin)
    return reflected


if __name__ == '__main__':
    xy_list = np.array(list(map(int, input().split())))
    p1_list = xy_list[:2]
    p2_list = xy_list[2:]
    Q = int(input())

    for i in range(Q):
        p_list = np.array(list(map(int, input().split())))
        x_list = reflection(p_list, p1_list, p2_list)
        print(' '.join(map(str, x_list)))

Submission status: Runtime Error

Solution code:
# coding=utf-8
from math import sqrt, fsum


def inner_product(vect1, vect2):
    return math.fsum([(v1_el*v2_el) for v1_el, v2_el in zip(vect1, vect2)])


def vector_abs(vect):
    return sqrt(sum([element**2 for element in vect]))


def direction_unit_vector(p_from, p_to):
    d_vector = [(xt - xf) for xt, xf in zip(p_to, p_from)]
    d_u_vector = [element/vector_abs(d_vector) for element in d_vector]
    return d_u_vector


def projection(origin, line_from, line_to):
    direction_unit = direction_unit_vector(line_from, line_to)
    origin_d_vector = [(org-lf) for org, lf in zip(origin, line_from)]
    inject_dist = inner_product(direction_unit, origin_d_vector)
    on_line_vect = [inject_dist*element for element in direction_unit]
    return [olv_el + lf_el for olv_el, lf_el in zip(on_line_vect, line_from)]


def reflection(origin, line_from, line_to):
    mid_point = projection(origin, line_from, line_to)
    direction = [2*(mp_el - or_el) for mp_el, or_el in zip(mid_point, origin)]
    reflected = [(dr_el + or_el) for dr_el, or_el in zip(direction, origin)]
    return reflected


if __name__ == '__main__':
    xy_list = list(map(int, input().split()))
    p1_list = xy_list[:2]
    p2_list = xy_list[2:]
    Q = int(input())

    for i in range(Q):
        p_list = list(map(int, input().split()))
        x_list = reflection(p_list, p1_list, p2_list)
        print(' '.join(map(str, x_list)))