------------------------------------------------------------------------------
MC logo
Point Impl File
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Point Hdr File] [Point Impl File] [Point User File] [Point Class Hdr File] [Point Class Impl File] [Point Class User File] [C++ Integer Stack ADT] [C++ Integer Stack Implementation] [C++ Integer Stack Driver]
cpoint.cpp
/*
 * Struct implementation file for points.
 */
#include <iostream>
#include "cpoint.h"
#include <math.h>

using namespace std;

/* Fill in a point. */
Point mkpt(double x, double y) {
        Point p;

        p.xval = x;
        p.yval = y;

        return p;
}

/* Distance to another point.  Pythagorean thm. */
double dist(Point a, Point b) {
        double xd = a.xval - b.xval;
        double yd = a.yval - b.yval;
        return sqrt(xd*xd + yd*yd);
}

/* Add or subtract two points. */
Point add(Point a, Point b)
{
        return mkpt(a.xval + b.xval, a.yval + b.yval);
}
Point sub(Point a, Point b)
{
        return mkpt(a.xval - b.xval, a.yval - b.yval);
}

/* Move the existing point. */
void move(Point *p, double a, double b)
{
        p->xval += a;
        p->yval += b;
}

/* Print the point to standard output.  The class ostream is a base class
   for output streams of various types. */
void print(Point p)
{
        cout << "(" << p.xval << "," << p.yval << ")";
}