pmc/rectangle.cc
changeset 42 83b8151b966d
child 43 cf8c1b5127b2
equal deleted inserted replaced
41:574503cf7bb0 42:83b8151b966d
       
     1 /**
       
     2  * test/demos/pmc/rectangle.cc
       
     3  * Copyright (C) 2008 Markus Broeker
       
     4  */
       
     5 
       
     6 #include <rectangle.h>
       
     7 #include <cstdio>
       
     8 
       
     9 Rectangle::Rectangle (Surface * s)
       
    10 {
       
    11     P[0] = new Vector (0, 0);
       
    12     P[1] = new Vector (0, 100);
       
    13     P[2] = new Vector (100, 100);
       
    14     P[3] = new Vector (0, 100);
       
    15 
       
    16     anker = Vector (0, 0);
       
    17 
       
    18     surface = s;
       
    19 }
       
    20 
       
    21 Rectangle::Rectangle (Surface * s, Vector p1, Vector p2, Vector p3, Vector p4)
       
    22 {
       
    23     P[0] = new Vector (p1);
       
    24     P[1] = new Vector (p2);
       
    25     P[2] = new Vector (p3);
       
    26     P[3] = new Vector (p4);
       
    27 
       
    28     anker = Vector (0, 0);
       
    29 
       
    30     surface = s;
       
    31 }
       
    32 
       
    33 Rectangle::~Rectangle ()
       
    34 {
       
    35     for (int i = 0; i < 4; i++) {
       
    36 #ifdef DEBUG
       
    37         fprintf (stderr, "Removing P[%d]: ", i);
       
    38 #endif
       
    39         delete P[i];
       
    40     }
       
    41 }
       
    42 
       
    43 void Rectangle::move (Vector location)
       
    44 {
       
    45     if ((anker.X () + location.X ()) < 0)
       
    46         return;
       
    47     if ((anker.Y () + location.Y ()) < 0)
       
    48         return;
       
    49 
       
    50     if ((anker.X () + location.X ()) >= surface->getWidth ())
       
    51         return;
       
    52     if ((anker.Y () + location.Y ()) >= surface->getHeight ())
       
    53         return;
       
    54 
       
    55     for (int i = 0; i < 4; i++) {
       
    56         *P[i] = *P[i] + location;
       
    57     }
       
    58 
       
    59     anker = Vector (P[0]->X (), P[0]->Y ());
       
    60 }
       
    61 
       
    62 void Rectangle::show ()
       
    63 {
       
    64     int i;
       
    65 
       
    66     for (i = 0; i < 4; i++) {
       
    67         fprintf (stderr, "[%d] ", i);
       
    68         P[i]->vector ();
       
    69         if ((i + 1) % 4 != 0)
       
    70             fprintf (stderr, ", ");
       
    71         else
       
    72             fprintf (stderr, "\n");
       
    73     }
       
    74 
       
    75     surface->drawLine (P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y ());
       
    76     surface->drawLine (P[3]->X (), P[3]->Y (), P[2]->X (), P[2]->Y ());
       
    77 
       
    78     surface->drawLine (P[2]->X (), P[2]->Y (), P[1]->X (), P[1]->Y ());
       
    79     surface->drawLine (P[3]->X (), P[3]->Y (), P[0]->X (), P[0]->Y ());
       
    80 }