Channels ▼
RSS

Design

Ellipse Specification Using Vectors

Source Code Accompanies This Article. Download It Now.


Rendering Ellipses

A typical uniform spline is applied over a set of control points P1, P2, ..., Pi, ..., Pn-1, Pn such that segment i is denoted by points Pi-1, Pi, Pi+1, Pi+2; see Figure 6(a). The end segments are specified by doubling up the endpoints such as for the first segment: P1, P1, P2, P3. A TSpline-based ellipse is formed from a set of control points in a rectangular formation denoted by P1, P2, P3, P4. Each ellipse segment is derived from the control points in a round-robin scheme: P4 ,P1, P2, P3 for segment 1, P1, P2, P3, P4 for segment 2, and so on; see Figure 6(b).

Since the TSpline is defined by a periodic basis function, you can dispense with defining the ellipse as a piecewise function and simply render all four quadrants of the ellipse by choosing one of the four curve segment configurations and iterating over the interval of [0,4] rather than in four times over [0,1]. This is a cleaner, more efficient implementation scheme than piecewise rendering. This functionality has been subclassed off of the VectorEllipse class as VectorEllipse4 (available online at www.ddj.com/code/).

Figure 6: How spline segments of the ellipses are defined.

Demo Program

The demo program, specified by the EllipseDemo class (Listing One, available online), is a framework subclassed from the Java Frame class and derived from the paint program in Steve Holzner's book Java After Hours: 10 Projects You'll Never Do at Work. Each function is selected from a drop-down menu called Draw.

The Freehand Drawing function lets users draw a freeform curve against which to test ellipse-fitting using the two methods. It simply leaves a line following the path of the mouse when dragged across the drawing space and needs no further explanation.

The Box Ellipse function demonstrates an ellipse drawn from a bounding box using Java's built-in Ellipse2D.Double definition. The bounding box is drawn in red during editing for reference. To match this ellipse to a portion of a freeform curve, eyeball where the nearest corner of the bounding box of the ellipse falls to match the ellipse to the curve without the ellipse drifting away. (Not always an easy task!) See Figure 1.

The Vector Ellipse function demonstrates how to improve upon the standard bounding-box method by rendering ellipses drawn from a vector using TSplines. The VectorEllipse class (Listing Two, available online) is used to define and then render the ellipse. VectorEllipse is defined globally rather than locally in the place where it is used, as was Ellipse2D.Double. Had VectorEllipse been defined locally during use, it would have negated the benefits of precomputing the basis function in the setBasis method. During editing, the defining vector is drawn in red. To align a vector ellipse with curve, choose a point at about a 45-degree angle or at the halfway point about an ellipse quadrant for optimal results; see Figure 2.

A second Vector Ellipse function, Vector Ellipse 4, demonstrates the TSpline ellipse drawn in one piece rather than four. The VectorEllipse4 class (Listing Three, available online) defines and renders the ellipse. Subclassed from VectorEllipse, the setBasis, plotEllipse, and putEllipse methods are overridden to achieve this. A separate menu selection, Vector Ellipse 4 is provided to demonstrate this functionality. Its outward behavior is identical to its piecewise counterpart.

Performance

I developed this program on a Mac Powerbook G4 1.5-GHz PowerPC processor under Mac OS X v 0.4.3 using Java 1.4, which came installed in the system. On the G4 system, the program ran smoothly without screen flicker. I then tested the program on a Mac G3 iBook and PC Pentium 4 under Java 1.3. On both platforms, flickering was prominently notable, indicating a deficiency in speed.

Conclusion

The program I present here demonstrates an alternative method for rendering ellipses. This method is suited to the specific task of aligning ellipses to arbitrary curve segments using trigonometric splines (which, as yet, seems little known among most computer graphics practitioners). You can do a number of other things to facilitate matching ellipses to freeform curves, such as adjusting the ellipse through rotation and resizing. Since these methods are well understood and available in many graphical editing programs, I didn't include them here.

References

Holzner, Steven. Java After Hours: 10 Projects You'll Never Do at Work, Sams Publishing, 2006.

Kauffmann, Robert F. "Implementing Uniform Trigonometric Spline Curves," DDJ, May 1997.

Niemeyer, Patrick and Jonathan Knudsen. Learning Java, Third Edition, O'Reilly & Associates, 2005.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video