function GetPointToLine(ox, oy, p1x, p1y, p2x, p2y){
	let anorm=atan2(p1y-p2y, p1x-p2x)-90;
	let n1=cos(anorm);
	let n2=sin(anorm);
	let dist= ( (ox-p1x)*cos(anorm)+(oy-p1y)*sin(anorm) )/(( (n1)^2 + (n2)^2 )^0.5);
	return dist;
}

function GetPointInRectangle(ox, oy, pcx, pcy, recl, recw, align){
	let dist1;
	let dist2;
	let isit=false;
	dist1=(| GetPointToLine(ox, oy, pcx, pcy, pcx+cos(align), pcy+sin(align)) |);
	dist2=(| GetPointToLine(ox, oy, pcx, pcy, pcx+cos(align+90), pcy+sin(align+90)) |);
	if( (dist1<0.5*recw)&&(dist2<0.5*recl) ){ isit=true; }
	return(isit);
}

function GetDistance(x1,y1,x2,y2){
	return(((x2-x1)^2+(y2-y1)^2)^(1/2));
}

function NormalizeAngle(angle){
	angle %= 360;
	if(angle<0){ angle += 360; }
	return angle;
}

function AngularDistance(angle1, angle2){
	let distance = NormalizeAngle(angle2 - angle1);
	if(distance>180){ distance-=360; }
	return distance;
}

function GetPointToLineSegment(ox, oy, p1x, p1y, p2x, p2y){

		let distance;

		let va=atan2(p1y-p2y, p1x-p2x);
		let vx=cos(va);
		let vy=sin(va);

		let anorm=va-90;
		let n1=cos(anorm);
		let n2=sin(anorm);

	    if(floor(10000*(|vx|))!=0 ){

		let b= ( (oy-p1y-ox*vy/vx+p1x*vy/vx)/(n2-n1*vy/vx) );

		let a= ( (ox-p1x-n1*b)/vx  );

		if( a<0){
			let b= ( (oy-p2y-ox*vy/vx+p2x*vy/vx)/(n2-n1*vy/vx) );
			let a= ( (ox-p2x-n1*b)/vx  );

			if(a>0){ distance=( (ox-p1x)*cos(anorm)+(oy-p1y)*sin(anorm) )/(( (n1)^2 + (n2)^2 )^0.5);
			} else{
				distance=( (ox-p2x)^2 + (oy-p2y)^2 )^0.5;
			}
		} else{
			distance=( (ox-p1x)^2 + (oy-p1y)^2 )^0.5;

		}
		distance=(|distance|);

	    } else{

		let uy=(p1y+p2y-(|p1y-p2y|))/2;
		let ly=(p1y+p2y+(|p1y-p2y|))/2;

		if(oy<=uy){
			distance=( (p1x-ox)^2 + (uy-oy)^2 )^0.5;
		}
		if(oy>=ly){
			distance=( (p1x-ox)^2 + (ly-oy)^2 )^0.5;
		}
		if(oy>uy&&oy<ly){
			distance=(| p1x-ox |);
		}

	    }

	    return distance;
	}

function GetPlayerMoveAngle {
  let angle = NULL;
  if(GetVirtualKeyState(VK_DOWN)==KEY_HOLD){
    if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
      angle = 45;
    }
    else if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
      angle = 135;
    }
    else {
      angle = 90;
    }
  }
  else if(GetVirtualKeyState(VK_UP)==KEY_HOLD){
    if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
      angle = 315;
    }
    else if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
      angle = 225;
    }
    else {
      angle = 270;
    }
  }
  else if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
    angle = 0;
  }
  else if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
    angle = 180;
  }
  return angle;
}

function GetLineBorderPoint(px, py, tx, ty){

	let ta = atan2(ty-py, tx-px);

	if( ta>= atan2(GetClipMinY - py, GetClipMinX - px) && ta<=  atan2(GetClipMinY - py, GetClipMaxX - px)){
		return [px - (GetClipMinY-py)*tan(ta+90), GetClipMinY];
	}
	if( ta>= atan2(GetClipMinY - py, GetClipMaxX - px) && ta<=  atan2(GetClipMaxY - py, GetClipMaxX - px)){
		return [GetClipMaxX, py + (GetClipMaxX-px)*tan(ta)];
	}
	if( ta>= atan2(GetClipMaxY - py, GetClipMaxX - px) && ta<=  atan2(GetClipMaxY - py, GetClipMinX - px)){
		return [px - (GetClipMaxY-py)*tan(ta-90), GetClipMaxY];
	}
	if( ta> atan2(GetClipMaxY - py, GetClipMinX - px) || ta<  atan2(GetClipMinY - py, GetClipMinX - px)){
		return [GetClipMinX, py + (GetClipMinX-px)*tan(ta+180)];
	}
}

// function sec(n) { return 1/cos(n); }
// function csc(n) { return 1/sin(n); }
// function cot(n) { return 1/tan(n); }

function cos2(n) { return cos(n)^2; }
function sin2(n) { return sin(n)^2; }
function tan2(n) { return tan(n)^2; }

function ReflectionAngle(angle, mirror)  {
   return 2*mirror-angle; }