QuestionQuestion

Purpose:
* Demonstrate ability to clip lines using the Cohen-Sutherland algorithm.
* Demonstrate ability to write some additional (simple) C code.

#----------------------------------------------------------------------
Background:

* The Cohen-Sutherland algorithm is explained in the handout posted to Canvas in the Modules section.
* The goal for this homework is to read lines (from a ".line" file) and clip them using the Cohen-Sutherland algorithm and the supplied view parameters (from a ".view" file). The clipped lines are then printed (along with the canvas size) to create a viewable ".sdf" file.

* Line files have the following format. (The basic structure is the same as the SVFM file, just with different entries.)

    - Blank lines are to be ignored. (A blank line has no characters at all or is just whitespace.)

    - Lines with '#' as the first non-whitespace character are comments and are to be ignored.

    - A line with an 'l' as the first non-whitespace character is a line line (ha!). There will be four numbers following the 'l': the p1X, p1Y, p2X, p2Y coordinates of the tw endpoints that define the line. The 'l' and the coordinates will be separated by whitespace. Example:

       l 237.4    123   8      96.1

      Notice that the coodinates may be either integer or floating point numbers.

    - If a line starts with any other non-whitespace character, it should be silently ignored. ('silently' means you should not print an error message. Just ignore it.)

* View files are as described in the previous homework.

#----------------------------------------------------------------------
Tasks:

    1. There are a number of TODO: comments in the .c files that need to be replaced with the appropriate C code. (Do NOT change anything in any .c file except to update the headers and to replace a TODO comment with your code.)

    Here's a list:

hmwk_03.c:46: // TODO: Read each line from the proposed line file (it was ...

line.c:24:    // TODO: Implement the clipLine algorithm here. ...
line.c:61:    // TODO: Print an 'l' line for the given Line l. ...
line.c:67:    // TODO: Implement the region code computation here. ...

    The Template/view.c file also has a couple of TODO spots in it, but those are left over from Homework 02. Just use the view.c file you created for that previous homework.

7. When you have done so, compile and run everything as shown.

    Notice that there are two input files being given to hmwk_03, the first is the line file (Tests/pyramid_01.line) and the second is the view file (Tests/pyramid_01.view).

    You should observe the given output.

$ gcc -o hmwk_03 hmwk_03.c line.c view.c
$ ./hmwk_03 Tests/pyramid_01.line Tests/pyramid_01.view
#- View parameters ---------------------
# B├ęzier resolution :    0
# Euler angles      :      0.000000,      0.000000,      0.000000
# World limits      :    -1.000000,    -1.000000,      1.000000,      1.000000
# Camera distance   :      0.000000
# Canvas width      :   500
# Canvas height    :   400
# Screen limits    :      0.100000,      0.100000,      0.900000,      0.900000
# Portal pixels    :   50, 450,   40, 360
#---------------------------------------
c 500 400
l    50.0    40.0    450.0    40.0
l    450.0    40.0    450.0    360.0
l    450.0    360.0    50.0    360.0
l    50.0    360.0    50.0    40.0
l    410.0    328.0    410.0    200.0
l    410.0    200.0    250.0    200.0
l    250.0    200.0    410.0    328.0
l    250.0    328.0    410.0    328.0
l    410.0    328.0    250.0    200.0
l    250.0    200.0    250.0    328.0
l    410.0    200.0    330.0    264.0
l    330.0    264.0    250.0    200.0
l    250.0    200.0    410.0    200.0
l    330.0    264.0    250.0    328.0
l    250.0    328.0    250.0    200.0
l    250.0    200.0    330.0    264.0
l    410.0    200.0    410.0    328.0
l    410.0    328.0    330.0    264.0
l    330.0    264.0    410.0    200.0
l    250.0    328.0    330.0    264.0
l    330.0    264.0    410.0    328.0
l    410.0    328.0    250.0    328.0
#- Line statistics ---------------------
#    22 lines drawn.
#      0 lines rejected.
#---------------------------------------
$

    Your output should match this EXACTLY. 'EXACTLY' means just that. It should be the same character-by-character. When your program is tested, it will be run against other data files and the output compared using diff.

8. There are a number of line files in Tests/ along with a view file for each. Run all of these tests and compare your results to those given in Results/. Your output should match EXACTLY.

9. Use diff (fc on Windows) to compare your output to that of the results file. They should match with NO DIFFERENCES.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

//----------------------------------------------------------
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "line.h"
#include "view.h"

//----------------------------------------------------------
int main( int argc, char *argv[] )
{
if ( argc != 3 ) {
    fprintf( stderr, "Usage: %s <lineFileName> <viewFileName\n", argv[0] );
    exit( 1 );
}

char *lineFileName = argv[1];
char *viewFileName = argv[2];

//--------------------------------------
// Load the view information. We're interested in the canvas
// size and the portal dimensions (both measured in pixels).
View *v;

v = loadView( viewFileName );
dumpView( v );

//--------------------------------------
// Print the canvas size for the SDF file.
printf( "c %4d %4d\n", v->m_width, v->m_height );

//--------------------------------------
// Open the file with proposed lines to draw.
FILE *fp = fopen( lineFileName...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$45.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats