caryclark@google.com | a7e483d | 2012-08-28 20:44:43 +0000 | [diff] [blame^] | 1 | /* |
| 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" |
| 9 | #include <algorithm> // used for std::max |
| 10 | |
| 11 | // http://cagd.cs.byu.edu/~557/text/cagd.pdf 2.7 |
| 12 | // A hodograph is the first derivative curve |
| 13 | void hodograph(const Cubic& cubic, Quadratic& hodo) { |
| 14 | hodo[0].x = 3 * (cubic[1].x - cubic[0].x); |
| 15 | hodo[0].y = 3 * (cubic[1].y - cubic[0].y); |
| 16 | hodo[1].x = 3 * (cubic[2].x - cubic[1].x); |
| 17 | hodo[1].y = 3 * (cubic[2].y - cubic[1].y); |
| 18 | hodo[2].x = 3 * (cubic[3].x - cubic[2].x); |
| 19 | hodo[2].y = 3 * (cubic[3].y - cubic[2].y); |
| 20 | } |
| 21 | |
| 22 | // A 2nd hodograph is the second derivative curve |
| 23 | void secondHodograph(const Cubic& cubic, _Line& hodo2) { |
| 24 | Quadratic hodo; |
| 25 | hodograph(cubic, hodo); |
| 26 | hodograph(hodo, hodo2); |
| 27 | } |
| 28 | |
| 29 | // The number of line segments required to approximate the cubic |
| 30 | // see http://cagd.cs.byu.edu/~557/text/cagd.pdf 10.6 |
| 31 | double subDivisions(const Cubic& cubic) { |
| 32 | _Line hodo2; |
| 33 | secondHodograph(cubic, hodo2); |
| 34 | double maxX = std::max(hodo2[1].x, hodo2[1].x); |
| 35 | double maxY = std::max(hodo2[1].y, hodo2[1].y); |
| 36 | double dist = sqrt(maxX * maxX + maxY * maxY); |
| 37 | double segments = sqrt(dist / (8 * FLT_EPSILON)); |
| 38 | return segments; |
| 39 | } |