首页 > > 详细

讲解留学生Python语言、Python编程解析、讲解留学生Python语言


PLEASE NOTE: This is a graded assessment of individual programing understanding and ability, and is not
a colaborative assignment; you must design, implement and test the solution(s) completely on your own
without outside assistance from anyone. You may not consult or discuss the solution with anyone. In addition,
you may not include solutions or portions of solutions obtained from any source other than those provided in
class. Note that providing a solution or assisting another student in any way on this examination is also
considered academic misconduct. Failure to heed these directives wil result in a failing grade for the course
and an incident report filed with the Office for Student Conduct and Academic Integrity for further sanction.


A. (100 points) Mandelbrot Fractals
This examination consists of designing and writing a single wel-structured Python program that must be
comited and pushed to your remote GitHub examination repository prior to the deadline.

Late submissions wil not be accepted and wil result in a zero score for the exam.

TA help for this examination wil not be provided. If you have clarification questions, they must be addressed
to the graduate TA for the class.

The total point value wil be awarded for solutions that are complete, corect, and wel structured. A "wel
structured" program entails good design that employs meaningful functional decomposition, appropriate
coments and general readability (descriptive names for variables and procedures, appropriate use of blank
space, etc.) If you are not sure what this means, review the "well-structured" program requirements provided in
Lab2.

Note that your work wil be graded using, and must function corectly with, the curent version of Python 3 on
CSE Labs UNIX machines. If you complete this programing exam using a diferent system, it is your
responsibility to ensure it works on CSELabs machines prior to submiting it.

The rubric includes the folowing specific (acumulative) point deductions:
• Mising academic integrity pledge -100 points
• Syntax errors -50 to -100 points
• Misnamed source file or incorect repository -25 points
• Use of global variables -25 points
• Mising main program function -25 points
Examination Repository
Examination files must be submited to GitHub using your remote exam repository. Exam repositories have
already been created for each registered student and are named using the string exam- followed by your X50
userID (e.g., exam-smit1234). You must first clone your exam repository in your local home directory
before submiting the materials for this exam. If you are having dificulty, consult the second Lab from earlier
in the semester or the GitHub tutorial on the class webpage. If your exam repository is mising or something is
amis, please contact the graduate TA. DO NOT SUBMIT YOUR EXAM FILES TO YOUR LAB/EXERCISE
REPOSITORY!
Introduction
For this project, you wil employ basic Object Oriented Programing techniques to construct a program that
displays a Mandlebrot fractal image using Turtle graphics. The program wil allow the user to "click" anywhere
on the fractal image to "zoom-in", demonstrating the self-similarity of complex fractals. The program wil
consist of thre separate classes that interact to construct and display a Mandelbrot fractal.

Background
Fractals are mathematical sequences that are "made of parts similar to the whole in some way". When we
graph the values of complex fractal sequences on an x,y Cartesian plane, they often produce stuning visual
images. Check out the folowing Wikipedia description:
https:/en.wikipedia.org/wiki/Fractal
Enlarging ("zoming-in") on fractal images wil often reveal "self-similar" paterns, i.e., smal paterns that look
exactly like the original.

One famous fractal image is derived from the so-called Mandelbrot set which is based on a curious property
exhibited by Mandelbrot sequences. A Mandelbrot sequence is constructed by starting with an arbitrary
complex number, C
0
. Each subsequent value of the sequence is computed from its predecesors according to the
folowing equation:

For an arbitrary complex number C
, the Mandelbrot sequence is an infinite series in which the first value is C
the next value is C
added to the square of the previous value in the sequence, and so on.
The values in most Mandelbrot sequences wil grow infinitely. However there is a special subset of the starting
values, C
that result in Mandelbrot sequences that remain bounded within a finite region! The colection of all
such starting values, C
, is referred to as the Mandelbrot set. The sequences formed from the starting values in
the Mandelbrot set are all bounded by a finite region in the complex plane and only contain complex numbers
whose absolute values les than 2.

