from math import pi, acos, sqrt, atan2, hypot, cos, sin # origin O = 0., 0. # take input def take_circle(): x, y, r = map(float, raw_input().strip().split()) return (x, y), r def take_vertex(): x, y = map(float, raw_input().strip().split()) return (x, y) # simple operations def add((a, b), (c, d)): return a + c, b + d def sub((a, b), (c, d)): return a - c, b - d def mul(c, (a, b)): return c*a, c*b def mag((x, y)): return hypot(x, y) def mag2(p): return mag(p)**2 def dot((a, b), (c, d)): return a*c + b*d def cross((a, b), (c, d)): return a * d - b * c def clip(a, b, x): assert a <= b return max(a, min(b, x)) def poly_area(poly): ''' signed polygon area ''' return sum(cross(poly[i-1], poly[i]) for i in xrange(len(poly))) def ccw(poly): ''' check if vertices of 'poly' are counterclockwise ''' return poly if poly_area(poly) >= 0 else poly[::-1] def angle((x, y)): ''' angle of vector in the range [0,2*pi) ''' return atan2(y, x) % (2*pi) class Chip(object): ''' a segment you can remove (or "chip away") parts/subsegments of. use .segs to iterate over all remaining, unremoved subsegments. ''' def __init__(self, m): self.m = m self.events = [] self.add(0., m) super(Chip, self).__init__() def add(self, a, b): self.change(a, b, +1) def subtract(self, a, b): self.change(a, b, -1) def change(self, a, b, s): assert 0 <= a <= b <= self.m self.events.append((a, +s)) self.events.append((b, -s)) def add_wrap(self, a, b): self.change_wrap(a, b, +1) def subtract_wrap(self, a, b): self.change_wrap(a, b, -1) def change_wrap(self, a, b, s): if a <= b: self.change(a, b, s) else: self.change(a, self.m, s) self.change(0, b, s) def segs(self): ct = 0 prev = 0. for curr, d in sorted(self.events): if ct > 0: assert ct == 1 yield prev, curr ct += d prev = curr def circ_intersect((C, R), (c, r)): ''' part of (C,R) inside (c,r) ''' # to origin c = sub(c, C) # angle of other circle th = angle(c) x = mag(c) if abs(x) <= 1e-11: if R < r: return 0, 2*pi else: return 0, 0 val = clip(-1, 1, (x*x + R*R - r*r) / (2*R*x)) if val <= -1: return 0, 2*pi if val >= 1: return 0, 0 TH = acos(val) a = (th - TH) % (2*pi) b = (th + TH) % (2*pi) return a, b def _sector_fix(a, b): B = (b - a) % (2*pi) if B <= pi: return a, b, -1 else: return b, a, +1 def circ_slice((c, r), (p, q)): ''' part of (c,r) inside (c,p,q). respect sign! ''' ta, tb = seg_slice((p, q), (c, r)) thp = angle(sub(p, c)) tha = angle(sub(seg_point((p, q), ta), c)) thb = angle(sub(seg_point((p, q), tb), c)) thq = angle(sub(q, c)) yield _sector_fix(thp, tha) yield _sector_fix(thb, thq) def seg_slice((p, q), (c, r)): ''' part of seg (p,q) inside (c,r) ''' p = sub(p, c) q = sub(q, c) v = sub(q, p) A = mag2(v) B = dot(p, v) C = mag2(p) - r*r D = B*B - A*C if D <= 0: return 0, 0 sqrtD = sqrt(D) ta = clip(0, 1, (-B - sqrtD) / A) tb = clip(0, 1, (-B + sqrtD) / A) return ta, tb def circ_point((c, r), t): ''' point in circle at angle "t" ''' return add(c, mul(r, (cos(t), sin(t)))) def seg_point((p, q), a): ''' point along the segment at ratio "a" from p to q. ''' return add(p, mul(a, sub(q, p))) def area_circs(circles, poly): ''' area of union of circles and poly ''' area = 0. for i in xrange(len(circles)): ch = Chip(2*pi) # circles on circles for j in xrange(len(circles)): if j == i: continue a, b = circ_intersect(circles[i], circles[j]) ch.subtract_wrap(a, b) # circles on poly for j in xrange(len(poly)): p = poly[j-1] q = poly[j] for a, b, c in circ_slice(circles[i], (p, q)): ch.change_wrap(a, b, c) # compute circle contribution c, r = circles[i] for a, b in ch.segs(): pa = circ_point(circles[i], a) pb = circ_point(circles[i], b) area += (b - a) * r**2 area -= poly_area([c, pa, pb]) area += poly_area([O, pa, pb]) for i in xrange(len(poly)): p = poly[i-1] q = poly[i] # poly on circles ch = Chip(1.) for circ in circles: a, b = seg_slice((p, q), circ) ch.subtract(a, b) # compute contribution of side for a, b in ch.segs(): pa = seg_point((p, q), a) pb = seg_point((p, q), b) area += poly_area([O, pa, pb]) return area circles = list(set(take_circle() for i in xrange(input()))) poly = ccw([take_vertex() for i in xrange(input())]) print 0.5 * (poly_area(poly) - area_circs(circles, poly) + area_circs(circles, []))