File: spinnaker.cpp

package info (click to toggle)
boats 201307-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,424 kB
  • ctags: 1,486
  • sloc: cpp: 10,139; makefile: 13; xml: 10
file content (140 lines) | stat: -rw-r--r-- 5,776 bytes parent folder | download | duplicates (2)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// C++ Implementation: SpinnakerGraphicsItem
//
// Description:
//
//
// Author: Thibaut GRIDEL <tgridel@free.fr>
//
// Copyright (c) 2010 Thibaut GRIDEL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//
#include <iostream>

#include <QPainter>
#include <QGraphicsScene>

#include "spinnaker.h"

#include "commontypes.h"
#include "situationmodel.h"
#include "trackmodel.h"
#include "boatmodel.h"

extern int debugLevel;

SpinnakerGraphicsItem::SpinnakerGraphicsItem(BoatModel *boat, QGraphicsItem *parent)
        : SailGraphicsItem(boat, parent),
        m_pole(new QGraphicsLineItem(this)) {
    setHeading(boat->heading());
}


SpinnakerGraphicsItem::~SpinnakerGraphicsItem() {}

void SpinnakerGraphicsItem::setSailSize(qreal sailSize) {
    m_sailSize = sailSize;

        QPainterPath sailPathStarboard;
        sailPathStarboard.cubicTo(.4 * sailSize, .2 * sailSize, .6 * sailSize, .2 * sailSize, sailSize, 0);
        sailPathStarboard.arcTo(-sailSize, -sailSize, 2 * sailSize, 2 * sailSize, 0, -120);
        sailPathStarboard.cubicTo(-.12 * sailSize, .62 * sailSize, -.03 * sailSize, .44 * sailSize, 0, 0);
        m_sailPathStarboard = sailPathStarboard;

        QPainterPath sailPathPort;
        sailPathPort.cubicTo(-.4 * sailSize, .2 * sailSize, -.6 * sailSize, .2 * sailSize, -sailSize, 0);
        sailPathPort.arcTo(- sailSize, -sailSize, 2 * sailSize, 2 * sailSize, 180, 120);
        sailPathPort.cubicTo(.12 * sailSize, .62 * sailSize, .03 * sailSize, .44 * sailSize, 0, 0);
        m_sailPathPort = sailPathPort;

        QLineF line;
        line.setP2(QPointF(0, -sailSize));
        m_pole->setLine(line);

        setSailAngle(m_boat->spinAngle() + m_boat->spinTrim());
}

/// calculate a sail incidence angle, corrected with user trimming
void SpinnakerGraphicsItem::setSailAngle(qreal value) {
    m_sailAngle = value;
    qreal angle = fmod(m_boat->heading() - m_boat->wind() - m_sailAngle, 360);
    if(angle < 0) angle += 360;

    if ((angle < 10 || angle > 350 || (angle > 170 && angle < 190)) && path() != m_sailPathStalled) {
        setPath(m_sailPathStalled);
    } else if (angle >= 10 && angle <= 170 && path() != m_sailPathStarboard) {
        setPath(m_sailPathStarboard);
    } else if (angle >= 190 && angle <= 350 && path() != m_sailPathPort) {
        setPath(m_sailPathPort);
    }
}

void SpinnakerGraphicsItem::setHeading(qreal value) {
    qreal angle = fmod(value - m_boat->wind(), 360);
    if (angle < 0) angle += 360;

    QTransform rotation;
    rotation.rotate(-angle);
    setTransform(rotation, false);

    QTransform poleRotation;
    if (angle > 90 && angle < 180) {
        poleRotation.rotate(90);
    } else if (angle >= 180 && angle < 270) {
        poleRotation.rotate(-90);
    } else {
        poleRotation.rotate(angle);
    }
    m_pole->setTransform(poleRotation, false);

    QPainterPath sailPathStalled;
    QPointF pole = m_pole->mapToParent(m_pole->line().p2());
    double sinx = sin(angle* M_PI/180.0);
    double cosx = cos(angle* M_PI/180.0);
    double sign = .1;
    if (angle>180) {
        sign = -.1;
    }
    sailPathStalled.cubicTo(sign * m_sailSize * cosx + .4 * m_sailSize * sinx, sign * m_sailSize * sinx - .4 * m_sailSize * cosx,
                            sign * m_sailSize * cosx + .6 * m_sailSize * sinx, sign * m_sailSize * sinx - .6 * m_sailSize * cosx,
                            pole.x(), pole.y());
    sailPathStalled.cubicTo(pole.x() + sign * m_sailSize, pole.y() + .2 * m_sailSize,
                            pole.x() + sign * m_sailSize, pole.y() + .2 * m_sailSize,
                            pole.x(), pole.y() + .3 * m_sailSize);
    sailPathStalled.cubicTo(pole.x() - sign * m_sailSize, pole.y() + .4 * m_sailSize,
                            pole.x() - sign * m_sailSize, pole.y() + .4 * m_sailSize,
                            pole.x(), pole.y() + .5 * m_sailSize);
    sailPathStalled.cubicTo(pole.x() + sign * m_sailSize, pole.y() + .6 * m_sailSize,
                            pole.x() + sign * m_sailSize, pole.y() + .6 * m_sailSize,
                            pole.x(), pole.y() + .7 * m_sailSize);
    sailPathStalled.cubicTo(pole.x() - sign * m_sailSize, pole.y() + .8 * m_sailSize,
                            pole.x() - sign * m_sailSize, pole.y() + .8 * m_sailSize,
                            pole.x(), pole.y() + m_sailSize);
    sailPathStalled.cubicTo(.8* pole.x(), pole.y() + m_sailSize,
                            .8 * pole.x(), pole.y() + m_sailSize,
                            .7 * pole.x(), .7 * (pole.y() + m_sailSize));
    sailPathStalled.cubicTo(.6 * pole.x(), .4 * (pole.y() + m_sailSize),
                            .6 * pole.x(), .4 * (pole.y() + m_sailSize),
                            .5 * pole.x(), .5 * (pole.y() + m_sailSize));
    sailPathStalled.cubicTo(.4 * pole.x(), .6 * (pole.y() + m_sailSize),
                            .4 * pole.x(), .6 * (pole.y() + m_sailSize),
                            .3 * pole.x(), .3 * (pole.y() + m_sailSize));
    sailPathStalled.cubicTo(.2 * pole.x(), 0,
                            .2 * pole.x(), 0,
                            0, 0);
    m_sailPathStalled = sailPathStalled;

}