<< 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.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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