# 3.4 Java3003: drawing graphs with turtles and pixels  (Page 2/13)

 Page 2 / 13

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

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.

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.

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.

what is the meaning of an idiosyncratic pattern
I am here for the first time just here to learn...
hi I'm new on here first time
Lisa
hello
GOPAL
hi,am new here
jennifer
what is this group all about
jennifer
Suppose an individual with OCD experiences obsessive thoughts about germs, contamination, and disease whenever she encounters a doorknob. What might have constituted a viable unconditioned stimulus?
What are factors that influence learning?
Environment Heredity(I am not sure about heredity)
Tusita
Enos
Sure, anytime
Tusita
I have other questions also
Enos
Based on the factors affecting learning, how do we improve learning
Enos
I think that addressing that everyone learns in their own time
James
Peer group influence can also be another reason
Sorie
Also knowing what's going on at home. what pressure are the parents putting on them.
James
am I close or no
James
also is there a lack of care? going one more step. with peer groups do an activity that shows how much they have in common
James
under what schedule of reinforcement do animals learn from ?
the role of emotional intelligence in a courtship behavior
1
describe the task and roles of industrial psychologist in solving the problems
hi I'm Vaishnavi and I'm new to psychology:)
what is motivation
getting passionate about something and strive for success
Leanne
observation and method based
the meaning of science
Winter
soul is the Greek word for psyche
Winter
psychology is scientific research of the human brain.
Winter
Psychology is a study of human mind and behaviour
Vaishnavi
psychology is a social science which aim at studying the mind as it affect the behavior of an individual.
Prince
write and explain four psychosexual stages of personality development
I'm asking on behalf of someone How can a guy get over his weakness for girls? Like the person have soft spot for every girl and he thinks he love them then suddenly stuff change.... Like a switch
yes
Beverly
whats the age of the boy?
Beverly
16 or 17
Ajayi
lol. it gets over with time. it's not something that can be changed instantly. and for teenagers.. anyone of opposite sex is a tasty lollipop.
Alex
yes in time it will go away but could also take years
Beverly
maybe because he doesn't really have mind of making decisions at the moment on what he need and what he doesn't want
Nwokwu
maybe he's trying to find someone to love because he's either lacked loved and feel that's the only way to feel loved or he's lonely and feels a girl can make it better . either he's got deal with the core issue
Jonan
its natural. I think it should be like this. girls also have weakness toward guys. but you can overcome on few things like concept of beauty. first step is realisation which will grow in you. also discipline help in these things.
Sujeet
what are cluster A disease?
because psychology is a natural science as well as a science. it's a interdisciplinary subject
yes absolutely we cabt out Psychology completely into science dur to its various measuring aspects
utkarsh
cannot* put*
utkarsh
Assess the relationship between heredity and environment in human development
Got questions? Join the online conversation and get instant answers!