// File: ddunbar[P]PDSample/ScallopedSector.cpp
// Function: ScallopedSector::subtractDisk(Vec2&, float, std::vector<ScallopedSector, std::allocator<ScallopedSector>>*)
void ScallopedSector::subtractDisk(Vec2 &C, float r, std::vector<ScallopedSector> *regions)
{
	std::vector<float> angles;

	Vec2 v(C.x - P.x, C.y-P.y);
	float d = sqrt(v.x*v.x + v.y*v.y);

	if (r<d) {
		float theta = atan2(v.y, v.x);
		float x = sqrt(d*d-r*r);
		float angle, alpha = asin(r/d);
		
		angle = canonizeAngle(theta+alpha);
		if (a1<angle && angle<a2) {
			if (distToCurve(angle,0)<x && x<distToCurve(angle,1))
				angles.push_back(angle);
		}

		angle = canonizeAngle(theta-alpha);
		if (a1<angle && angle<a2) {
			if (distToCurve(angle,0)<x && x<distToCurve(angle,1)) 
				angles.push_back(angle);
		}
	}

	for (int arcIndex=0; arcIndex<2; arcIndex++) {
		Vec2 &C2 = arcs[arcIndex].P;
		float R = arcs[arcIndex].r;
		Vec2 v(C.x - C2.x, C.y - C2.y);
		float d = sqrt(v.x*v.x + v.y*v.y);

		if (d>FLT_EPSILON) {
			float invD = 1.0f/d;
			float x = (d*d - r*r + R*R)*invD*.5f;
			float k = R*R - x*x;

			if (k>0) {
				float y = sqrt(k);
				float vx = v.x*invD;
				float vy = v.y*invD;
				float vx_x = vx*x, vy_x = vy*x;
				float vx_y = vx*y, vy_y = vy*y;
				float angle;

				angle = canonizeAngle(atan2(C2.y + vy_x + vx_y - P.y,
											C2.x + vx_x - vy_y - P.x));
				if (a1<angle && angle<a2) angles.push_back(angle);

				angle = canonizeAngle(atan2(C2.y + vy_x - vx_y - P.y,
											C2.x + vx_x + vy_y - P.x));
				if (a1<angle && angle<a2) angles.push_back(angle);
			}
		}
	}
	
	sort(angles.begin(), angles.end());
	angles.insert(angles.begin(), a1);
	angles.push_back(a2);

	for (unsigned int i=1; i<angles.size(); i++) {
		float a1 = angles[i-1], a2 = angles[i];
		float midA = (a1+a2)*.5f;
		float inner = distToCurve(midA,0);
		float outer = distToCurve(midA,1);
		float d1, d2;

		distToCircle(midA, C, r, &d1, &d2); // d1<=d2

		if (d2<inner || d1>outer) {
			regions->push_back(ScallopedSector(P, a1, a2, arcs[0].P, arcs[0].r, arcs[0].sign, arcs[1].P, arcs[1].r, arcs[1].sign));
		} else {
			if (inner<d1) {
				regions->push_back(ScallopedSector(P, a1, a2, arcs[0].P, arcs[0].r, arcs[0].sign, C, r, -1));
			}
			if (d2<outer) {
				regions->push_back(ScallopedSector(P, a1, a2, C, r, 1, arcs[1].P, arcs[1].r, arcs[1].sign));
			}
		}
	}
}

