Problem:


Projection

For given three points p1, p2, p, find the projection 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 projection 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 0.0000000000
0.0000000000 0.0000000000
1.0000000000 0.0000000000

Sample Input 2

0 0 3 4
1
2 5

Sample Output 2

3.1200000000 4.1600000000



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


Sample output:
-1.0000000000 0.0000000000
0.0000000000 0.0000000000
1.0000000000 0.0000000000

Submission status: Wrong Answer

Solution code:
def dot(a, b):
  return a.real * b.real + a.imag * b.imag

def projection(a, b):
  return a * dot(a, b) / (abs(a) ** 2)

#複素平面の座標はクラスだから直接加算できる
def solve(p0,p1,p2):
  a=p1-p0
  b=p2-p0
  pro=projection(a,b)
  t=p0+pro
  return t

def main():
  x0,y0,x1,y1=map(float,input().split())
  p0=complex(x0,y0)
  p1=complex(x1,y1)
  q=int(input())
  for i in range(q):
    p2=complex(*map(float,input().split()))
    t=solve(p0,p1,p2)
    print('{:.10f}{:.10f}'.format(t.real,t.imag))

main()

Submission status: Wrong Answer

Solution code:
def dot(a, b):
  return a.real * b.real + a.imag * b.imag

def projection(a, b):
  return a * dot(a, b) / (abs(a) ** 2)

#複素平面の座標はクラスだから直接加算できr
def solve(p0,p1,p2):
  a=p1-p0
  b=p2-p0
  pro=projection(a,b)
  t=p0+pro
  return t

def main():
  x0,y0,x1,y1=map(float,input().split())
  p0=complex(x0,y0)
  p1=complex(x1,y1)
  q=int(input())
  for i in range(q):
    p2=complex(*map(float,input().split()))
    t=solve(p0,p1,p2)
    print('{:.10f}{:.10f}'.format(t.real,t.imag))

main()

Submission status: Wrong Answer

Solution code:
def dot(a, b):
  return a.real * b.real + a.imag * b.imag

def projection(a, b):
  return a * dot(a, b) / (abs(a) ** 2)

#複素平面の座標はクラスだから直接加算できr
def solve(p0,p1,p2):
  a=p1-p0
  b=p2-p0
  pro=projection(a,b)
  t=p0+pro
  return t

def main():
  x0,y0,x1,y1=map(float,input().split())
  p0=complex(x0,y0)
  p1=complex(x1,y1)
  q=int(input())
  for i in range(q):
    p2=complex(*map(float,input().split()))
    t=solve(p0,p1,p2)
    print('{:.10f}{:.10f}'.format(t.real,t.imag))
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def dot(a, b):
  return a.real * b.real + a.imag * b.imag

def projection(a, b):
  return a * dot(a, b) / (abs(a) ** 2)

#複素平面の座標はクラスだから直接加算できr
def solve(p0,p1,p2):
  a=p1-p0
  b=p2-p0
  pro=projection(a,b)
  t=p0+pro
  return t

def main():
  x0,y0,x1,y1=map(float,input().split())
  p0=complex(x0,y0)
  p1=complex(x1,y1)
  q=int(input())
  for i in range(q):
    p2=complex(*map(float,input().split()))
    t=solve(p0,p1,p2)
    print("{:.10f}{:.10f}".format(t.real,t.imag))

main()

Submission status: Wrong Answer

Solution code:
a,b,c,d = map(float,input().split())
a = complex(a,b)
b = complex(c,d)
n=int(input())
for i in range(n):
  x,y=map(int,input().split())
  c=complex(x,y)
  q = b-a
  c -= a
  p = a+q*(c/q).conjugate()
  print("{:.10} {:.10}".format(p.real, p.imag))

Submission status: Wrong Answer

Solution code:
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("{:.10} {:.10}".format(p.real, p.imag))

Submission status: Wrong Answer

Solution code:
from sys import stdin
readline = stdin.readline

def main():
    x1, y1, x2, y2 = map(int, readline().split())
    p1, p2 = x1 + y1 * 1j, x2 + y2 * 1j
    unit = (p2 - p1) / abs(p2 - p1)
    q = int(readline())
    for i in range(q):
        xi, yi = map(int, readline().split())
        pi = xi + yi * 1j
        cross = p1 + unit * dot(unit, pi)
        print('{:.10f} {:.10f}'.format(cross.real, cross.imag))
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
main()

