# How to draw an ellipse in ruby?

I’d like to draw a portion of an ellipse from a ruby script. From basic geometry, given some combination of foci, center, eccentricity, etc., I can plug into a standard ellipse equation and compute point locations. But, what I’d like to do is generate the equation from the following 4 pieces of information: Point1, slope of the tangent line at Point1, Point2, slope of the tangent line at Point2. Using that information, I tried solving analytically for a and b (major/minor axes), and center (h,k). Further investigation suggested I needed a fifth piece of information, so I eyeballed where the center would be. I couldn’t get this to work either. I then posted to a math site http://math.stackexchange.com/questions/1885315/equation-for-an-ellipse-from-2-points-and-their-tangents. Solutions presented were beyond my skills. So how might I draw an ellipse from Ruby? Thanks.

I looked at the page you referenced, and the first thing I noticed was that he observes that there is no unique solution to the problem you posed, there is an infinite family of ellipses that could match the points and slopes. You have to add another constraint to choose one of them. He suggests that you choose minimum eccentricity, which is easy but might or might not be what you want.

But if you go that way, his approach is to create a circle of diameter 2 and centered at 1,1,0 on the x,y plane (Entities#add_circle in the SketchUp Ruby API) and then transform it using an affine transformation (Geom::Transformation). He presents a 3x3 transformation matrix to do the trick for the 2D problem. SketchUp uses a 4x4 matrix in 3D, so you have to insert a row and column for the z axis. They are the ones for an identity matrix since you are working in the x,y plane. Make a Transformation from the matrix (that’s one of the supported constructors) and apply it to the circle’s entities. Finally, delete the segments you don’t want (alternatively, you could just draw the arc between 0,1,0 and 1,0,0 and transform it).

Because SketchUp represents circles and arcs using a sequence of straight edges, it may happen that the first and last edges of the arc won’t be truly tangent the way you wanted. You can improve this by using more segments or you can rotate the original circle so that its start and end segments are parallel to the axes.

It is actually the `Geom::Transformation` class.

Oops! Typed too fast…edited the original to avoid creating a problem for anyone who only reads that far.