// Assembly:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rdi, 0x2b0(%rsp)
movq %rsi, 0x2a8(%rsp)
movss %xmm0, 0x2a4(%rsp)
movq %rdx, 0x298(%rsp)
movq 0x2b0(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x280(%rsp), %rdi
callq 0x120a0
movq 0xa0(%rsp), %rax
movq 0x2a8(%rsp), %rcx
movss (%rcx), %xmm0
movss 0x4(%rcx), %xmm1
movss (%rax), %xmm3
movss 0x4(%rax), %xmm2
subss %xmm3, %xmm0
subss %xmm2, %xmm1
leaq 0x278(%rsp), %rdi
callq 0x6b60
jmp 0x10ede
movss 0x278(%rsp), %xmm0
movss 0x27c(%rsp), %xmm1
mulss %xmm1, %xmm1
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
callq 0x6d50
movss %xmm0, 0x9c(%rsp)
jmp 0x10f0c
movss 0x9c(%rsp), %xmm0
movss %xmm0, 0x268(%rsp)
movss 0x2a4(%rsp), %xmm1
movss 0x268(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1123d
movss 0x278(%rsp), %xmm1
movss 0x27c(%rsp), %xmm0
callq 0x6dc0
movss %xmm0, 0x98(%rsp)
jmp 0x10f5b
movss 0x98(%rsp), %xmm0
movss %xmm0, 0x264(%rsp)
movss 0x268(%rsp), %xmm0
movss 0x2a4(%rsp), %xmm1
mulss %xmm1, %xmm1
mulss %xmm0, %xmm0
subss %xmm1, %xmm0
callq 0x6d50
movss %xmm0, 0x94(%rsp)
jmp 0x10f9b
movss 0x94(%rsp), %xmm0
movss %xmm0, 0x260(%rsp)
movss 0x2a4(%rsp), %xmm0
movss 0x268(%rsp), %xmm1
divss %xmm1, %xmm0
callq 0x120b0
movss %xmm0, 0x90(%rsp)
jmp 0x10fd3
movq 0xa0(%rsp), %rdi
movss 0x90(%rsp), %xmm0
movss %xmm0, 0x258(%rsp)
movss 0x264(%rsp), %xmm0
movss 0x258(%rsp), %xmm1
addss %xmm1, %xmm0
callq 0x10c60
movss %xmm0, 0x8c(%rsp)
jmp 0x11013
movq 0xa0(%rsp), %rax
movss 0x8c(%rsp), %xmm0
movss %xmm0, 0x25c(%rsp)
movss 0x8(%rax), %xmm1
movss 0x25c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11131
movq 0xa0(%rsp), %rax
movss 0x25c(%rsp), %xmm1
movss 0xc(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11131
movq 0xa0(%rsp), %rdi
movss 0x25c(%rsp), %xmm0
xorl %esi, %esi
callq 0x10970
movss %xmm0, 0x88(%rsp)
jmp 0x11086
movss 0x88(%rsp), %xmm1
movss 0x260(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1112f
movq 0xa0(%rsp), %rdi
movss 0x260(%rsp), %xmm0
movss %xmm0, 0x80(%rsp)
movss 0x25c(%rsp), %xmm0
movl $0x1, %esi
callq 0x10970
movss %xmm0, 0x84(%rsp)
jmp 0x110d9
movss 0x84(%rsp), %xmm0
movss 0x80(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x1112f
leaq 0x280(%rsp), %rdi
leaq 0x25c(%rsp), %rsi
callq 0x120d0
jmp 0x11107
jmp 0x1112f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x270(%rsp)
movl %eax, 0x26c(%rsp)
leaq 0x280(%rsp), %rdi
callq 0x124b0
jmp 0x11aab
jmp 0x11131
movq 0xa0(%rsp), %rdi
movss 0x264(%rsp), %xmm0
movss 0x258(%rsp), %xmm1
subss %xmm1, %xmm0
callq 0x10c60
movss %xmm0, 0x7c(%rsp)
jmp 0x1115c
movq 0xa0(%rsp), %rax
movss 0x7c(%rsp), %xmm0
movss %xmm0, 0x25c(%rsp)
movss 0x8(%rax), %xmm1
movss 0x25c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1123b
movq 0xa0(%rsp), %rax
movss 0x25c(%rsp), %xmm1
movss 0xc(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1123b
movq 0xa0(%rsp), %rdi
movss 0x25c(%rsp), %xmm0
xorl %esi, %esi
callq 0x10970
movss %xmm0, 0x78(%rsp)
jmp 0x111c9
movss 0x78(%rsp), %xmm1
movss 0x260(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11239
movq 0xa0(%rsp), %rdi
movss 0x260(%rsp), %xmm0
movss %xmm0, 0x70(%rsp)
movss 0x25c(%rsp), %xmm0
movl $0x1, %esi
callq 0x10970
movss %xmm0, 0x74(%rsp)
jmp 0x1120f
movss 0x74(%rsp), %xmm0
movss 0x70(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x11239
leaq 0x280(%rsp), %rdi
leaq 0x25c(%rsp), %rsi
callq 0x120d0
jmp 0x11237
jmp 0x11239
jmp 0x1123b
jmp 0x1123d
movl $0x0, 0x254(%rsp)
cmpl $0x2, 0x254(%rsp)
jge 0x116c4
movq 0xa0(%rsp), %rax
movslq 0x254(%rsp), %rcx
leaq (%rcx,%rcx,8), %rcx
leaq 0x14(%rax,%rcx,4), %rcx
movq %rcx, 0x248(%rsp)
movslq 0x254(%rsp), %rcx
leaq (%rcx,%rcx,8), %rcx
movss 0x1c(%rax,%rcx,4), %xmm0
movss %xmm0, 0x244(%rsp)
movq 0x2a8(%rsp), %rcx
movq 0x248(%rsp), %rax
movss (%rcx), %xmm0
movss 0x4(%rcx), %xmm1
movss (%rax), %xmm3
movss 0x4(%rax), %xmm2
subss %xmm3, %xmm0
subss %xmm2, %xmm1
leaq 0x23c(%rsp), %rdi
callq 0x6b60
jmp 0x112cb
movss 0x23c(%rsp), %xmm0
movss 0x240(%rsp), %xmm1
mulss %xmm1, %xmm1
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
callq 0x6d50
movss %xmm0, 0x6c(%rsp)
jmp 0x112f6
movss 0x6c(%rsp), %xmm0
movss %xmm0, 0x238(%rsp)
movss 0x238(%rsp), %xmm0
movss 0x7112(%rip), %xmm1     # 0x18428
ucomiss %xmm1, %xmm0
jbe 0x116ac
movss 0x51a1(%rip), %xmm0     # 0x164c8
divss 0x238(%rsp), %xmm0
movss %xmm0, 0x234(%rsp)
movss 0x238(%rsp), %xmm1
movss 0x238(%rsp), %xmm2
movss 0x2a4(%rsp), %xmm0
mulss 0x2a4(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x244(%rsp), %xmm0
movss 0x244(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
mulss 0x234(%rsp), %xmm0
movss 0x4ca7(%rip), %xmm1     # 0x16044
mulss %xmm1, %xmm0
movss %xmm0, 0x230(%rsp)
movss 0x244(%rsp), %xmm0
movss 0x244(%rsp), %xmm2
movss 0x230(%rsp), %xmm1
mulss 0x230(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x22c(%rsp)
movss 0x22c(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x116aa
movss 0x22c(%rsp), %xmm0
callq 0x6d50
movss %xmm0, 0x68(%rsp)
jmp 0x11417
movq 0xa0(%rsp), %rax
movss 0x68(%rsp), %xmm0
movss %xmm0, 0x228(%rsp)
movss 0x23c(%rsp), %xmm0
movss 0x234(%rsp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x224(%rsp)
movss 0x240(%rsp), %xmm0
movss 0x234(%rsp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x220(%rsp)
movss 0x224(%rsp), %xmm0
movss 0x230(%rsp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x21c(%rsp)
movss 0x220(%rsp), %xmm0
movss 0x230(%rsp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x218(%rsp)
movss 0x224(%rsp), %xmm0
movss 0x228(%rsp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x214(%rsp)
movss 0x220(%rsp), %xmm0
movss 0x228(%rsp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, 0x210(%rsp)
movq 0x248(%rsp), %rcx
movss (%rcx), %xmm1
movss 0x4(%rcx), %xmm0
movss 0x218(%rsp), %xmm2
addss %xmm2, %xmm0
movss 0x214(%rsp), %xmm2
addss %xmm2, %xmm0
movss (%rax), %xmm2
movss 0x4(%rax), %xmm3
subss %xmm3, %xmm0
movss 0x21c(%rsp), %xmm3
addss %xmm3, %xmm1
movss 0x210(%rsp), %xmm3
subss %xmm3, %xmm1
subss %xmm2, %xmm1
callq 0x6dc0
movss %xmm0, 0x64(%rsp)
jmp 0x1154b
movss 0x64(%rsp), %xmm0
movq 0xa0(%rsp), %rdi
callq 0x10c60
movss %xmm0, 0x60(%rsp)
jmp 0x11566
movq 0xa0(%rsp), %rax
movss 0x60(%rsp), %xmm0
movss %xmm0, 0x20c(%rsp)
movss 0x8(%rax), %xmm1
movss 0x20c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x115c4
movq 0xa0(%rsp), %rax
movss 0x20c(%rsp), %xmm1
movss 0xc(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x115c4
leaq 0x280(%rsp), %rdi
leaq 0x20c(%rsp), %rsi
callq 0x120d0
jmp 0x115c2
jmp 0x115c4
movq 0xa0(%rsp), %rax
movq 0x248(%rsp), %rcx
movss (%rcx), %xmm1
movss 0x4(%rcx), %xmm0
movss 0x218(%rsp), %xmm2
addss %xmm2, %xmm0
movss 0x214(%rsp), %xmm2
subss %xmm2, %xmm0
movss (%rax), %xmm2
movss 0x4(%rax), %xmm3
subss %xmm3, %xmm0
movss 0x21c(%rsp), %xmm3
addss %xmm3, %xmm1
movss 0x210(%rsp), %xmm3
addss %xmm3, %xmm1
subss %xmm2, %xmm1
callq 0x6dc0
movss %xmm0, 0x5c(%rsp)
jmp 0x1162f
movss 0x5c(%rsp), %xmm0
movq 0xa0(%rsp), %rdi
callq 0x10c60
movss %xmm0, 0x58(%rsp)
jmp 0x1164a
movq 0xa0(%rsp), %rax
movss 0x58(%rsp), %xmm0
movss %xmm0, 0x20c(%rsp)
movss 0x8(%rax), %xmm1
movss 0x20c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x116a8
movq 0xa0(%rsp), %rax
movss 0x20c(%rsp), %xmm1
movss 0xc(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x116a8
leaq 0x280(%rsp), %rdi
leaq 0x20c(%rsp), %rsi
callq 0x120d0
jmp 0x116a6
jmp 0x116a8
jmp 0x116aa
jmp 0x116ac
jmp 0x116ae
movl 0x254(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x254(%rsp)
jmp 0x11248
leaq 0x280(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x12180
movq 0x50(%rsp), %rdi
movq %rax, 0x200(%rsp)
callq 0x121b0
movq %rax, 0x1f8(%rsp)
movq 0x200(%rsp), %rdi
movq 0x1f8(%rsp), %rsi
callq 0x12140
jmp 0x11707
leaq 0x280(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x12180
movq %rax, 0x1e8(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x12400
movq 0xa0(%rsp), %rdx
movq 0x40(%rsp), %rdi
addq $0x8, %rdx
movq 0x1f0(%rsp), %rsi
callq 0x121e0
movq %rax, 0x48(%rsp)
jmp 0x1175b
movq 0xa0(%rsp), %rsi
movq 0x48(%rsp), %rax
movq %rax, 0x1e0(%rsp)
addq $0xc, %rsi
leaq 0x280(%rsp), %rdi
callq 0x120d0
jmp 0x11783
movl $0x1, 0x1dc(%rsp)
movl 0x1dc(%rsp), %eax
movq %rax, 0x38(%rsp)
leaq 0x280(%rsp), %rdi
callq 0x12440
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0x11a96
movl 0x1dc(%rsp), %eax
decl %eax
movl %eax, %esi
leaq 0x280(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x12460
movq 0x28(%rsp), %rdi
movss (%rax), %xmm0
movss %xmm0, 0x1d8(%rsp)
movl 0x1dc(%rsp), %eax
movl %eax, %esi
callq 0x12460
movq 0xa0(%rsp), %rdi
movss (%rax), %xmm0
movss %xmm0, 0x1d4(%rsp)
movss 0x1d8(%rsp), %xmm0
movss 0x1d4(%rsp), %xmm1
addss %xmm1, %xmm0
movss 0x481c(%rip), %xmm1     # 0x16044
mulss %xmm1, %xmm0
movss %xmm0, 0x1d0(%rsp)
movss 0x1d0(%rsp), %xmm0
xorl %esi, %esi
callq 0x10970
movss %xmm0, 0x34(%rsp)
jmp 0x1184d
movq 0xa0(%rsp), %rdi
movss 0x34(%rsp), %xmm0
movss %xmm0, 0x1cc(%rsp)
movss 0x1d0(%rsp), %xmm0
movl $0x1, %esi
callq 0x10970
movss %xmm0, 0x24(%rsp)
jmp 0x1187f
movq 0xa0(%rsp), %rdi
movss 0x24(%rsp), %xmm0
movss %xmm0, 0x1c8(%rsp)
movss 0x1d0(%rsp), %xmm0
movq 0x2a8(%rsp), %rsi
movss 0x2a4(%rsp), %xmm1
leaq 0x1c4(%rsp), %rdx
leaq 0x1c0(%rsp), %rcx
callq 0x10cd0
jmp 0x118c7
movss 0x1c0(%rsp), %xmm1
movss 0x1cc(%rsp), %xmm0
ucomiss %xmm1, %xmm0
ja 0x118f1
movss 0x1c4(%rsp), %xmm0
ucomiss 0x1c8(%rsp), %xmm0
jbe 0x11962
movq 0xa0(%rsp), %rsi
movq 0x298(%rsp), %rax
movq %rax, 0x18(%rsp)
movss 0x1d8(%rsp), %xmm0
movss 0x1d4(%rsp), %xmm1
movq %rsi, %rdx
addq $0x14, %rdx
movss 0x1c(%rsi), %xmm2
movss 0x20(%rsi), %xmm3
movq %rsi, %rcx
addq $0x38, %rcx
movss 0x40(%rsi), %xmm4
movss 0x44(%rsi), %xmm5
leaq 0x164(%rsp), %rdi
callq 0x10390
jmp 0x11949
movq 0x18(%rsp), %rdi
leaq 0x164(%rsp), %rsi
callq 0x12480
jmp 0x1195d
jmp 0x11a7e
movss 0x1cc(%rsp), %xmm1
movss 0x1c4(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x119ef
movq 0xa0(%rsp), %rsi
movq 0x298(%rsp), %rax
movq %rax, 0x10(%rsp)
movss 0x1d8(%rsp), %xmm0
movss 0x1d4(%rsp), %xmm1
movq %rsi, %rdx
addq $0x14, %rdx
movss 0x1c(%rsi), %xmm2
movss 0x20(%rsi), %xmm3
movq 0x2a8(%rsp), %rcx
movss 0x2a4(%rsp), %xmm4
leaq 0x108(%rsp), %rdi
movss 0x4af2(%rip), %xmm5     # 0x164c4
callq 0x10390
jmp 0x119d9
movq 0x10(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x12480
jmp 0x119ed
jmp 0x119ef
movss 0x1c0(%rsp), %xmm1
movss 0x1c8(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11a7c
movq 0xa0(%rsp), %rsi
movq 0x298(%rsp), %rax
movq %rax, 0x8(%rsp)
movss 0x1d8(%rsp), %xmm0
movss 0x1d4(%rsp), %xmm1
movq 0x2a8(%rsp), %rdx
movss 0x2a4(%rsp), %xmm2
movq %rsi, %rcx
addq $0x38, %rcx
movss 0x40(%rsi), %xmm4
movss 0x44(%rsi), %xmm5
leaq 0xac(%rsp), %rdi
movss 0x4a69(%rip), %xmm3     # 0x164c8
callq 0x10390
jmp 0x11a66
movq 0x8(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x12480
jmp 0x11a7a
jmp 0x11a7c
jmp 0x11a7e
jmp 0x11a80
movl 0x1dc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1dc(%rsp)
jmp 0x1178e
leaq 0x280(%rsp), %rdi
callq 0x124b0
addq $0x2b8, %rsp            # imm = 0x2B8
retq
movq 0x270(%rsp), %rdi
callq 0x22f0
nopl (%rax,%rax)