~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libannotate/LineSegment.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cmath>
 
2
using namespace std;
 
3
 
 
4
#include "LineSegment.h"
 
5
 
 
6
#include "libdisplay/libdisplay.h"
 
7
 
 
8
LineSegment::LineSegment(const unsigned char color[3], 
 
9
                         const double X0, const double Y0,
 
10
                         const double X1, const double Y1)
 
11
    : Annotation(color), X0_(X0), Y0_(Y0), X1_(X1), Y1_(Y1)
 
12
{
 
13
}
 
14
 
 
15
LineSegment::~LineSegment()
 
16
{
 
17
}
 
18
 
 
19
void
 
20
LineSegment::Draw(DisplayBase *display)
 
21
{
 
22
    const double width = display->Width();
 
23
    const double height = display->Height();
 
24
 
 
25
    if ((X0_ < 0 && X1_ < 0)
 
26
        || (X0_ >= width && X1_ >= width)
 
27
        || (Y0_ < 0 && Y1_ < 0)
 
28
        || (Y0_ >= height && Y1_ >= height)) return;
 
29
 
 
30
    if (X0_ == X1_)
 
31
    {
 
32
        for (double y = Y0_; y < Y1_; y++)
 
33
            display->setPixel(X0_, y, color_);
 
34
        return;
 
35
    }
 
36
 
 
37
    const double slope = (Y1_ - Y0_) / (X1_ - X0_);
 
38
 
 
39
    if (fabs(slope) < 1)
 
40
    {
 
41
        double y = Y0_;
 
42
        double x0 = X0_;
 
43
        double x1 = X1_;
 
44
        if (X0_ > X1_)
 
45
        {
 
46
            y = Y1_;
 
47
            x0 = X1_;
 
48
            x1 = X0_;
 
49
        }
 
50
        if (x0 < 0) 
 
51
        {
 
52
            y -= slope * x0;
 
53
            x0 = 0;
 
54
        }
 
55
        if (x1 >= width) x1 = width - 1;
 
56
        for (double x = x0; x < x1; x++)
 
57
        {
 
58
            display->setPixel(x, y, color_);
 
59
            y += slope;
 
60
        }
 
61
    }
 
62
    else
 
63
    {
 
64
        double x = X0_;
 
65
        double y0 = Y0_;
 
66
        double y1 = Y1_;
 
67
        if (Y0_ > Y1_)
 
68
        {
 
69
            x = X1_;
 
70
            y0 = Y1_;
 
71
            y1 = Y0_;
 
72
        }
 
73
        if (y0 < 0) 
 
74
        {
 
75
            x -= y0/slope;
 
76
            y0 = 0;
 
77
        }
 
78
        if (y1 >= height) y1 = height - 1;
 
79
        for (double y = y0; y < y1; y++)
 
80
        {
 
81
            display->setPixel(x, y, color_);
 
82
            x += (1/slope);
 
83
        }
 
84
    }
 
85
}