Problem:
Projection For given three points p1, p2, p , find the projection point x of p onto p1p2 .   Input  x p1  y p1  x p2  y p2  q  x p 0  y p 0  x p 1  y p 1 ... x p q−1  y p q−1  In the first line, integer coordinates of p1 and p2 are given. Then, q queries are given for integer coordinates of p . Output 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 ≤ x i , y i ≤ 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

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



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()

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):

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])

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):


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



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()

Solution (Python):
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)

Solution (Python):



"""
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)

Solution (Python):



"""
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)

Solution (Python):



"""
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)

Solution (Python):



"""
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)

Solution (Python):



"""
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)

Solution (Python):



"""
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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())


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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

"""
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:])

Solution (Python):
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)))

Solution (Python):
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)

Solution (Python):


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):  
        return math.sqrt(float(self.x * self.x + self.y * self.y))

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

    def cross(self, other):  
        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)

Solution (Python):


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):  
        return math.sqrt(float(self.x * self.x + self.y * self.y))

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

    def cross(self, other):  
        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)
        x = p1 + base * (hypo.dot(base) / abs(base)**2)

        print(x.x, x.y)

Solution (Python):
import math

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

class Vector():
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.abs = math.sqrt(x*x + y*y)

    def constant_multipled(self, c):
        return Vector(self.x*c, self.y*c)

    def inner_product(self, vec):
        return self.x*vec.x + self.y*vec.y

x1, y1, x2, y2 = list(map(int, input().split(' ')))
start = Point(x1, y1)
base_vec = Vector(x2-x1, y2-y1)
q = int(input())
vectors = []
for i in range(q):
    x, y = list(map(int, input().split(' ')))
    vectors.append(Vector(x-start.x, y-start.y))

for vec in vectors:
    if vec.abs == 0:
        ans_x, ans_y = 0, 0
        print('%.8f'%ans_x, '%.8f'%ans_y)
        continue
    cos = base_vec.inner_product(vec)/(base_vec.abs*vec.abs)
    b_cos = vec.abs*cos
    vec_x = base_vec.constant_multipled(b_cos/base_vec.abs)
    ans_x, ans_y = vec_x.x + start.x, vec_x.y + start.y
    print('%.8f'%ans_x, '%.8f'%ans_y)

Solution (Python):

from math import sqrt


def inner_product(vect1, vect2):
    return sum([(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)]


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 = projection(p_list, p1_list, p2_list)
        print(' '.join(map(str, x_list)))

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

output_list = []


init_point = map(float, raw_input().split())

p0 = Point(init_point[0], init_point[1])
p1 = Point(init_point[2], init_point[3])


q = int(raw_input())
datalist = [map(float, raw_input().split()) for i in range(q)]





if (p0.x - p1.x) == 0:
	for data in datalist:
		p2 = Point(data[0], data[1])
		output_list.append( (p0.x, p2.y) )

elif (p0.y - p1.y) == 0:
	for data in datalist:
		p2 = Point(data[0], data[1])
		output_list.append( (p2.x, p0.y) )
		
else:
	a = (p0.y - p1.y) / (p0.x - p1.x)
	c = -1/a
	b = p0.y - a*p0.x
	
	for data in datalist:
		p2 = Point(data[0], data[1])
		
		d = p2.y - c*p2.x
		
		X = (-b+d) / (a-c)
		Y = a*X + b
		
		output_list.append( (X,Y) )
		
for x, y in output_list:
	print "%.10f %.10f" % (x, y)

Solution (Python):
from math import hypot

x1, y1, x2, y2 = map(int, input().split())
dx, dy = x2-x1, y2-y1
vector_base = hypot(dx, dy)
q = int(input())

for i in range(q):
    x3, y3 = map(int, input().split()) 
    d = ((x3-x1)*dx + (y3-y1)*dy) / (vector_base**2)
    print(x1+dx*d, y1+dy*d)

Solution (Python):
from math import sin, cos, atan2

def sgn(x, eps=1e-10):
    if x < -eps: return -1
    if -eps <= x <= eps: return 0
    if eps < x: return 1

class Vector():
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

    def arg(self):
        return atan2(self.y, self.x)

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

    def rotate(self, t):
        nx = self.x * cos(t) - self.y * sin(t)
        ny = self.x * sin(t) + self.y * cos(t)
        return Vector(nx, ny)

    def counter(self):
        nx = -self.x
        ny = -self.y
        return Vector(nx, ny)

    def times(self, k):
        nx = self.x * k
        ny = self.y * k
        return Vector(nx, ny)

    def unit(self):
        norm = self.norm()
        nx = self.x / norm
        ny = self.y / norm
        return Vector(nx, ny)

    def normal(self):
        norm = self.norm()
        nx = -self.y / norm
        ny = self.x / norm
        return Vector(nx, ny)

    def add(self, other): 
        nx = self.x + other.x
        ny = self.y + other.y
        return Vector(nx, ny)

    def sub(self, other):
        nx = self.x - other.x
        ny = self.y - other.y
        return Vector(nx, ny)

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

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

    def __str__(self):
        return str(self.x) + ' ' + str(self.y)

class Line():
    def __init__(self, bgn=Vector(), end=Vector()):
        self.bgn = bgn
        self.end = end

    def build(self, a, b, c): 
        assert sgn(a) != 0 or sgn(b) != 0
        if sgn(b) == 0:
            self.bgn = Vector(-c / a, 0.0)
            self.end = Vector(-c / a, 1.0)
        else:
            self.v = Vector(0, -c / b)
            self.u = Vector(1.0, -(a + b) / b)

    def vec(self):
        return self.end.sub(self.bgn)

    def projection(self, point):
        v = self.vec()
        u = point.sub(self.bgn)
        k = v.dot(u) / v.norm()
        h = v.unit().times(k)
        return self.bgn.add(h)

xp1, yp1, xp2, yp2 = map(int, input().split())
q = int(input())

p1 = Vector(xp1, yp1)
p2 = Vector(xp2, yp2)
l = Line(p1, p2)

for _ in range(q):
    x, y = map(int, input().split())
    p = Vector(x, y)
    proj = l.projection(p)
    print(proj)

Solution (Python):
import math

class Vector:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        
    def norm_2(self):
        return dot(self,self)
    
    def norm(self):
        return math.sqrt(self.norm_2())
    
def v_sum(v1,v2):
    return Vector(v1.x+v2.x,v1.y+v2.y)
def v_diff(v1,v2):
    return Vector(v1.x-v2.x,v1.y-v2.y)
def scalar_multi(k,v):
    return Vector(k*v.x,k*v.y)
    
def dot(vector1,vector2):
    return vector1.x*vector2.x+vector1.y*vector2.y

def cross(vector1,vector2):
    return vector1.x*vector2.y+vector1.y*vector2.x

x1,y1,x2,y2 = map(int,input().split())
p1 = Vector(x1,y1)
p2 = Vector(x2,y2)

d = v_diff(p2,p1)

q = int(input())
for i in range(q):
    x,y = map(int,input().split())
    v = v_diff(Vector(x,y),p1)
    u = v_sum(scalar_multi(dot(v,d)/d.norm_2(),d),p1)
    print('{} {}'.format(round(u.x,9),round(u.y,9)))