-
Notifications
You must be signed in to change notification settings - Fork 0
/
PressureBrush.cpp
81 lines (65 loc) · 2.01 KB
/
PressureBrush.cpp
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
//
// PressureBrush.cpp
//
// The implementation of Pressure Brush. It is a kind of ImpBrush. All your brush implementations
// will look like the file with the different GL primitive calls.
//
// Pressure brush will simulate a brush's pressure by the moving speed of the brush
//
#include "ImpressionistDoc.h"
#include "ImpressionistUI.h"
#include "PressureBrush.h"
#include <numeric>
#include <iostream>
extern float frand();
PressureBrush::PressureBrush( ImpressionistDoc* pDoc, char* name ) :
ImpBrush(pDoc,name)
{
}
void PressureBrush::BrushBegin( const Point source, const Point target )
{
BrushMove( source, target );
}
void PressureBrush::BrushMove( const Point source, const Point target )
{
ImpressionistDoc* pDoc = GetDocument();
ImpressionistUI* dlg=pDoc->m_pUI;
if ( pDoc == NULL ) {
printf( "PressureBrush::BrushMove, document is NULL\n" );
return;
}
// Push the current target into the back stack
this->pointHistory.push_back(target);
// If history size exceed our desired count, pop the first one (most outdated one)
if(this->pointHistory.size() > pointHistoryCount)
{
this->pointHistory.pop_front();
}
// Compute the mean of distance of the point history
float mean = 0;
std::list<Point>::const_iterator it;
for (it = this->pointHistory.begin(); it != this->pointHistory.end(); ++it) {
auto nextIt = std::next(it, 1);
if (nextIt != this->pointHistory.end())
{
mean += sqrt(pow((it->x - nextIt->x), 2) + pow((it->y - nextIt->y), 2));
}
}
mean /= this->pointHistory.size();
// Set the mean as the radius size, factored by
float radius = mean * this->radiusFactor;
dlg->setSize(int(radius));
glBegin( GL_TRIANGLE_FAN );
SetColor( source );
glVertex2d(target.x, target.y);
for (int i = 0; i < 360; i++) {
float angle = i * M_PI / 180;
glVertex2f(target.x + radius * cos(angle), target.y + radius * sin(angle));
}
glEnd();
}
void PressureBrush::BrushEnd( const Point source, const Point target )
{
// Clear the point history when brush finishes
this->pointHistory.clear();
}