blob: 2d034b69e84e17d8b4156c3f1ee9525120b3ed70 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2http://stackoverflow.com/questions/2009160/how-do-i-convert-the-2-control-points-of-a-cubic-curve-to-the-single-control-poi
3*/
4
5/*
6Let's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2.
7Then for degree elevation, the equations are:
8
9Q0 = P0
10Q1 = 1/3 P0 + 2/3 P1
11Q2 = 2/3 P1 + 1/3 P2
12Q3 = P2
13In your case you have Q0..Q3 and you're solving for P0..P2. There are two ways to compute P1 from
14 the equations above:
15
16P1 = 3/2 Q1 - 1/2 Q0
17P1 = 3/2 Q2 - 1/2 Q3
18If this is a degree-elevated cubic, then both equations will give the same answer for P1. Since
19 it's likely not, your best bet is to average them. So,
20
21P1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3
caryclark@google.com07393ca2013-04-08 11:47:37 +000022*/
23
24#include "SkPathOpsCubic.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000025#include "SkPathOpsQuad.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000026
27SkDQuad SkDCubic::toQuad() const {
28 SkDQuad quad;
29 quad[0] = fPts[0];
30 const SkDPoint fromC1 = {(3 * fPts[1].fX - fPts[0].fX) / 2, (3 * fPts[1].fY - fPts[0].fY) / 2};
31 const SkDPoint fromC2 = {(3 * fPts[2].fX - fPts[3].fX) / 2, (3 * fPts[2].fY - fPts[3].fY) / 2};
32 quad[1].fX = (fromC1.fX + fromC2.fX) / 2;
33 quad[1].fY = (fromC1.fY + fromC2.fY) / 2;
34 quad[2] = fPts[3];
35 return quad;
36}