Language: C++

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
double pi=3.14159265359; //円周率
double EPS = 1e-10; //誤差

class point
{
	public:
	double x,y;

	point()
	{x=0;y=0;}
	//コンストラクタ
	point(double a,double b)
	{
		x=a;y=b;
	}

	//足し算
	point operator + (point p)
	{
		return point(x+p.x,y+p.y);
	}
	//引き算
	point operator - (point p)
	{
		return point(x-p.x,y-p.y);
	}
	//d倍
	point operator * (double d)
	{
		return point(x*d,y*d);
	}
	//x座標昇順でソートする
	bool operator < (const point &p)
	const {
		if(std::abs(x-p.x)>EPS)
			return x<p.x;
		else
			return y<p.y;
	}
	//内積
	//直交判定...abs(dot())<EPS
	double dot(point p)
	{
		return x*p.x+y*p.y;
	}
	//外積
	//平行判定...abs(cross())<EPS
	double cross(point p)
	{
		return x*p.y-y*p.x;
	}
	//絶対値を求める
	double abs()
	{
		return sqrt(x*x+y*y);
	}
	//単位ベクトルを求める
	point ev()
	{
		return point(x/abs(),y/abs());
	}
	//単位法線ベクトル（の1つ）を求める
	//もうひとつは-1倍することで求まる
	point nev()
	{
		return point(-y/abs(),x/abs());
	}
	//2点間の距離を求める
	//distがEPS未満なら2点は同じ位置にあるとみなす
	double pdist(point p)
	{
		point v=point(p.x-x,p.y-y);
		return v.abs();
	}
	//この点と直線p1-p2の距離を求める
	double ldist(point p1,point p2)
	{
		return std::abs((p2.y-p1.y)*x-(p2.x-p1.x)*y+p2.x*p1.y+p2.y*p1.x)/(p2-p1).abs();
	}

};

//線分p1-p2上に点qがあるか判定
bool on_seg(point p1,point p2,point q)
{
	return abs((p1-q).cross(p2-q))<EPS && (p1-q).dot(p2-q)<=EPS;
}
//直線p1-p2と直線q1-q2の交点
//線分の交差判定はこれの戻り値が線分上にあるかどうかで判定する...on_seg(p1,p2,intersection(p1,p2,q1,q2))
point intersection(point p1,point p2,point q1,point q2)
{
	return p1 + (p2-p1) * ((q2-q1).cross(q1-p1) / (q2-q1).cross(p2-p1));
}
//n頂点からなる多角形の面積を求める(vectorで頂点は与えられる)
double area(vector<point> p)
{
	double ret=0;
	for(int i=0;i<p.size()-1;i++)
	{
		ret+=p[i].cross(p[i+1]);
	}
	ret+=p[p.size()-1].cross(p[0]);
	ret/=2;
	return ret;
}
/*
3点a,b,cをa->b->cと進むとき、
* a->bで時計方向に折れてb->c (clockwise)
* a->bで反時計方向に折れてb->c(counter clockwise)
* a->bで逆を向いてaを通り越してb->c(c--a--b on line)
* a->bでそのままb->c(a--b--c on line)
* a->bで逆を向いてb->c(a--c--b on line)

のいずれのパターンかを判定する
*/
int ccw(point a,point b,point c)
{
	b=b-a;c=c-a;
	if(b.cross(c)>EPS)
		return 1; //counter clockwise
	if(b.cross(c)<-EPS)
		return -1; //clockwise
	if(b.dot(c)<-EPS)
		return 2; //c--a--b on line
	if(b.x*b.x+b.y*b.y < c.x*c.x+c.y*c.y)
		return -2; //a--b--c on line
	return 0; //a--c--b on line
}
//凸包を求める
vector<point> convex_hull(vector<point> p)
{
	int n=p.size();
	int k=0;
	sort(p.begin(),p.end());
	vector<point> ch(2*n);
	//lower-hull 下側凸包
	for(int i=0;i<n;ch[k++]=p[i++])
	{
		while(k>=2 && ccw(ch[k-2],ch[k-1],p[i])<=0)
			k--;
	}
	//upper-hull 上側凸包
	for(int i=n-2,t=k+1;i>=0;ch[k++]=p[i--])
	{
		while(k>=t && ccw(ch[k-2],ch[k-1],p[i])<=0)
			k--;
	}
	ch.resize(k-1);
	return ch;
}

int main()
{
	point p1,p2;
	double a,b,c,d;
	cin>>a>>b>>c>>d;
	p1=point(a,b);
	p2=point(c,d);
	int q;
	cin>>q;
	for(int i=0;i<q;i++)
	{
		point f;
		cin>>a>>b;
		f=point(a,b);
		point x;
		x=f+(p2-p1).abs()*(p2-p1).nev();
		if(!on_seg(p1,p2,x))
			x=f+(p2-p1).abs()*(p2-p1).nev();
		cout<<x.x<<" "<<x.y<<endl;
	}
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:171:34: error: no match for 'operator*' (operand types are 'double' and 'point')
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                     ~~~~~~~~~~~~~^~~~~~~~~~~~~~
      |                                |             |
      |                                double        point
In file included from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127,
                 from a.cc:1:
/usr/include/c++/14/complex:400:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const complex<_Tp>&)'
  400 |     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:400:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::complex<_Tp>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/complex:409:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const _Tp&)'
  409 |     operator*(const complex<_Tp>& __x, const _Tp& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:409:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::complex<_Tp>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/complex:418:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const complex<_Tp>&)'
  418 |     operator*(const _Tp& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:418:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   'point' is not derived from 'const std::complex<_Tp>'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
In file included from /usr/include/c++/14/valarray:605,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const _Expr<_Dom2, typename _Dom2::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const typename _Dom::value_type&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   'point' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const valarray<typename _Dom::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const valarray<typename _Dom::value_type>&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   'point' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const valarray<_Tp>&, const valarray<_Tp>&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::valarray<_Tp>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_Constant, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const valarray<_Tp>&, const typename valarray<_Tp>::value_type&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   mismatched types 'const std::valarray<_Tp>' and 'double'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Constant, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const typename valarray<_Tp>::value_type&, const valarray<_Tp>&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:171:47: note:   'point' is not derived from 'const std::valarray<_Tp>'
  171 |                 x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                               ^
a.cc:173:42: error: no match for 'operator*' (operand types are 'double' and 'point')
  173 |                         x=f+(p2-p1).abs()*(p2-p1).nev();
      |                             ~~~~~~~~~~~~~^~~~~~~~~~~~~~
      |                                        |             |
      |                                        double        point
/usr/include/c++/14/complex:400:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const complex<_Tp>&)'
  400 |     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:400:5: note:   template argument deduction/substitution failed:
a.cc:173:55: note:   mismatched types 'const std::complex<_Tp>' and 'double'
  173 |                         x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                                       ^
/usr/include/c++/14/complex:409:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const _Tp&)'
  409 |     operator*(const complex<_Tp>& __x, const _Tp& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:409:5: note:   template argument deduction/substitution failed:
a.cc:173:55: note:   mismatched types 'const std::complex<_Tp>' and 'double'
  173 |                         x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                                       ^
/usr/include/c++/14/complex:418:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const complex<_Tp>&)'
  418 |     operator*(const _Tp& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:418:5: note:   template argument deduction/substitution failed:
a.cc:173:55: note:   'point' is not derived from 'const std::complex<_Tp>'
  173 |                         x=f+(p2-p1).abs()*(p2-p1).nev();
      |                                                       ^
/usr/include/