Download ASSIGNMENT 4 General Comments Question 1: Create a Point2D

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ASSIGNMENT 4
COMP-202A, Fall 2012, All Sections
Due: December 4th, 2012 (23:30)
Please read the entire pdf before starting.
You must do this assignment individually and, unless otherwise specified, you must follow all the general
instructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the
value of this assignment for deviations from the general instructions and regulations. These regulations
are posted on the course website. Be sure to read them before starting.
General Comments
The main goal of this assignment is to familiarize you with object oriented programming and also to give
you practice with recursion and ArrayList.
This program will use graphics in Java. Some sample code is provided for you: Q1.java, Q2.java, and
DrawingPanel.java. These files can be found on the course website and should not be modified.
Question 1: Create a Point2D class (20%)
Create a new class that would encapsulate the concept of 2D point. The class header should be:
public class Point2D {
The class should have the following private properties:
private double x;
private double y;
The class will have one constructor that will take two doubles. The header of the constructors will be:
public Point2D(double x, double y);
The Point2D class should also have public methods for return the x and y coordinates, drawing the point
as a circle with radious R, and returning the midpoint between two points1; headers:
public double getX();
public double getX();
public Point2D midpoint(Point2D b);
1
For two points P1=[x1,y1] and P2=[x2,y2], the midpoint M=[(x1+x2)/2, (y1+y2)/2].
public void draw(Graphics g, int radius);
The midpoint method should produce a new point by averaging the x and y values of the object that the
method midpoint() was called upon (i.e. the this point), with the point passed as input, constructing a
new Point2D, and returning it.
For the draw method, you will need to use a method and class from the Graphics library. These are all
defined inside the package java.awt.* and so you must import these. The draw method will take as
parameter a Graphics variable g and the radius of the point. It should then, using the library method
below, draw and oval centered at the Point2D coordinate with height and width of radius. To do this,
you can use the method from the Graphics class:
g.fillOval(int x, int y, int radius, int radius);
You can learn more about this method at
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html#fillOv
al(int, int, int, int)
Remember that the x and y private properties of the Point2D class are doubles, and R is the radius of the
circle. Import the package:
java.awt.*
The following code can be used to test question 1. You must compile it along with
DrawingPanel.java (also found on the course website) along with your Point2D class.
import java.util.Random;
import java.awt.*;
public class Q1 {
public static void main(String [] args) {
int SIZE=512;
// initialize drawing panel
DrawingPanel p = new DrawingPanel(SIZE, SIZE);
p.setBackground(Color.CYAN);
Graphics g = p.getGraphics();
Random randomGenerator = new Random();
for (int i=0; i<1000; i++) {
double x= randomGenerator.nextDouble()*SIZE;
double y= randomGenerator.nextDouble()*SIZE;
Point2D point=new Point2D(x,y);
point.Draw(g,(int)(randomGenerator.nextDouble()*10));
}
}
}
Test code for Question 1 (found in the file Q1.java)
Sample output from the above program.
Use the code provided for the class DrawingPanel.
Question 2: Create a Triangle Class (20%)
Create a triangle class that stores three points of type Point2D into private property of type
ArrayList of Point2D. The header of the class should be:
public class Triangle {
The Triangle class should have one public constructor that takes as input three Point2D variables; the
header of the constructor should be:
public Triangle(Point2D p1, Point2D p2, Point2D p3);
In addition, the Triangle class should have public methods for returning the three points and for
drawing the triangle:
public
public
public
public
Point2D getP1();
Point2D getP2();
Point2D getP3();
void draw(Graphics g);
Draw will also take a Graphics variable g in order to perform the drawing. However you will also need
to use the Polygon class from the java.util package. Create a Polygon p:
Polygon p=new Polygon();
Add a Point2D point to the Polygon p:
p.addPoint((int )pnt.getX(), (int )pnt.getY());
Draw the Polygon p to using the Graphics g:
g.fillPolygon(p);
In order to draw the triangle (a polygon of three points) you will need to traverse the ArrayList
where the three vertices are stored. Note that since points is a private property, you may assume (if you
handle it correctly in other metohds), that there are exactly three Point2D references in it. You should
also import the packages:
java.awt.*
java.util.*
import java.awt.*;
public class Q2 {
public static void main(String [] args) {
int SIZE=512;
DrawingPanel p = new DrawingPanel(SIZE, SIZE);
p.setBackground(Color.CYAN);
Graphics g = p.getGraphics();
int triangleHeight = (int) Math.round(SIZE * Math.sqrt(3.0) / 2.0);
Point2D p1 = new Point2D(0, triangleHeight);
Point2D p2 = new Point2D(SIZE / 2, 0);
Point2D p3 = new Point2D(SIZE, triangleHeight);
Triangle T=new Triangle(p1,p2,p3);
g.setColor(Color.RED);
T.draw(g);
}
}
Sample code to test question 2 found in the file Q2.java
Sample output from the above program.
Question 3: Draw the Sierpinski Triangle (60%)
Sierpinski’s triangles are one of the simplest and also very interesting fractals shapes to construct. In this
assignment you are going to practice recursion by drawing the Sierpinski triangle fractal. The recursion is
performed by starting at a level bigger or equal to 1, and recursively drawing the triangle for ever
decreasing levels until level one is reached. For an arbitrary triangle defined by three points P1, P2, and
P3, you perform the recursion as follows. If the level is 1 then use the draw method implemented for
Question 2 to draw the triangle and then exit the method. This is the base case. If the level is greater
than one then calculate the three midpoints between the vertices of the triangle M1=midpoint(P1,P2),
M2=midpoint(P2,P3), and M3=midpoint(P3,P1). Then construct three new triangles T1=<P1,M1,M3>,
T2=<M1,P2,M2>, and T3=<M3,M2,P3>. Finally, call the drawSierpinski method three times one for each
of the new triangles for a level reduced by one.
import java.awt.*;
public class Q3 {
public static void main(String [] args) {
int SIZE=512;
// initialize drawing panel
DrawingPanel p = new DrawingPanel(SIZE, SIZE);
p.setBackground(Color.CYAN);
Graphics g = p.getGraphics();
int triangleHeight = (int) Math.round(SIZE * Math.sqrt(3.0) / 2.0);
Point2D p1 = new Point2D(0, triangleHeight);
Point2D p2 = new Point2D(SIZE / 2, 0);
Point2D p3 = new Point2D(SIZE, triangleHeight);
Triangle T=new Triangle(p1,p2,p3);
g.setColor(Color.MAGENTA);
int level = 5;
drawSierpinski(level,g,T);
}
public static void drawSierpinski(int level, Graphics g, Triangle t) {
// Add your Code HERE
…}
}
Sample code to test question 3 (posted in the file Q3.java which you should modify and put your
solution in)
Remember to use the midPoint method from Question1. The results should look like the following
pictures.
Level 1
Level 2
Level 3
Level 4
Level 5
Level 6
A few notes:
1. You may assume the initial Triangle passed as input to your method is a valid Triangle. (i.e. all
Point2Ds are displayed and the Point2Ds are not collinear)
2. You may assume the initial depth passed is a positive number.
What to submit:
You should submit your assignment on MyCourses. In order to do this, you will need to make a zip of the
file. You can do this on windows by following the instructions at this link:
http://condor.depaul.edu/slytinen/instructions/zip.html. On a mac or linux, you can find instructions at
http://osxdaily.com/2012/01/10/how-to-zip-files-in-mac-os-x/ You should submit a zip file called
Assignment4.zip with the following files inside it:
Point2D.java, Triangle.java, Q1.java Q2.java Q3.java
Confession.txt (optional): In this file, you can tell the TA about any issues you ran into doing this
assignment. If you point out an error that you know occurs in your problem, it may lead the TA to give
you more partial credit. On the other hand, it also may lead the TA to notice something that otherwise
he or she would not.