When plotted on an x,y Cartesian plane, the Mandelbrot set produces stuning fractal paterns such as this one
(from wikipedia.org):
Drawing Mandelbrot Fractals
We can visualize the Mandelbrot set by graphing it on the complex plane instead of the real plane. Throughout
the semester we've been using Turtle graphics to graph numbers in the real plane. If we pased two real
numbers to turtle.goto(x, y) the turtle would go the position x on the x-axis and the position y on the
y-axis. The complex plane is similar to the real plane, however we no longer think in terms of x and y axes.
Instead, we wil let the horizontal axis corespond to the real part of a complex number and the vertical axis
corespond to the imaginary part of a complex number.
In this way, we can match each of the x,y turtle coordinates on a scren to an equivalent point on the complex
plane and then test each complex point for membership in the Mandelbrot sequence. If the complex point
represents a starting value, C
 
for a Mandelbrot sequence that always produces a number whose absolute value
is 2) before this limit is reached, then C
is not a member of the
set. If all of the values up to Z
limit
have absolute values 2.
Part 1: Complex Number Clas
In order to create a Mandelbrot sequence, we must be able to represent complex values and compute complex
expressions. Although Python provides a built-in numeric type: complex that suports operations on complex
numbers, assume that such a class does not exist and create your own user-defined complex number class to use
in Part 2. Note that a complex number is of the form. a + bi where a and b are real numbers and i
example, 2.4 + 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number
and bi the imaginary part. For more information on complex numbers refer to the Wikipedia page:
Create and (thoroughly test!) a class named Complex to represent complex numbers. Save your class
definition in a module named complex.py. The members of this class are as folows:
Instance variables:
• Private real and imag of type float to represent the complex number real + imag i
Methods:
• A constructor that wil initialize real and imag from user suplied arguments (default to zero)
• An overloaded __repr__ method that wil return the complex value as a string in this form, e.g.:
2.3 + 4.6i
Note that if the imaginary component is zero, omit the "+ xi" part (i.e., display the number as a non-
complex "float", and if the imaginary component is negative, replace the '+' with '-'.
• Accesor and mutator methods for both instance variables
• An overloaded __add__ (addition) operator that wil return the sum of two Complex objects. For
complex numbers a + bi and c + di, addition is defined as: (a+c) + (b+d)i.
• An overloaded __mul__ (multiplication) operator that wil return the product of two Complex
objects. Note that the product of 2 complex numbers is determined using distributed multiplication
(as in multiplying two polynomials):
• An overloaded __abs__ (absolute value) operation that wil return the absolute value of a complex
number. Note that the absolute value is the distance from the origin of the complex plane (0 + 0i )
to the complex number (HINT: "pythagoras").
Part 2: Mandelbrot Sequence Clas
After you have your complex number class working (and tested!), construct a separate class to represent
Mandelbrot sequences. The class name is Mandelbrot and should contain the folowing members:
Instance variables:
• Private limit (type int) that represents the maximum length Mandelbrot sequence
• Private colormap, a list of strings containing the Turtle color values used to color each point
• Private cardinality (type int) that indicates the number of consecutive complex values in the
computed sequence (starting from 0) whose absolute value is "in bounds" (i.e., <= 2)
Methods:
• A constructor that wil take two arguments: the starting value, C
0
for the Mandelbrot sequence (type
Complex) and the maximum length sequence to be computed (type int, defaults to 50). The
constructor should compute the Mandelbrot sequence for the given starting value and initialize the
instance variables.
• An accessor method named get_color that wil return a color from the color map coresponding
to the cardinality of the set. Lower cardinality values (further from the Mandelbrot set) wil
corespond with colors earlier in the list. A cardinality value equal to the maximum (limit)
should corespond to the color 'black'.

Part 3: Graphical Display Clas
After you have your Mandelbrot class working (and tested!), construct a third separate class named
Display to handle the graphical display details. The Display class should instantiate a turtle object and
handle on-click operations. Your class must include the folowing:
• A constructor method that takes no arguments and handles hiding the turtle, making the turtle canvas
square, seting the turtle sped to zero, changing the `tracer` so that the program can draw more
quickly, seting up the on-click action, and drawing the first Mandelbrot fractal at the default zoom.
The default zoom should draw the complex point -2 - 2i in the bottom left corner, and the complex
point 2 + 2i in the top right corner. You must keep track of the bounds of the visible part of the
Mandelbrot set as instance variables and update them as the fractal is zoomed.
• A mutator method named click: that is called whenever the user clicks on the scren. (On-click
actions are described in more detail in the Python 3 Turtle documentation). If a Mandelbrot set is
curently being drawn, this method should do nothing. If the turtle is not curently drawing the
Mandelbrot set, the zoom operation should be performed and the fractal redrawn.
• A mutator method named zoom that modifies the complex plane coordinates. This method should
modify the complex plane coordinates and redraw the Mandelbrot set so that it is magnified 2x and re-
centered around the point the user clicked. This wil halve the width and height of the viewable section
of the Mandelbrot set. In order to accomplish this you should determine which point in the complex
plane was clicked, then recalculate the boundaries of the viewable section of the plane by making that
point the center of the new grid and making the new width and height of the grid half of the old width
and height.
• A mutator method named draw that wil re-draw the Mandelbrot fractal at whatever the curent
resolution is set to. This method should visit each x, y coordinate on the turtle canvas, translate that
coordinate onto the zoomed section of the complex plane, and color it according to how close it is to
membership in the Mandelbrot set (se sugestion 1 below). Computing membership in the
Mandelbrot set for each pixel on the canvas can significantly slow down your computer. Limiting the
length of the sequence to 50 iterations wil sped it up, but in order to draw your fractal as quickly as
possible, it is recomended that you do NOT accomplish the drawing using the .dot or .stamp
methods. Instead, color each pixel (x, y) by putting the pen down, changing the pen to use the
appropriate color, and moving the turtle to a pixel close by (i.e., x, y + 1), which wil paint the canvas
with approximately one pixel worth of color.

General Program Requirements
Using Turtle graphics, write a wel-structured Python program that wil display the fractal Mandelbrot set.
Your program wil consist of 3 class definitions as described and a single main() function. The main function
simply needs to instantiate a single Display object to display the interactive graphics.

Your program must do the folowing:
• Include the academic integrity pledge in the program source code (details below)
• Use turtle graphics to implement the graphical interface (set the turtle sped to 0 in order to sped
things up).
• Your program should have a main function as described in class. Runing your program from the
comand line should automatically draw the Mandelbrot set at the default zoom and set up the scren
to accept user clicks.

Submision
Your Complex and Mandelbrot classes should each reside in separate modules named complex.py and
mandelbrot.py respectively. Your main program should include a main() function and reside in a
module named fractal.py that includes your Display class. Comit and push all thre modules to your
exam repository. Please be sure to observe the naming requirements.
Constraints:
• You may use any built-in Python object class methods (string, list, etc.) except the built-in complex
number class
• You may use imported functions and class methods from the turtle module only.
• Do not use the .setworldcoordinates turtle method

Academic Integrity Pledge
The first lines of your program must contain the folowing text (exactly as it appears below), replacing the first
line with your ful name and X50 ID. If this statement does not appear in your program source, you wil
receive a score of zero for the exam:

Suggestions:
1) The proces of plotting employs a nested loop that systematicaly moves the turtle to every pixel of the
display. As the turtle moves, it wil draw a smal (1 or 2 pixel) line in the color specified by the colormap for
the Mandelbrot sequence. Note that the turtle must be moved in x,y pixel coordinates, but the Mandelbrot
sequence that each pixel represents must be computed in complex number plane coordinates. This requires a
conversion from pixel coordinates (x,y) to complex plane coordinates (real, imaginary).

Consider the example figure below for a 300 by 300 pixel turtle window. The center coordinates of the turtle
window are 0,0 which corespond to 0+0i in the complex plane. The lower leftmost pixel is at -150, -150 which
coresponds to the -2-2i in the complex plane.

The conversion is straightforward based on the folowing scale relationship:
Example Program Output:

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!