from typing import List
import math
PI = math.acos(-1)
# 浮点数的符号函数
ESP = 10 ** (-8)
def sign(val: float):
if abs(val) < ESP:
return 0
return 1 if val > 0 else -1
# 向量的内乘积,表示 |a| * |b| * cos(theta), 输入是两个向量的坐标(x1, y1), (x2, y2)
# 输入是两个向量的坐标(x1, y1), (x2, y2)
def dot(a, b):
return a[0]*b[0] + a[1]*b[1]
# 向量的外乘积,表示两个向量围成的平行四边形面积,b在a逆时针方向,面积为正,否则面积为负
# 输入是两个向量的坐标(x1, y1), (x2, y2)
def cross(a, b):
return a[0]*b[1] - b[0]*a[1]
# 向量取模运算(向量长度)
# 输入参数为(x, y)
def vec_length(v):
return math.sqrt(dot(v, v))
# 求解点到直线的距离
# line_point是直线上的点,line_vec是直线向量,point是待判断的点
def point_line_distance(line_point, line_vec, point):
area = cross( (point[0]-line_point[0], point[1]-line_point[1]), line_vec )
return abs(area / vec_length(line_vec))
# 求解两条直线的交点(注意前提是两条直线不平行, 平行直线输入会异常)
# p1, v1 是第一条直线的点向式表示,p2, v2 是第二条直线的点向式表示
def intersection_point(p1, v1, p2, v2):
u = (p1[0]-p2[0], p1[1]-p2[1])
t = cross(v2, u) / cross(v1, v2)
return (p1[0] + v1[0]*t, p1[1] + v1[1]*t)
# 求解向量顺时针旋转theta角度(弧度制)后的向量, 逆时针旋转theta等价于顺时针旋转-theta
def vec_rotate(v, theta):
x, y = v
return (x * math.cos(theta) + y * math.sin(theta), -x * math.sin(theta) + y * math.cos(theta))
# 直线和圆的交点, p, v是直线的点向式
def intersection_point_line_on_circle(p, v):
dis = point_line_distance(p, v, (0, 0))
len_val = math.sqrt(R**2 - dis**2)
vec_len = vec_length(v)
vv = (v[0]/vec_len, v[1]/vec_len)
pp = intersection_point(p, v, (0, 0), vec_rotate(v, PI/2))
return (pp[0]-len_val*vv[0], pp[1]-len_val*vv[1]), (pp[0]+len_val*vv[0], pp[1]+len_val*vv[1])
# 判断点在不在线段上(线段包含端点),在线段上返回True, 反之返回False
# 线段的两个端点是line_point1和line_point2, point是待判断的点
def point_on_line_seegment(line_point1, line_point2, point):
a, b, p = line_point1, line_point2, point
v1, v2 = (p[0]-a[0], p[1]-a[1]), (p[0]-b[0], p[1]-b[1])
return sign(cross(v1, v2)) == 0 and sign(dot(v1, v2)) <= 0
# 求解向量夹角,结果[0, pi/2] (注意是夹角不是转角)
def vec_angle(a, b):
return math.acos(dot(a, b) / vec_length(a) / vec_length(b))
while True:
try:
R = float(input())
except:
break
n = int(input())
def in_circle(p):
return p[0]**2 + p[1]**2 <= R**2
points = []
for i in range(n):
x, y = map(float, input().split())
points.append((x, y))
points.append(points[0])
tot = 0
for i in range(len(points)-1):
a, b = points[i], points[i+1]
if in_circle(a) and in_circle(b):
tot += cross(a, b) / 2
continue
if in_circle(a) and not in_circle(b):
p1, p2 = intersection_point_line_on_circle(a, (b[0]-a[0], b[1]-a[1]))
pp = None
if point_on_line_seegment(a, b, p1) and sign(cross(p1, a)) != 0:
pp = p1
else:
pp = p2
sign_val = sign(cross(a, b))
tot += sign_val * (R**2) * vec_angle(pp, b) / 2
tot += cross(a, pp) / 2
continue
if not in_circle(a) and in_circle(b):
p1, p2 = intersection_point_line_on_circle(a, (b[0] - a[0], b[1] - a[1]))
pp = None
if point_on_line_seegment(a, b, p1) and sign(cross(p1, a)) != 0:
pp = p1
else:
pp = p2
sign_val = sign(cross(a, b))
tot += sign_val * (R**2) * vec_angle(a, pp) / 2
tot += cross(pp, b) / 2
continue
dis = point_line_distance(a, (b[0]-a[0], b[1]-a[1]), (0, 0))
if dis < R:
p1, p2 = intersection_point_line_on_circle(a, (b[0] - a[0], b[1] - a[1]))
if (not point_on_line_seegment(a, b, p1)) and (not point_on_line_seegment(a, b, p2)):
angle = vec_angle(a, b)
sign_val = sign(cross(a, b))
tot += sign_val * (R ** 2) * angle / 2
else:
angle1 = min(vec_angle(a, p1), vec_angle(a, p2))
angle2 = min(vec_angle(b, p1), vec_angle(b, p2))
sign_val = sign(cross(a, b))
tot += sign_val * (R**2) * (angle1 + angle2) / 2
tot += sign_val * abs(cross(p1, p2)/2)
else:
angle = vec_angle(a, b)
sign_val = sign(cross(a, b))
tot += sign_val * (R**2) * angle / 2
print("%.2f" % round(abs(tot), 2))