static int SliceCubicBézierSegments(IApiDraw api, Vertex PT, double n1, double n2, Tuple <Point, LineObj> Tu1, Tuple <Point, LineObj> Tu2, int recursion) { // Tu1 may be null and cause Exception // Tu2 may be null and cause Exception // infinity recursion ? if (recursion > MAX_RECURSION) { Point P = Tu2.Item1; if (api != null) { api.lineTo(Tu2.Item1); } return(1); } // process segment Point CP = Tu1.Item2.GetLineCross(Tu2.Item2); // a controlpoint is considered misplaced if its distance // from one of the anchor is greater than the distance // between the two anchors. Point PO = Tu1.Item1 - Tu2.Item1; // point double DN = PO.Pow(); // double double D0 = Tu1.Item1.To(CP); // double double D1 = Tu1.Item1.To(Tu2.Item1); // double double D2 = Tu2.Item1.To(CP); // double if (CP == null || D1 > D0 || D2 > D0) { int total = 0; // total for this subsegment starts at 0 double umid = (n1 + n2) / 2D; // if misplaced control-point, slice segment more Vertex PM = PT.CloneAt(Convert.ToInt64(umid)); Tuple <Point, LineObj> tumid = PM.CubicTangent(); // sub segment count total += SliceCubicBézierSegments(api, PT, n1, umid, Tu1, tumid, recursion + 1); total += SliceCubicBézierSegments(api, PT, umid, n2, tumid, Tu2, recursion + 1); return(total); } else { // draw curve if (api != null) { api.curveTo(CP.X, CP.Y, Tu2.Item1.X, Tu2.Item1.Y); } return(1); } }
int DrawCubicBézier(IApiDraw api, Vertex pt) { Tuple <Point, LineObj> curt = null; // Tangent Object (LineObj)? Tuple <Point, LineObj> next; // Tangent Object (LineObj)? int total = 0; // int or long int nSegment = numSegments < 2 ? 4 : this.numSegments; double tstep = 1 / nSegment; curt = new Tuple <Point, LineObj>(pt.p0, LineObj.GetLine(pt.p0, pt.p1)); if (api != null) { api.moveTo(pt.p0); } for (int i = 1; i < nSegment; i++) { next = pt.CubicTangent(Convert.ToInt32(i * tstep)); // next tangent total += SliceCubicBézierSegments(api, pt, (i - 1) * tstep, i * tstep, curt, next, 0); // current segment if (total > numSegments) { } curt = next; } return(total); }
int DrawCubicBézier(IApiDraw api, Vertex pt) { Tuple<Point,LineObj> curt = null; // Tangent Object (LineObj)? Tuple<Point,LineObj> next; // Tangent Object (LineObj)? int total = 0; // int or long int nSegment = numSegments < 2 ? 4 : this.numSegments; double tstep = 1 / nSegment; curt = new Tuple<Point,LineObj>(pt.p0,LineObj.GetLine(pt.p0,pt.p1)); if (api!=null) api.moveTo(pt.p0); for (int i=1; i < nSegment; i++) { next = pt.CubicTangent(Convert.ToInt32(i * tstep)); // next tangent total += SliceCubicBézierSegments( api, pt, (i - 1) * tstep, i * tstep, curt, next, 0); // current segment if (total > numSegments) {} curt = next; } return total; }