File: ClodPolyline.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 90,112 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 39
file content (126 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)

#include "ClodPolyline.h"

WM5_WINDOW_APPLICATION(ClodPolyline);

//----------------------------------------------------------------------------
ClodPolyline::ClodPolyline ()
    :
    WindowApplication2("SampleMathematics/ClodPolyline", 0, 0, 256, 256,
        Float4(1.0f, 1.0f, 1.0f, 1.0f))
{
    mPolyline = 0;
    mSize = GetWidth();
}
//----------------------------------------------------------------------------
bool ClodPolyline::OnInitialize ()
{
    if (!WindowApplication2::OnInitialize())
    {
        return false;
    }

    // Generate points on unit circle, then adjust the distances to center.
    int numVertices = 16;
    Vector3f* vertices = new1<Vector3f>(numVertices);
    int i;
    for (i = 0; i < numVertices; ++i)
    {
        float angle = Mathf::TWO_PI*i/numVertices;
        vertices[i].X() = Mathf::Cos(angle);
        vertices[i].Y() = Mathf::Sin(angle);
        vertices[i].Z() = 0.0f;

        float adjust = 1.0f + 0.25f*Mathf::SymmetricRandom();
        vertices[i] *= adjust;
    }

    mPolyline = new0 Polyline3(numVertices, vertices, true);

    OnDisplay();
    return true;
}
//----------------------------------------------------------------------------
void ClodPolyline::OnTerminate ()
{
    delete0(mPolyline);

    WindowApplication2::OnTerminate();
}
//----------------------------------------------------------------------------
void ClodPolyline::OnDisplay ()
{
    ClearScreen();

    ColorRGB black(0, 0, 0);
    const int numVertices = mPolyline->GetNumVertices();
    const Vector3f* vertices = mPolyline->GetVertices();
    const int numEdges = mPolyline->GetNumEdges();
    const int* edges = mPolyline->GetEdges();

    Vector3f vertex;
    int i;
    for (i = 0; i < numVertices; ++i)
    {
        vertex = vertices[i];
        int x = (int)(0.25f*mSize*(vertex.X() + 2.0f));
        int y = mSize - 1 - (int)(0.25f*mSize*(vertex.Y() + 2.0f));
        SetThickPixel(x, y, 1, black);
    }

    for (i = 0; i < numEdges; ++i)
    {
        vertex = vertices[edges[2*i]];
        int x0 = (int)(0.25f*mSize*(vertex.X() + 2.0f));
        int y0 = mSize - 1 - (int)(0.25f*mSize*(vertex.Y() + 2.0f));

        vertex = vertices[edges[2*i+1]];
        int x1 = (int)(0.25*mSize*(vertex.X() + 2.0f));
        int y1 = mSize - 1 - (int)(0.25f*mSize*(vertex.Y() + 2.0f));
        DrawLine(x0, y0, x1, y1, black);
    }

    WindowApplication2::OnDisplay();
}
//----------------------------------------------------------------------------
bool ClodPolyline::OnKeyDown (unsigned char key, int x, int y)
{
    if (WindowApplication2::OnKeyDown(key, x, y))
    {
        return true;
    }

    int levelOfDetail;

    switch (key)
    {
    case '+':  // increase level of detail
    case '=':
        levelOfDetail = mPolyline->GetLevelOfDetail();
        if (levelOfDetail < mPolyline->GetMaxLevelOfDetail())
        {
            mPolyline->SetLevelOfDetail(levelOfDetail + 1);
            OnDisplay();
        }
        return true;
    case '-':  // decrease level of detail
    case '_':
        levelOfDetail = mPolyline->GetLevelOfDetail();
        if (levelOfDetail > mPolyline->GetMinLevelOfDetail())
        {
            mPolyline->SetLevelOfDetail(levelOfDetail - 1);
            OnDisplay();
        }
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------