Programming Assignment 3 - Regular Polygons

Advanced Programming II, Spring 2004


Due Date

This assignment is due by 2:00 p.m. on Tuesday, 16 March.

See the assignment turn-in page (last modified on 9 February 2004) for instructions on turning in your assignment.

Background

An n-point regular polygon is defined by placing n points equally spaced around the circumference of a circle and then joining adjacent points by lines. For example, an equilateral triangle is a 3-point regular polygon, a square is a 4-point regular polygon, and so on.

The Problem

Write a program that accepts from std-in a set of planer integral points and writes to std-out all the points defining n-point regular polygons.

Input

Input will be a sequence of numbers interpreted as a sequence of n points:

X1 Y1 X2 Y2 . . . Xn Yn

Adjacent numbers are separated by at least one space character; space characters in excess of those needed to separated adjacent numbers should be ignored.

Because the numbers are describing coordinate points, there should be an even number of them; if there isn't, the input is invalid.

Here as example input:

0.0583 -0.4999 0.4622 -0.1994 
0.4039 0.3005		-0.0582 0.5 
-0.4621 0.1995 -0.4038
    -0.3004

Output

All regular polygons found should be written to std-out by increasing number of sides. Each set of polygons should be output with the following format:

s
X11 Y11 ... X1s Y1s
...
Xj1 Yj1 ... Xjs Yjs

where s is the number of sides in the polygon, j is the number of s-sided polygons found and (Xki, Yki) is the i-th point of the k-th polygon.

All points for the same polygon should appear on one line; points may be ordered arbitrarily. Adjacent coordinates should be separated by at least one space character.

Given the example input in the previous section (which describes a regular hexagon), the output is

3
0.058 -0.500 -0.462 0.200 0.404 0.300 
-0.404 -0.300 -0.058 0.500 0.462 -0.200 
6
-0.404 -0.300 -0.462 0.200 -0.058 0.500 0.404 0.300 0.462 -0.200 0.058 -0.500 

Should an error occur, an informative, single-line error message starting with "! " (that's a bang followed by a space) should be written to std-err. Processing ends immediately after the error message is printed.

Programming Tip

It is a fatal programming mistake to directly compare two floating-point numbers. Given all the errors that can occur when manipulating floating-point numbers, it is virtually certain that two floating-point numbers will never be equal.

You should consider two floating-point numbers to be equal if they are within 0.0001 of each other; that is, x == y if (and only if) fabs(x - y) < 0.0001. Use a similar definition for less than.

Testing

The program gen-polygons writes regular polygons to std-out. The command format is

./gen-polygons [-sn]...

where -sn is an option that causes gen-polygons to output an n-sided if n is an integer. If n is a question mark (?), gen-polygons generates a random number between 3 and 12 inclusive and then generates a polygon of that many sides. The -s option may be repeated; a polygon is generated for each repetition. For example, the command

./gen-polygons -s3 -s? -s12

Generates three regular polygons, one with three sides, one with twelve sides, and one with a randomly selected number of sides.

Because gen-polygons randomly generates polygons each time it's run, you may find it more convenient to send the output of gen-polygons to a file and then redirect the file into your program's std-in:

$ ./gen-polygons > in

$ ./rpolys < in

gen-polygons can be found in the assignment directory

/export/home/class/cs-509/pa3

You can use the program see-polygons, available in the assignment directory, to view your program's output. see-polygons takes a single command-line argument, which is the name of the file containing the points input to your program. see-polygons reads std-in for your program's output, then displays the points. The points input to your program are black, and the polygons found by your program are red.

Probably the simplest way to use see-polygons is like this:

$ gen-polygons > in

$ ./rpolys < in > out

$ see-polygons in < out

If you used gen-polygons to generate your program's input, then every input point (black spot) should contain a red spot (polygon vertex), and every red spot should be within a black spot (and the red spots should form regular polygons).

see-polygons uses the X Window System, which means you have to be sitting in front of a machine that's running X to use it. Unless you do something special (such as install cygwin) you can't use see-polygons when sitting in front of a machine running Microsoft software. You should be able to use see-polygons when sitting in front of CS lab or Linux lab machines.

You can find my answer to the assignment in the same directory. Remember, the objective of this assignment is to find regular polygons; the objective is not to faithfully reproduce the behavior of my solution. If my solution's wrong and you copy the error, you're going to lose points.


This page last modified on 12 March 2004.