Changed Monday 12th November
This is not a major change; you will not need to modify your work in
any way. I accidentally added an extra requirement for the shape class.
This HTML page originally said that your shape class must include a method
float dispance(point p). That was an editing error,
I didn't state that requirement in class, so you don't have to do it.
Finding the distance from a shape to a point is not as easy as you
might imagine.
If anyone actually implemented that method, I'll give you extra credit
for it.
Assignment 4
Due Tuesday 13th November.
This assignment has three parts, plus an optional fourth for extra credit.
Part A
In
programming terminology, a Vector is an object that behaves exactly like
a normal array, except that its size can be changed at any time, and accesses to the
contents are protected, so that improper accesses (for example trying to use A[-3])
do not crash the program.
Implement
a Vector-of-ints object (almost exactly as we investigated it in class). Use your
object definition in a program that thoroughly tests it.
Part
A should be a separate
program, there is nothing to gain by trying to combine it with parts B and C.
Part B
Implement
a class representing a geometric point in 2-dimensional space (that is,
it has two components, its x co-ordinate and its y co-ordinate). Very
much like the second question of the recent mid-term.
Make
sure that your definition
includes all appropriate constructors, etc. It must include the following
required methods: - void scale(float factor)
- void rotate(float angle)
- void translate(float xshift, float yshift)
- float distance(point p)
-
void print()
Rotation and
translation are of course relative to the origin (0,0), angles are in radians.
Part C
Implement
a class representing a geometric shape in 2-dimensional space. A shape (in this case)
is just a connected collection of straight lines, and can be completely described
by the co-ordinates of its vertices (i.e. corners). You should implement a shape
as a vector of points, so part C should be combined with part B into a single
program.
Your
shape class must include
the following
required methods: - void scale(float factor)
- void rotate(float angle)
- void translate(float xshift, float yshift)
- float perimeter()
- float area()
- void print()
Note that the perimeter and area methods are new to part C (they are not part of the
requirements for part B). As an example, this snippet of code shows a square (between x=1 and x=2
and y=3 and y=4) being defined. The calculated area should be 1, and the perimeter 4.
{ shape s;
s.add(new point(1,3));
s.add(new point(1,4));
s.add(new point(2,4));
s.add(new point(2,3));
float a=s.area();
float p=s.perimeter();
printf("shape ");
s.print();
printf(" has area %f and perimeter %f\n", a, p); }
Optional Part D for extra credit only
and implement some useful methods
Design
that find the intersections between two shape objects. One possibility is to
find out whether two shapes intersect at all; another is to find out how many intersection
points there are; another would be to find all the points of intersection, and construct
a new shape that represents the whole area of intersection (although this is quite tricky).