summaryrefslogtreecommitdiffstats
path: root/polygon.cpp
blob: d8b71b746fc6a5587e4ee0d0719a487de9c86db7 (plain)
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
#include "polygon.h"

Polygon::Polygon()
    : Line()
{
    m_polygon.append(QPoint(10, 50));
    m_polygon.append(QPoint(30, 70));
    m_polygon.append(QPoint(50, 50));
}

bool Polygon::isHit(const QPoint &clickPoint)
{
    return m_polygon.containsPoint(clickPoint, Qt::OddEvenFill);
}

void Polygon::draw(QPainter *painter)
{
    if (m_selected)
    {
        QPen penTemp(Qt::DotLine);
        penTemp.setColor(Qt::red);
        painter->setPen(penTemp);

        painter->drawEllipse(m_polygon.point(0), 5, 5);
        painter->drawEllipse(m_polygon.point(1), 5, 5);
        painter->drawEllipse(m_polygon.point(2), 5, 5);
    }
    else
    {
        QPen penNormal(Qt::SolidLine);
        penNormal.setColor(Qt::black);
        painter->setPen(penNormal);
    }

    painter->drawPolygon(m_polygon);
}


void Polygon::move(const QPoint &oldPoint, const QPoint &newPoint)
{
    if(m_selected)
    {
        QPoint offset = newPoint - oldPoint;
        QVector3D vecOld(oldPoint);
        if(vecOld.distanceToPoint(QVector3D(m_polygon.point(0))) < 5)
        {
            m_polygon.replace(0, m_polygon.point(0) + offset);
        }
        else if(vecOld.distanceToPoint(QVector3D(m_polygon.point(1))) < 5)
        {
            m_polygon.replace(1, m_polygon.point(1) + offset);
        }
        else if(vecOld.distanceToPoint(QVector3D(m_polygon.point(2))) < 5)
        {
            m_polygon.replace(2, m_polygon.point(2) + offset);
        }
        else
        {
            m_polygon.replace(0, m_polygon.point(0) + offset);
            m_polygon.replace(1, m_polygon.point(1) + offset);
            m_polygon.replace(2, m_polygon.point(2) + offset);
        }
    }
}