public QuadraticCurveSegment[] Split(double t) { var p = new List<Point>(); var q = new List<Point>(); var _p = new List<Point>(); p.Add(Start); p.Add(ControlPoint); p.Add(End); q.Add(p[0]); q.Add(p[1]); q.Add(p[2]); while (p.Count > 1) { _p = new List<Point>(); for(int i = 0; i < p.Count - 1; i++) { var pt = Point.Lerp(t, p[i], p[i + 1]); q.Add(pt); _p.Add(pt); } p = _p; } var a = new QuadraticCurveSegment(q[0], q[3], q[5]) { Convex = Convex }; var b = new QuadraticCurveSegment(q[5], q[4], q[2]) { Convex = Convex }; return new QuadraticCurveSegment[] { a, b }; }
private static void AddQuadraticCurveSegment(QuadraticCurveSegment quadraticCurveSegment, List<PolygonPoint> points, BufferGenerator vertexBufferGenerator, BufferGenerator indexBufferGenerator, ref int indexCount, float r, float g, float b, int subDiffs = 0) { if(subDiffs < 2) { var splits = quadraticCurveSegment.Split(0.5); AddQuadraticCurveSegment(splits[0], points, vertexBufferGenerator, indexBufferGenerator, ref indexCount, r, g, b, subDiffs + 1); AddQuadraticCurveSegment(splits[1], points, vertexBufferGenerator, indexBufferGenerator, ref indexCount, r, g, b, subDiffs + 1); return; } float sign; quadraticCurveSegment.Split(0.5); if (quadraticCurveSegment.Convex) { sign = 1f; points.Add(new PolygonPoint(quadraticCurveSegment.End.X, quadraticCurveSegment.End.Y)); } else { sign = -1f; points.Add(new PolygonPoint(quadraticCurveSegment.ControlPoint.X, quadraticCurveSegment.ControlPoint.Y)); points.Add(new PolygonPoint(quadraticCurveSegment.End.X, quadraticCurveSegment.End.Y)); } AddVertex(vertexBufferGenerator, (float)quadraticCurveSegment.Start.X, (float)quadraticCurveSegment.Start.Y, 0f, 0f, r, g, b, sign); AddVertex(vertexBufferGenerator, (float)quadraticCurveSegment.End.X, (float)quadraticCurveSegment.End.Y, 1f, 1f, r, g, b, sign); AddVertex(vertexBufferGenerator, (float)quadraticCurveSegment.ControlPoint.X, (float)quadraticCurveSegment.ControlPoint.Y, 0.5f, 0f, r, g, b, sign); indexBufferGenerator.WriteUInt((uint)indexCount++, (uint)indexCount++, (uint)indexCount++); }