blob: 95ed1e04992253a9ab503e2aa2538e6d75087522 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com639df892012-01-10 21:46:10 +00008#include "TestUtilities.h"
9
10void quad_to_cubic(const Quadratic& quad, Cubic& cubic) {
11 cubic[0] = quad[0];
12 cubic[1].x = quad[0].x / 3 + quad[1].x * 2 / 3;
13 cubic[1].y = quad[0].y / 3 + quad[1].y * 2 / 3;
14 cubic[2].x = quad[2].x / 3 + quad[1].x * 2 / 3;
15 cubic[2].y = quad[2].y / 3 + quad[1].y * 2 / 3;
16 cubic[3] = quad[2];
17}
18
19static bool tiny(const Cubic& cubic) {
20 int index, minX, maxX, minY, maxY;
21 minX = maxX = minY = maxY = 0;
22 for (index = 1; index < 4; ++index) {
23 if (cubic[minX].x > cubic[index].x) {
24 minX = index;
25 }
26 if (cubic[minY].y > cubic[index].y) {
27 minY = index;
28 }
29 if (cubic[maxX].x < cubic[index].x) {
30 maxX = index;
31 }
32 if (cubic[maxY].y < cubic[index].y) {
33 maxY = index;
34 }
35 }
36 return approximately_equal(cubic[maxX].x, cubic[minX].x)
37 && approximately_equal(cubic[maxY].y, cubic[minY].y);
38}
39
40void find_tight_bounds(const Cubic& cubic, _Rect& bounds) {
41 CubicPair cubicPair;
42 chop_at(cubic, cubicPair, 0.5);
43 if (!tiny(cubicPair.first()) && !controls_inside(cubicPair.first())) {
44 find_tight_bounds(cubicPair.first(), bounds);
45 } else {
46 bounds.add(cubicPair.first()[0]);
47 bounds.add(cubicPair.first()[3]);
48 }
49 if (!tiny(cubicPair.second()) && !controls_inside(cubicPair.second())) {
50 find_tight_bounds(cubicPair.second(), bounds);
51 } else {
52 bounds.add(cubicPair.second()[0]);
53 bounds.add(cubicPair.second()[3]);
54 }
55}
56
57bool controls_inside(const Cubic& cubic) {
58 return
59 ((cubic[0].x <= cubic[1].x && cubic[0].x <= cubic[2].x && cubic[1].x <= cubic[3].x && cubic[2].x <= cubic[3].x)
60 || (cubic[0].x >= cubic[1].x && cubic[0].x >= cubic[2].x && cubic[1].x >= cubic[3].x && cubic[2].x >= cubic[3].x))
61 && ((cubic[0].y <= cubic[1].y && cubic[0].y <= cubic[2].y && cubic[1].y <= cubic[3].y && cubic[2].y <= cubic[3].y)
62 || (cubic[0].y >= cubic[1].y && cubic[0].y >= cubic[2].y && cubic[1].y >= cubic[3].y && cubic[2].x >= cubic[3].y));
63}
64