## Homework 4

Read Chapter 11 in the text because next Monday we will be discussing Splines.

Also, do the assignment below. When you have finished the assignment, email your source code to the grader at wanghua@cs.nyu.edu, and post the working applet onto the web.

Write an applet which displays at least two convex polygons. If the user pushs the mouse button down within of one of these polygons, and then drags the mouse around, the polygon moves accordingly (until the user lifts the mouse). If the user drags within the intersection area, then move both polygons around together.

Display the two polygons in two different fill colors, and display their mutual intersection in a third color. You can make the two polygons any convex shapes you like. Go ahead and make creative shapes if you prefer.

To do the above, you should create a convex Polygon class, I recommend you do this by extending class java.util.Vector. Your class should contain the following methods:

• void add(Point p) // append a vertex
• Point get(int i) // return the Point at the ith vertex
• void insert(int i, Point p) // insert a new vertex before the ith vertex
• void remove(int i) // remove the ith vertex
• void removeAll() // remove all vertices
• void set(int i, Point p) // set a different value for the ith vertex
• void set(Polygon p) // copy from another polygon
• int size() // return number of vertices
• double x(int i) // get x coord of ith vertex
• double y(int i) // get y coord of ith vertex
• void draw(Graphics g) // draw polygon outline
• void fill(Graphics g) // fill polygon region
• double area() // return polygon's area
• void intersect(Polygon p) // intersect with another polygon
• boolean contains(Point p) // does it contain the specified point?

You should also create the following classes:

• Point
which has constructor:
• Point(double x, double y)
and access methods:
• public String toString() // return "{x,y}"
• double x() // return x coordinate
• double y() // return y coordinate

• HalfPlane
which has constructor:
• HalfPlane(Point p, Point q)
and evaluation method:
• double eval(Point p)
and which internally maintains coefficients a, b and c. Method eval(Point) should evaluate a linear function, returning positive values for points to the left of the line going from p to q.

The HalfPlane class will let you implement Polygon.intersect(Polygon) by implementing a Polygon.intersect(HalfPlane) method, which you can repeatedly invoke to intersect with the entire Polygon.

It will also let you implement Polygon.contains(Point) by successively invoking HalfPlane.eval(Point) for the HalfPlane defined by each of the polygon's successive edges, and returning true if all these evaluations return non-negative.