public override GeneralConic2d ToGeneralConic() { Transform2d tr = Transform2d.Scale(MajorRadius) * Transform2d.Rotate(Rotation) * Transform2d.Translate(center.X, center.Y); GeneralConic2d elcon = new GeneralConic2d(1, 0, 1.0 / (sigratio * sigratio), 0, 0, -1); //x^2+(1/b)^2-1=0 => unit ellipse elcon.Transform(tr); //transform conic to position of ellipse return(elcon); //TODO: optimize this function }
public override GeneralConic2d ToGeneralConic() { Transform2d tr = Transform2d.Rotate(rotation) * Transform2d.Translate(vertex.X, vertex.Y); GeneralConic2d res = new GeneralConic2d(a, 0, 0, 0, -1, 0); //y=ax^2 res.Transform(tr); return(res); //TODO: optimize this function }
public override GeneralConic2d ToGeneralConic() { //TODO: test this function Transform2d tr = Transform2d.Scale(majoraxis.Length) * Transform2d.Rotate(Rotation) * Transform2d.Translate(center.X, center.Y); GeneralConic2d hypcon = new GeneralConic2d(1, 0, -1.0 / (ratio * ratio), 0, 0, -1); //x^2-(y/b)^2-1=0 => unit hypcon.Transform(tr); return(hypcon); //TODO: optimize this function }
/// <summary> /// Intersects a line with a conic that is in standard position, that is, not rotated and /// centered at 0,0 /// </summary> /// <param name="con"></param> /// <param name="lin"></param> /// <returns>A list of parameters on line that is intersection points, or null if no intersections</returns> private static double[] ConicLineParametric(GeneralConic2d con, Line2d lin) { //We construct a matrix so that: conic is unrotated (B term=0) and line starts at origo and has length=1.0 //This is to improve stabillity of the equation double invlen = 1.0 / lin.Length; if (double.IsInfinity(invlen)) { return(null); //zero length line does not intersect } Transform2d tr = Transform2d.Translate(-lin.X1, -lin.Y1) * Transform2d.Rotate(-con.Rotation) * Transform2d.Scale(invlen); GeneralConic2d c = new GeneralConic2d(con); //copy for modification double x1 = lin.X2, y1 = lin.Y2; c.Transform(tr); tr.Apply(x1, y1, out x1, out y1, true); //transformed line end double t2 = y1 * y1 * c.C + x1 * x1 * c.A; double t1 = y1 * c.E + x1 * c.D; double t0 = c.F; double[] ts = RealPolynomial.SolveQuadric(t2, t1, t0); return(ts); /*double dx=lin.DX; * double dy=lin.DY; * double x0=lin.X1; * double y0=lin.Y1; * * double t2=con.C*dy*dy+con.A*dx*dx; * double t1=2*con.C*dy*y0+2*con.A*dx*x0+dy*con.E+con.D*dx; * double t0=con.C*y0*y0+con.E*y0+con.A*x0*x0+con.D*x0+con.F; * * return RealPolynomial.SolveQuadric(t2, t1, t0, 1e-9);*/ }
/// <summary> /// Gets the line geometries, one or two lines. The type of the conic has to be /// Intersecting lines (2 results), Paralell lines (2 results) or Coincident lienes (1 result), /// otherwise the result is null. /// </summary> /// <returns></returns> public Line2d[] ToLines() { // Inspired by line extraction conmat.c from book Graphics Gems V double xx, yy; ConicType type = Type; Line2d tmplin; Transform2d tr; GeneralConic2d cpy; List <Line2d> res = null; double de = B * B * 0.25 - A * C; if (MathUtil.IsZero(A) && MathUtil.IsZero(B) && MathUtil.IsZero(C)) { //single line // compute endpoints of the line, avoiding division by zero res = new List <Line2d>(); if (Math.Abs(d) > Math.Abs(e)) { res.Add(new Line2d(-f / (d), 0.0, -(e + f) / (d), 1.0)); } else { res.Add(new Line2d(0.0, -f / (e), 1.0, -(d + f) / (e))); } } else { // two lines cpy = new GeneralConic2d(this); double a = cpy.a, b = cpy.b * 0.5, c = cpy.c, d = cpy.d * 0.5, e = cpy.e * 0.5, f = cpy.f; //get matrix coefficient // use the expression for phi that takes atan of the smallest argument double phi = (Math.Abs(b + b) < Math.Abs(a - c) ? Math.Atan((b + b) / (a - c)) : MathUtil.Deg360 - Math.Atan((a - c) / (b + b))) / 2.0; //phi = cpy.Rotation; if (MathUtil.IsZero(de)) { //parallel lines tr = Transform2d.Rotate(-phi); cpy.Transform(tr); a = cpy.A; b = cpy.B * 0.5; c = cpy.c; d = cpy.d * 0.5; e = cpy.e * 0.5; f = cpy.f; //get matrix coefficient if (Math.Abs(c) < Math.Abs(a)) // vertical { double[] xs = RealPolynomial.SolveQuadric(a, d, f); if (xs != null) { res = new List <Line2d>(); foreach (double x in xs) { tmplin = new Line2d(x, -1, x, 1); tmplin.Transform(tr.Inversed); //back to original spacxe res.Add(tmplin); } } } else //horizontal { double[] ys = RealPolynomial.SolveQuadric(c, e, f, 0.0); if (ys != null) { res = new List <Line2d>(); foreach (double y in ys) { tmplin = new Line2d(-1, y, 1, y); tmplin.Transform(tr.Inversed); res.Add(tmplin); } } } } //end parallel lines case else { //crossing lines Point2d center = Center; double rot = this.Rotation; tr = Transform2d.Translate(-center.X, -center.Y) * Transform2d.Rotate(-rot); cpy.Transform(tr); a = cpy.A; b = cpy.B * 0.5; c = cpy.c; d = cpy.c * 0.5; e = cpy.e * 0.5; f = cpy.f; res = new List <Line2d>(); xx = Math.Sqrt(Math.Abs(1.0 / a)); yy = Math.Sqrt(Math.Abs(1.0 / c)); tr = tr.Inversed; tmplin = new Line2d(-xx, -yy, xx, yy); tmplin.Transform(tr); res.Add(tmplin); tmplin = new Line2d(new Line2d(-xx, yy, xx, -yy)); tmplin.Transform(tr); res.Add(tmplin); } //end crossing lines case } //end two lines if (res == null || res.Count == 0) { return(null); } return(res.ToArray()); }