blob: 58b7b85d4929b517596ce235fcdbf4f770ab9a65 [file] [log] [blame]
caryclark@google.coma7e483d2012-08-28 20:44:43 +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 */
7#include "CubicLineSegments.h"
8#include "QuadraticLineSegments.h"
caryclark@google.coma7e483d2012-08-28 20:44:43 +00009
10// http://cagd.cs.byu.edu/~557/text/cagd.pdf 2.7
11// A hodograph is the first derivative curve
12void hodograph(const Cubic& cubic, Quadratic& hodo) {
13 hodo[0].x = 3 * (cubic[1].x - cubic[0].x);
14 hodo[0].y = 3 * (cubic[1].y - cubic[0].y);
15 hodo[1].x = 3 * (cubic[2].x - cubic[1].x);
16 hodo[1].y = 3 * (cubic[2].y - cubic[1].y);
17 hodo[2].x = 3 * (cubic[3].x - cubic[2].x);
18 hodo[2].y = 3 * (cubic[3].y - cubic[2].y);
19}
20
21// A 2nd hodograph is the second derivative curve
22void secondHodograph(const Cubic& cubic, _Line& hodo2) {
23 Quadratic hodo;
24 hodograph(cubic, hodo);
25 hodograph(hodo, hodo2);
26}
27
28// The number of line segments required to approximate the cubic
29// see http://cagd.cs.byu.edu/~557/text/cagd.pdf 10.6
30double subDivisions(const Cubic& cubic) {
31 _Line hodo2;
32 secondHodograph(cubic, hodo2);
caryclark@google.comaa358312013-01-29 20:28:49 +000033 double maxX = SkTMax(hodo2[1].x, hodo2[1].x);
34 double maxY = SkTMax(hodo2[1].y, hodo2[1].y);
caryclark@google.coma7e483d2012-08-28 20:44:43 +000035 double dist = sqrt(maxX * maxX + maxY * maxY);
36 double segments = sqrt(dist / (8 * FLT_EPSILON));
37 return segments;
38}