Results 1 to 40 of 42

Thread: [RESOLVED] Plot ellipse through apex and two other points

Threaded View

  1. #30

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Plot ellipse through apex and two other points

    EDIT: In the light of day, it was clear that the absurdly large absolute values of A, B and C must mean an error in my coding of the formula. Now testing with missing brackets added as in red below.

    Hi Jemediah,
    At last I got round to trying to put your advice of posts #21 and 23 (with the polar coordinate function) into practice. The following function returns an array of PointFs which can be plotted in GDI+ with DrawPolygon or DrawCurve:
    Code:
    	Public Shared Function FindEllipse3(P1 As PointF, P2 As PointF, P3 As PointF, E As Double, scaleFactor As Integer) As PointF()
    		'Translate the origin to P2:
    		Dim u As Single = P1.X - P2.X
    		Dim v As Single = P1.Y - P2.Y
    		Dim r As Single = P3.X - P2.X
    		Dim s As Single = P3.Y - P2.Y
    
    		'Calculate coefficients for the general ellipse equation:
    		'A = (s(r-u)v + E(rs-uv))/((r-u) (ru + sv))
    		Dim A As Double = (s * (r - u) * v + E * (r * s - u * v)) / ((r - u) * (r * u + s * v))
    		'B = (-(r - u)(su + rv) + E (-r^2 + s^2 + u^2 - v^2))/((r - u) (ru + sv))
    		Dim B As Double = (-(r - u) * (s * u + r * v) + E * (-r ^ 2 + s ^ 2 + u ^ 2 - v ^ 2)) / ((r - u) * (r * u + s * v))
    		Dim C As Double = 1 - A
    		'D = E(-s + v)/(r - u)
    		Dim D As Double = E * (-s + v) / (r - u)
    		'F = 0
    
    		'Plot points with the polar formula: r = sqrt( 1 / | a*cos2(th) + b*sin(th)*cos(th) + c*sin2(th) | )
    		Dim pfs As New List(Of PointF)
    		For i As Integer = 0 To 359 Step 5
    			Dim theta As Double = i * Math.PI / 180
    			Dim sin As Double = Math.Sin(theta)
    			Dim cos As Double = Math.Cos(theta)
    			Dim radius As Double = Math.Sqrt(1 / Math.Abs(A * (cos ^ 2) + B * sin * cos + C * (sin ^ 2))) * scaleFactor
    			Dim radX As Single = CSng(radius * cos)
    			Dim radY As Single = CSng(radius * sin)
    			pfs.Add(New PointF(radX + P2.X, radY + P2.Y))
    		Next
    		Return pfs.ToArray
    	End Function
    The value of E ranges from -1000 to 1000. ScaleFactor is a fudge needed to make the output visible; it typically takes a value of around 1 million. In other words, the values generated are on the small side (speaking logarithmically ) The typical output forms were a hyperbola or a "ninja star" like those below:
    Attachment 109499Attachment 109501
    No sign of an ellipse through P1 , P2 and P3, alas, at any value of E or relative positions of P1, P2 and P3. The value of D is not in fact being used, so maybe that indicates something wrong in the procedure or of my interpretation of it.

    you'll end up with a quadratic equation in r, which can be solved with the quadratic formula
    Now I am unsure what quadratic equation you refer to. Is it the one used to derive the polar coordinate formula I have used in the code above, with r representing the polar radius? I assume the quadratic formula is the well known (or well forgotten in my case) formula for solving ax^2 + bx + c = 0.

    I hope you can find time to look at this.
    cheers, BB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width