Submission status: Wrong Answer

Solution code:
xp0,yp0,xp1,yp1 = map(float,raw_input().split())
q = int(raw_input())
for i in range(q):
    xp2,yp2 = map(float,raw_input().split())
    p0p2 = (xp2-xp0,yp2-xp0)
    p0p1 = (xp1-xp0,yp1-yp0)
    k = ((p0p2[0]*p0p1[0]+p0p2[1]*p0p1[1])/(p0p1[0]**2 + p0p1[1]**2))
    p0T = (k*p0p1[0],k*p0p1[1])
    OT = (xp0+p0T[0],yp0+p0T[1])
    print OT[0],OT[1]

Submission status: Wrong Answer

Solution code:
xp0,yp0,xp1,yp1 = map(float,raw_input().split())
q = int(raw_input())
for i in range(q):
    xp2,yp2 = map(float,raw_input().split())
    p0p2 = (xp2-xp0,yp2-xp0)
    p0p1 = (xp1-xp0,yp1-yp0)
    k = ((p0p2[0]*p0p1[0]+p0p2[1]*p0p1[1])/(p0p1[0]**2 + p0p1[1]**2))
    p0T = (k*p0p1[0],k*p0p1[1])
    OT = (xp0+p0T[0],yp0+p0T[1])
    print "%.10f %.10f" % (OT[0],OT[1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from __future__ import division
import sys
import math

class Vertex(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def length(self):
        return math.sqrt(self.dot(self))

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

    def unitify(self):
        return self / self.length()

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

    def __rmul__(self, c):
        return Vertex(c * self.x, c * self.y)

    def __truediv__(self, c):
        return Vertex(self.x / c, self.y / c)

def main():
    x0, y0, x1, y1 = map(int, raw_input().split())
    v0 = Vertex(x0, y0)
    u1 = Vertex(x1 - x0, y1 - y0).unitify()
    n = input()
    for _ in xrange(n):
        x2, y2 = map(int, raw_input().split())
        v2 = Vertex(x2 - x0, y2 - y0)
        t = v0 + u1.dot(v2) * u1
        print t.x, t.y

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def dot(a,b):
    return a[0]*b[0] + a[1]*b[1]

x1,y1,x2,y2 = [int(i) for i in input().split()] 
q = int(input())
for i in range(q):
    x,y = [int(i) for i in input().split()]
    a = [x2-x1,y2-y1]
    b = [x-x1,y-y1]
    co = dot(a,b) / dot(a,a)
    print(a[0]*co,a[1]*co)

Submission status: Wrong Answer

Solution code:
def dot(a,b):
    return a[0]*b[0] + a[1]*b[1]

x1,y1,x2,y2 = [int(i) for i in input().split()] 
q = int(input())
for i in range(q):
    x,y = [int(i) for i in input().split()]
    a = [x2-x1,y2-y1]
    b = [x-x1,y-y1]
    co = dot(a,b) / dot(a,a)
    print(co)
    print(a[0]*co+x1,a[1]*co+y1)

Submission status: Wrong Answer

Solution code:
# Aizu Problem CGL_1_A: Projection
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


x1, y1, x2, y2 = [int(_) for _ in input().split()]
sx = x2 - x1
sy = y2 - y1
s_sq = sx**2 + sy**2

Q = int(input())
for q in range(Q):
    px, py = [int(_) for _ in input().split()]
    x = x1 + (px * sx + py * sy) * sx / s_sq
    y = y1 + (px * sx + py * sy) * sy / s_sq
    print("%.10f %.10f" % (x, y))

Submission status: Wrong Answer

Solution code:
# Aizu Problem CGL_1_A: Projection
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input3.txt", "rt")


x1, y1, x2, y2 = [int(_) for _ in input().split()]
sx = x2 - x1
sy = y2 - y1
s_sq = sx**2 + sy**2

Q = int(input())
for q in range(Q):
    px, py = [int(_) for _ in input().split()]
    if y1 == y2:
        x = px
        y = y1
    else:
        x = x1 + (px * sx + py * sy) * sx / s_sq
        y = y1 + (px * sx + py * sy) * sy / s_sq
    print("%.10f %.10f" % (x, y))

Submission status: Wrong Answer

Solution code:
# Aizu Problem CGL_1_A: Projection
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input4.txt", "rt")


x1, y1, x2, y2 = [int(_) for _ in input().split()]
sx = x2 - x1
sy = y2 - y1
s_sq = sx**2 + sy**2

Q = int(input())
for q in range(Q):
    px, py = [int(_) for _ in input().split()]
    if y1 == y2:
        x = px
        y = y1
    elif x1 == x2:
        x = x1
        y = py
    else:
        x = x1 + (px * sx + py * sy) * sx / s_sq
        y = y1 + (px * sx + py * sy) * sy / s_sq
    print("%.10f %.10f" % (x, y))

Submission status: Wrong Answer

Solution code:
xp1,yp1,xp2,yp2 = map(int, input().split())
q = int(input())
r = (xp2-xp1,yp2-yp1)

def a(p1,p2):
    s = (p1-xp1,p2-yp1)
    d = r[0] * s[0] + r[1] * s[1]
    a = xp1 + r[0] * d / (r[0]**2 + r[1]**2)
    b = yp1 + r[1] * d / (r[0]**2 + r[1]**2)
    return a,b

for i in range(q):
    x, y = map(int, input().split())
    print(a(x, y))

Submission status: Accepted

Solution code:
def inner_product(v1, v2):
    return v1.real * v2.real + v1.imag * v2.imag


# ????????????b???????????????a??????????????´????????£?°???±???????????????????????????
def projection(a, b):
    return a * inner_product(a, b) / (abs(a) ** 2)


def solve(p0, p1, p2):
    a = p1 - p0
    b = p2 - p0
    pro = projection(a, b)
    t = p0 + pro
    return t


def main():
    x_p0, y_p0, x_p1, y_p1 = map(float, input().split())
    p0 = complex(x_p0, y_p0)
    p1 = complex(x_p1, y_p1)
    q = int(input())
    for _ in range(q):
        p2 = complex(*map(float, input().split()))
        t = solve(p0, p1, p2)
        print("{:.10f} {:.10f}".format(t.real, t.imag))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def dot(a, b):
  return a.real * b.real + a.imag * b.imag

def projection(a, b):
  return a * dot(a, b) / (abs(a) ** 2)

#複素平面の座標はクラスだから直接加算できr
def solve(p0,p1,p2):
  a=p1-p0
  b=p2-p0
  pro=projection(a,b)
  t=p0+pro
  return t

def main():
  x0,y0,x1,y1=map(float,input().split())
  p0=complex(x0,y0)
  p1=complex(x1,y1)
  q=int(input())
  for i in range(q):
    p2=complex(*map(float,input().split()))
    t=solve(p0,p1,p2)
    print("{:.10f} {:.10f}".format(t.real,t.imag))

main()

Submission status: Accepted

Solution code:
x1,y1,x2,y2=map(float,raw_input().split(" "))
if y1==y2 or x1==x2:
    if y1==y2:
        t0=0
        t1=-1
    if x1==x2:
        t1=0
        t0=-1
else:
    t0=(y2-y1)/(x2-x1)
    b0=y1-t0*x1
    t1=-(x2-x1)/(y2-y1)

for i in xrange(int(raw_input())):
    px,py=map(float,raw_input().split(" "))
    if t0==0 or t1==0:
        if t0==0:
            a=1.0
            b=-t0
            c=y1
            d=0.0
            e=-t1
            f=px
        if t1==0:
            a=0.0
            b=-t0
            c=x1
            d=1.0
            e=-t0
            f=py
    else:
        b1=py-t1*px
        a,b,c,d,e,f=1.0,-t0,b0,1.0,-t1,b1
    kouten_y=(c*e-f*b)/(a*e-b*d)
    kouten_x=(a*f-c*d)/(a*e-b*d)
    print "{0:.10f} {1:.10f}".format(kouten_x,kouten_y)

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.readline


def main():
    x1, y1, x2, y2 = map(int, readline().split())
    p1, p2 = x1 + y1 * 1j, x2 + y2 * 1j
    for i in range(int(readline())):
        xi, yi = map(int, readline().split())
        pi = xi + yi * 1j
        cross = p1 + (p2 - p1) * projecter(p2 - p1, pi - p1)
        print('{:.10f} {:.10f}'.format(cross.real, cross.imag))


def projecter(a, b):
    return dot(a, b) / dot(a, a)


def dot(a, b):
    return a.real * b.real + a.imag * b.imag
main()

Submission status: Accepted

Solution code:
# coding: utf-8
xp0,yp0,xp1,yp1 = map(float,raw_input().split())
q = int(raw_input())
for i in range(q):
    xp2,yp2 = map(float,raw_input().split())
    p0p2 = (xp2-xp0,yp2-yp0)
    p0p1 = (xp1-xp0,yp1-yp0)
    k = ((p0p2[0]*p0p1[0]+p0p2[1]*p0p1[1])/(p0p1[0]**2 + p0p1[1]**2))
    p0T = (k*p0p1[0],k*p0p1[1])
    OT = (xp0+p0T[0],yp0+p0T[1])
    print "%.10f %.10f" % (OT[0],OT[1])

Submission status: Accepted

Solution code:
x1, y1, x2, y2 = map(int, input().split())
q = int(input())

p1p2 = complex(x2 - x1, y2 - y1)
d2_p1p2 = abs(p1p2) ** 2
conj_p1p2 = p1p2.conjugate()

while q:
    q -= 1
    x0, y0 = map(int, input().split())
    p1p0 = complex(x0 - x1, y0 - y1)
    t = (conj_p1p2 * p1p0).real / d2_p1p2
    print(x1 + p1p2.real * t, y1 + p1p2.imag * t)

Submission status: Accepted

Solution code:
x0,y0,x1,y1 = map(float,input().split())
n = int(input())
vx1,vy1 = x1-x0,y1-y0

for i in range(n):
    x2,y2 = map(float,input().split())
    vx2,vy2 = x2-x0,y2-y0
    sqr = vx1**2 + vy1**2
    in_p = vx1*vx2 + vy1*vy2
    xt,yt = in_p*vx1/sqr + x0,in_p*vy1/sqr + y0
    print("{0:.10f} {1:.10f}".format(xt,yt))

Submission status: Accepted

Solution code:
from math import sqrt
x1, y1, x2, y2 = map(int, raw_input().split())
dx = x2 - x1
dy = y2 - y1
for i in xrange(input()):
    x, y = map(int, raw_input().split())
    a = float(dx*(x - x1) + dy*(y - y1)) / (dx**2 + dy**2)
    print "%.09f %0.09f" % (x1 + a*dx, y1 + a*dy)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def inner_product(v1, v2):
    return v1.real * v2.real + v1.imag * v2.imag


# ????????????b???????????????a??????????????´????????£?°???±???????????????????????????
def projection(a, b):
    return a * inner_product(a, b) / (abs(a) ** 2)


def solve(p0, p1, p2):
    a = p1 - p0
    b = p2 - p0
    pro = projection(a, b)
    t = p0 + pro
    return t


def main():
    x_p0, y_p0, x_p1, y_p1 = map(float, input().split())
    p0 = complex(x_p0, y_p0)
    p1 = complex(x_p1, y_p1)
    q = int(input())
    for _ in range(q):
        p2 = complex(*map(float, input().split()))
        t = solve(p0, p1, p2)
        print("{:.10f} {:.10f}".format(t.real, t.imag))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x1,y1,x2,y2 = map(float, raw_input().split())

q = int(raw_input())
for _ in xrange(q):
  x,y = map(float, raw_input().split())
  if x1 == x2:
    print "%f %f" % (x1, y)
  elif y1 == y2:
    print "%f %f" % (x, y1)
  else:
    a0 = (y2 - y1) / (x2 - x1)
    b0 = y1 - a0 * x1
    ap = -1 / a0
    bp = y - ap * x
    xe = (bp - b0) / (a0 - ap)
    ye = a0 * xe + b0

    print "%.12f %.12f" % (xe, ye)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
0 0 3 4
1
2 5

output:
3.1200000000 4.1600000000
"""

import sys


def solve(_prj_info):
    for point in _prj_info:
        xp, yp = map(int, point)
        p = xp + yp * 1j
        hypo = p - p1
        prj = p1 + base_vector * project(base_vector, hypo)
        print('{:.10f} {:.10f}'.format(prj.real, prj.imag))

    return _prj_info


def dot(a, b):
    return a.real * b.real + a.imag * b.imag


def project(a, b):
    return dot(a, b) / dot(a, a)


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    base_info = _input[0].split()
    q_num = int(_input[1])
    prj_points = map(lambda x: x.split(), _input[2:])

    x1, y1, x2, y2 = map(int, base_info)
    p1, p2 = x1 + y1 * 1j, x2 + y2 * 1j
    base_vector = p2 - p1
    res = solve(prj_points)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
0 0 3 4
1
2 5

output:
3.1200000000 4.1600000000
"""

import sys


def solve(_prj_info):
    for point in _prj_info:
        xp, yp = map(int, point)
        p = xp + yp * 1j
        hypo = p - p1
        prj = p1 + base * project(base, hypo)
        print('{0:.10f} {1:.10f}'.format(prj.real, prj.imag))

    return _prj_info


def dot(a, b):
    return a.real * b.real + a.imag * b.imag


def project(a, b):
    return dot(a, b) / dot(a, a)


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    base_info = _input[0].split()
    q_num = int(_input[1])
    prj_points = map(lambda x: x.split(), _input[2:])

    x1, y1, x2, y2 = map(int, base_info)
    p1, p2 = x1 + y1 * 1j, x2 + y2 * 1j
    base = p2 - p1
    res = solve(prj_points)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
0 0 3 4
1
2 5

output:
3.1200000000 4.1600000000
"""

import sys


class Segment(object):
    __slots__ = ('source', 'target')

    def __init__(self, source, target):
        self.source = complex(source)
        self.target = complex(target)


def dot(a, b):
    return a.real * b.real + a.imag * b.imag


def project(s, p):
    base_vector = s.target - s.source
    prj_ratio = dot(p - s.source, base_vector) / pow(abs(base_vector), 2)
    return s.source + base_vector * prj_ratio


def solve(_prj_points):
    for point in _prj_points:
        xp, yp = map(int, point)
        p = xp + yp * 1j
        prj = project(prj_segment, p)
        print('{real:.10f} {imag:.10f}'.format(real=prj.real, imag=prj.imag))

    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()

    line = tuple(map(int, _input[0].split()))
    p1, p2 = (x + y * 1j for x, y in zip(line[::2], line[1::2]))
    prj_segment = Segment(p1, p2)

    q_num = int(_input[1])
    prj_points = map(lambda x: x.split(), _input[2:])

    solve(prj_points)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
0 0 3 4
1
2 5

output:
3.1200000000 4.1600000000
"""

import sys


class Segment(object):
    __slots__ = ('source', 'target')

    def __init__(self, source, target):
        self.source = complex(source)
        self.target = complex(target)


def dot(a, b):
    return a.real * b.real + a.imag * b.imag


def project(s, p):
    base_vector = s.target - s.source
    prj_ratio = dot(p - s.source, base_vector) / pow(abs(base_vector), 2)
    return s.source + base_vector * prj_ratio


def solve(_prj_points):
    for point in _prj_points:
        xp, yp = map(int, point)
        p = xp + yp * 1j
        prj = project(prj_segment, p)
        print('{real:.10f} {imag:.10f}'.format(real=prj.real, imag=prj.imag))

    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()

    line = tuple(map(int, _input[0].split()))
    q_num = int(_input[1])
    p1, p2 = (x + y * 1j for x, y in zip(line[::2], line[1::2]))
    prj_segment = Segment(p1, p2)

    prj_points = map(lambda x: x.split(), _input[2:])

    solve(prj_points)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
0 0 3 4
1
2 5

output:
3.1200000000 4.1600000000
"""

import sys
from collections import namedtuple


def dot(a, b):
    return a.real * b.real + a.imag * b.imag


def project(s, p):
    base_vector = s.target - s.source
    prj_ratio = dot(p - s.source, base_vector) / pow(abs(base_vector), 2)
    return s.source + base_vector * prj_ratio


def solve(_prj_points):
    for point in _prj_points:
        xp, yp = map(int, point)
        p = xp + yp * 1j
        prj = project(prj_segment, p)
        print('{real:.10f} {imag:.10f}'.format(real=prj.real, imag=prj.imag))

    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()

    line = tuple(map(int, _input[0].split()))
    q_num = int(_input[1])
    prj_points = map(lambda x: x.split(), _input[2:])

    p1, p2 = (x + y * 1j for x, y in zip(line[::2], line[1::2]))
    Segment = namedtuple('Segment', ('source', 'target'))
    prj_segment = Segment(p1, p2)

    solve(prj_points)

Submission status: Accepted

Solution code:
x1,y1,x2,y2 = map(float,input().split())
q = int(input())
x,y = [],[]
for i in range(q):
    x_t,y_t = map(float,input().split())
    x.append(x_t)
    y.append(y_t)

if x1 == x2:
    for i in range(q):
        print(x1,y[i])
elif y1 == y2:
    for i in range(q):
        print(x[i],y1)
else:
    a = (y2-y1)/(x2-x1)
    b = y1-a*x1
    for i in range(q):
        d = y[i]+(1/a)*x[i]
        px = (d-b)/(a+1/a)
        py = a*px+b
        print(px,py)

Submission status: Accepted

Solution code:
a,b,c,d=map(float,input().split())
c-=a;d-=b
for _ in [0]*int(input()):
    x,y=map(float,input().split())
    x-=a;y-=b
    z=(c*x+d*y)/(c*c+d*d)
    print(a+c*z,b+d*z)

Submission status: Accepted

Solution code:
# Vector class
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def is_orthogonal(self, v):
        if self.x * v.x + self.y * v.y == 0:
            return True
        else:
            return False
    def is_parallel(self, v):
        if self.x * v.y - self.y * v.x == 0:
            return True
        else:
            return False
    def dot_product(self, v):
        return self.x * v.x + self.y * v.y
    def quadrance(self):
        return self.x ** 2 + self. y ** 2
    def mul(self, k):
        return Vector(self.x * k, self.y *k)
    def __add__(self, v):
        return Vector(self.x + v.x, self.y + v.y)
    def __str__(self):
        return "{0:.8f} {1:.8f}".format(self.x, self. y)
    def project_vector(self, v):
        return v.mul(self.dot_product(v) / v.quadrance())

# Acceptance of input and Print the projection point
import sys

file_input = sys.stdin

x_p1, y_p1, x_p2, y_p2 = map(int, file_input.readline().split())
p1_p2 = Vector(x_p2 - x_p1, y_p2 - y_p1)
o_p1 = Vector(x_p1, y_p1)

q = int(file_input.readline())

for line in file_input:
    x_p, y_p = map(int, line.split())
    p1_p = Vector(x_p - x_p1, y_p - y_p1)
    print(o_p1 + p1_p.project_vector(p1_p2))

Submission status: Accepted

Solution code:
import sys
from itertools import starmap
readline = sys.stdin.readline
p0, p1 = starmap(complex, zip(*[map(int, readline().split())] * 2))
n = int(readline())
def cross(a, b):
    return a.real * b.imag - a.imag * b.real
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
def norm(base):
    return base.real * base.real + base.imag * base.imag
def project(p0, p1, p2):
    base = p0 - p1
    r = dot(p2 - p0, base) / norm(base)
    return p0 + base * r
for _ in [0] * n:
    p2 = complex(*[int(i) for i in readline().split()])
    ap = project(p0, p1, p2)
    print(ap.real, ap.imag)

Submission status: Accepted

Solution code:
import sys
from itertools import starmap
readline = sys.stdin.readline
class Segment(object):
    __slots__ = ('fi', 'se')
    def __init__(self, fi, se):
        self.fi = fi
        self.se = se
def cross(a, b):
    return a.real * b.imag - a.imag * b.real
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
def norm(base):
    return abs(base) ** 2
def project(s, p2):
    base = s.fi - s.se
    r = dot(p2 - s.fi, base) / norm(base)
    return s.fi + base * r
s = Segment(*list(starmap(complex, zip(*[map(int, readline().split())] * 2))))
n = int(readline())
for _ in [0] * n:
    p2 = complex(*[int(i) for i in readline().split()])
    ap = project(s, p2)
    print(ap.real, ap.imag)

Submission status: Accepted

Solution code:
import sys
from itertools import starmap
readline = sys.stdin.readline
class Segment(object):
    __slots__ = ('fi', 'se')
    def __init__(self, fi, se):
        self.fi = fi
        self.se = se
def cross(a, b):
    return a.real * b.imag - a.imag * b.real
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
def norm(base):
    return abs(base) ** 2
def project(s, p2):
    base = s.fi - s.se
    r = dot(p2 - s.fi, base) / norm(base)
    return s.fi + base * r
s = Segment(*starmap(complex, zip(*[map(int, readline().split())] * 2)))
n = int(readline())
for _ in [0] * n:
    p2 = complex(*map(float, readline().split()))
    ap = project(s, p2)
    print(ap.real, ap.imag)

Submission status: Accepted

Solution code:
import sys
from itertools import starmap
readline = sys.stdin.readline
class Segment(object):
    __slots__ = ('fi', 'se')
    def __init__(self, fi, se):
        self.fi = fi
        self.se = se
def cross(a, b):
    return a.real * b.imag - a.imag * b.real
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
def norm(base):
    return abs(base) ** 2
def project(s, p2):
    base = s.fi - s.se
    r = dot(p2 - s.fi, base) / norm(base)
    return s.fi + base * r
s = Segment(*(starmap(complex, zip(*[map(int, readline().split())] * 2))))
n = int(readline())
for _ in [0] * n:
    p2 = complex(*map(float, readline().split()))
    ap = project(s, p2)
    print(ap.real, ap.imag)

Submission status: Accepted

Solution code:
import sys
from itertools import starmap
readline = sys.stdin.readline
class Segment(object):
    __slots__ = ('fi', 'se')
    def __init__(self, fi, se):
        self.fi = fi
        self.se = se
def cross(a, b):
    return a.real * b.imag - a.imag * b.real
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
def norm(base):
    return abs(base) ** 2
def project(s, p2):
    base = s.fi - s.se
    r = dot(p2 - s.fi, base) / norm(base)
    return s.fi + base * r
s = Segment(*list(starmap(complex, zip(*[map(int, readline().split())] * 2))))
n = int(readline())
for _ in [0] * n:
    p2 = complex(*map(float, readline().split()))
    ap = project(s, p2)
    print(ap.real, ap.imag)

Submission status: Accepted

Solution code:
import sys
from itertools import starmap
readline = sys.stdin.readline
class Segment(object):
    __slots__ = ('fi', 'se')
    def __init__(self, fi, se):
        self.fi = fi
        self.se = se
def cross(a, b):
    return a.real * b.imag - a.imag * b.real
def dot(a, b):
    return a.real * b.real + a.imag * b.imag
def norm(base):
    return abs(base) ** 2
def project(s, p2):
    base = s.fi - s.se
    r = dot(p2 - s.fi, base) / norm(base)
    return s.fi + base * r
s = Segment(*starmap(complex, zip(*[map(int, readline().split())] * 2)))
n = int(readline())
for _ in [0] * n:
    p2 = complex(*map(int, readline().split()))
    ap = project(s, p2)
    print(ap.real, ap.imag)

Submission status: Accepted

Solution code:
def dot(a,b):
    return a[0]*b[0] + a[1]*b[1]

x1,y1,x2,y2 = [int(i) for i in input().split()] 
q = int(input())
for i in range(q):
    x,y = [int(i) for i in input().split()]
    a = [x2-x1,y2-y1]
    b = [x-x1,y-y1]
    co = dot(a,b) / dot(a,a)
    print(a[0]*co+x1,a[1]*co+y1)

Submission status: Accepted

Solution code:
def dot(a,b):return a[0]*b[0]+a[1]*b[1]
def projection(a,b):return [x*dot(a,b)/dot(a,a) for x in a]

x1,y1,x2,y2 = [int(i) for i in input().split()]
a = [x2-x1,y2-y1]
q = int(input())
for i in range(q):
 x,y = [int(i) for i in input().split()]
 b = [x-x1,y-y1]
 c = projection(a,b)
 print(c[0]+x1,c[1]+y1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A&lang=jp

"""
import sys
from sys import stdin
input = stdin.readline


class Point(object):
    epsilon = 1e-10

    def __init__(self, x=0.0, y=0.0):
        if isinstance(x, tuple):
            self.x = x[0]
            self.y = x[1]
        else:
            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):
        return Point(other * self.x, other * self.y)

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

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

    def __eq__(self, other):
        from math import fabs
        if fabs(self.x - other.x) < Point.epsilon and fabs(self.y - other.y) < Point.epsilon:
            return True
        else:
            return False

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

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


class Vector(Point):
    def __init__(self, x=0.0, y=0.0):
        if isinstance(x, tuple):
            self.x = x[0]
            self.y = x[1]
        elif isinstance(x, Point):
            self.x = x.x
            self.y = x.y
        else:
            self.x = x
            self.y = y

    # ????????????
    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

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

    def __mul__(self, other):
        return Vector(other * self.x, other * self.y)

    def __truediv__(self, other):
        return Vector(other / self.x, other / self.y)

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

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

    @classmethod
    def is_orthogonal(cls, a, b):
        return Vector.dot(a, b) == 0.0

    @classmethod
    def is_parallel(cls, a, b):
        return Vector.cross(a, b) == 0.0


class Segment(object):
    def __init__(self, p1=Point(), p2=Point()):
        if isinstance(p1, Point):
            self.p1 = p1
            self.p2 = p2
        elif isinstance(p1, tuple):
            self.p1 = Point(p1[0], p1[1])
            self.p2 = Point(p2[0], p2[1])

    @classmethod
    def is_orthogonal(cls, s1, s2):
        a = Vector(s1.p2 - s1.p1)
        b = Vector(s2.p2 - s2.p1)
        return Vector.is_orthogonal(a, b)

    @classmethod
    def is_parallel(cls, s1, s2):
        a = Vector(s1.p2 - s1.p1)
        b = Vector(s2.p2 - s2.p1)
        return Vector.is_parallel(a, b)


class Line(Segment):
    pass


class Cirle(object):
    def __init__(self, c=Point(), r=0.0):
        self.c = c
        self.r = r


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


def main(args):
    x_p1, y_p1, x_p2, y_p2 = map(int, input().split())
    q = int(input())
    s = Segment((x_p1, y_p1), (x_p2, y_p2))
    for _ in range(q):
        x, y = map(int, input().split())
        p = Point(x, y)
        result = solve(s, p)
        print('{:.10f} {:.10f}'.format(result.x, result.y))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
xp1,yp1,xp2,yp2 = map(int, input().split())
q = int(input())
r = (xp2-xp1,yp2-yp1)

def a(p1,p2):
    s = (p1-xp1,p2-yp1)
    d = r[0] * s[0] + r[1] * s[1]
    a = xp1 + r[0] * d / (r[0]**2 + r[1]**2)
    b = yp1 + r[1] * d / (r[0]**2 + r[1]**2)
    return str(a),str(b)

for i in range(q):
    x, y = map(int, input().split())
    print(' '.join(a(x, y)))

Submission status: Accepted

Solution code:
def dot(a,b):
    return a[0]*b[0] + a[1]*b[1]

x1, y1, x2, y2 = map(int, input().split()) 
a = [x2-x1, y2-y1]
q = int(input())
for i in range(q):
    x,y = map(int, input().split()) 
    b = [x-x1, y-y1]
    co = dot(a,b) / dot(a,a)
    print(a[0]*co+x1, a[1]*co+y1)

Submission status: Accepted

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):  # cross product
        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 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


if __name__ == '__main__':

    a, b, c, d = map(int, input().split())
    p1 = Vector2(a, b)
    p2 = Vector2(c, d)
    base = p2 - p1
    q = int(input())

    for _ in range(q):
        e, f = map(int, input().split())
        hypo = Vector2(e - a, f - b)
        r = hypo.dot(base) / abs(base)**2
        x = p1 + base * r
        print(x.x, x.y)