-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstraightlinebrush.cpp
More file actions
95 lines (74 loc) · 1.88 KB
/
straightlinebrush.cpp
File metadata and controls
95 lines (74 loc) · 1.88 KB
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
#include "straightlinebrush.h"
#include <iostream>
namespace my{
StraightLineBrush::StraightLineBrush(Image *i, int size):Brush(i,size)
{
}
void StraightLineBrush::OnMouseMove(int xpos, int ypos, int color)
{
}
void StraightLineBrush::OnMouseDown(int xpos, int ypos, int color)
{
lastX = xpos;
lastY = ypos;
}
void StraightLineBrush::OnMouseUp(int x, int y, int color){
DrawLine(x,y,color);
}
int StraightLineBrush::getDirection(int pos, int target){
if(pos > target)
return -1;
else
return 1;
}
int StraightLineBrush::GetDelta(int x1, int x2){
int delta (x2 - x1);
delta = std::abs(delta) << 1;
return delta;
}
void StraightLineBrush::DrawLine(int xpos, int ypos, int color){
int ix = getDirection (xpos, lastX);
int iy = getDirection (ypos, lastY);
//std::cout <<" Last X " << lastX <<" ix" << xIterator;
int delta_x = GetDelta(xpos , lastX);
int delta_y = GetDelta(ypos , lastY);
int saveXpos = xpos;
int saveYpos = ypos;
if (delta_x >= delta_y)
{
// error may go below zero
int error(delta_y - (delta_x >> 1));
while (xpos != lastX)
{
if ((error >= 0) && (error || (ix > 0)))
{
error -= delta_x;
ypos += iy;
}
// else do nothing
error += delta_y;
xpos += ix;
Emit(xpos,ypos, color);
}
}
else
{
// error may go below zero
int error(delta_x - (delta_y >> 1));
while (ypos != lastY)
{
if ((error >= 0) && (error || (iy < 0)))
{
error -= delta_y;
xpos += ix;
}
// else do nothing
error += delta_x;
ypos += iy;
Emit(xpos,ypos, color);
}
}
lastX = saveXpos;
lastY = saveYpos;
}
}