<< Chapter < Page Chapter >> Page >

Preview

In this lesson, I will explain programs that use a Turtle object to graph the following mathematical functions:

  • A straight line
  • A parabola
  • A cubic
  • A circle (two approaches)
  • A cosine

I will also show you how to graph a circle without using a Turtle object and compare the two approaches.

General background information

You learned about the World class, the Turtle class, and the Picture class in an earlier lesson titled Java3002: Creating and Manipulating Turtles and Pictures in a World Object . You learned the fundamentals of causing the turtle to move and to draw a linewith different colors and different line widths in the process. For review, Figure 1 shows an image that you learned to create in that earlier lesson.

Figure 1. Two turtles in a World object.

Missing image

In comparison, Figure 2 shows the sort of thing that you will learn to do in this lesson.

Figure 2. Graph of a cubic function.

Missing image

The blue line in Figure 2 shows a graph of the following cubic function in a Cartesian coordinate system:

y = x*x*x

The red lines in Figure 2 show the x and y axes.

The graph in Figure 2 was created by computing the x and y coordinate values (points) for a set of x values uniformly spaced along the x axis and causing the Turtle object to move and connect those points with a blue line. The Turtle object was made invisible.

As a practical matter, computing the coordinate values is relatively simple. The more complex problem is to get everything scaled properly to produce avisually pleasing graph in the space provided by the World object.

Discussion and sample code

I will explain six different programs in this lesson. Complete listings of those programs are provided in Listing 15 through Listing 20 in the section titled Complete program listings .

As is my custom, I will break the programs down and explain them in fragments. However, I will explain only those portions of the programsthat are new to this lesson. I will let the comments in the listings speak to those portions of the code that you should already understand.

Much of the code repeats from one program to the next. I will explain new code the first time that it appears in a program and won't explain that code in theexplanation of programs that follow.

A straight line

In this section, I will present and explain a program named Line01 that can be used to draw a straight line using the standard formula for a straight line given below:

y = slope*x + yIntercept

A complete listing of the program is provided in Listing 15 near the end of the lesson.

Program output for Line01

Before getting into the code, Figure 3 shows the program output.

Figure 3. Program output for Line01.

Missing image

As you can see, in addition to the red axes, this program graphs three different lines in different colors with different slopes and differenty-intercept values. I will touch on the slopes and the y-intercept values as I explain the code.

May need multiple points

At this point, I will explain a practical issue before it becomes a concern to you. As we all know, only two points are required to define a line.However the program that I will explain uses multiple points to define and draw a line. This requirement results from the behavior of a Turtle object when moving from one point to another point.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask