pmc/vector.cc
changeset 42 83b8151b966d
child 43 cf8c1b5127b2
equal deleted inserted replaced
41:574503cf7bb0 42:83b8151b966d
       
     1 /**
       
     2  * test/demos/pmc/vector.cc
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <cmath>
       
     7 #include <vector.h>
       
     8 
       
     9 #include <cstdio>
       
    10 
       
    11 Vector::Vector (int xx, int yy)
       
    12 {
       
    13     name = "Vector";
       
    14     x = xx;
       
    15     y = yy;
       
    16     mode = RAD;
       
    17 
       
    18 #ifdef DEBUG
       
    19     fprintf (stderr, "New Vector: (%.3d, %.3d)\n", x, y);
       
    20 #endif
       
    21 }
       
    22 
       
    23 Vector::~Vector ()
       
    24 {
       
    25 #ifdef DEBUG
       
    26     fprintf (stderr, "Bye, bye Vector: (%.3d, %.3d)\n", x, y);
       
    27 #endif
       
    28 }
       
    29 
       
    30 int Vector::X ()
       
    31 {
       
    32     return x;
       
    33 }
       
    34 
       
    35 int Vector::Y ()
       
    36 {
       
    37     return y;
       
    38 }
       
    39 
       
    40 Vector Vector::operator+ (Vector a)
       
    41 {
       
    42     return Vector (x + a.X (), y + a.Y ());
       
    43 }
       
    44 
       
    45 Vector Vector::operator- (Vector a)
       
    46 {
       
    47     return Vector (x - a.X (), y - a.Y ());
       
    48 }
       
    49 
       
    50 double Vector::abs ()
       
    51 {
       
    52     return (std::sqrt (x * x + y * y));
       
    53 }
       
    54 
       
    55 void Vector::vector ()
       
    56 {
       
    57     fprintf (stderr, "(%.3d, %.3d)", x, y);
       
    58 }
       
    59 
       
    60 double Vector::angle (Vector v)
       
    61 {
       
    62     if (mode == DEG)
       
    63         return ((180.0 / M_PI) * std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ())));
       
    64 
       
    65     return (std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ())));
       
    66 }
       
    67 
       
    68 void Vector::setMode (Mode m)
       
    69 {
       
    70     mode = m;
       
    71 }