Design Patterns Java™ Workbook

(Michael S) #1
Chapter 4. Facade

the complete equation for y:


Figure 4.2 shows the equations for a parabola in action.


Another advantage of parametric equations is that there is no problem drawing
curves that have more than one y value for a given xvalue. Consider drawing a
circle.


The equation for a circle with a radius of 1 is:


or


Handling the fact that two y values emerge for every xvalue is complicated. It's also
difficult to adjust these values to plot correctly within a Graphicsobject's height
and width. Polar coordinates make the function for a circle simpler:


These formulas are parametric equations that show x and yas functions of a new
parameter, theta. Theta represents the sweep of an arc that varies from 0 to 2 pi as a
circle is drawn. You can set the radius of a circle so that it will fit within the height h
and width w of a Graphics object. A handful of parametric equations suffice to
plot a circle within the bounds of a Graphics object:


Translating these equations into Java produces the circle shown in Figure 4.3.

Free download pdf