-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.h
More file actions
125 lines (109 loc) · 2.53 KB
/
Line.h
File metadata and controls
125 lines (109 loc) · 2.53 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
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
/*
* Line.h
* Morph
*
* Created by Christian Brunschen on 26/11/2010.
* Copyright 2010 Christian Brunschen. All rights reserved.
*
*/
#ifndef __Line_h__
#define __Line_h__
#include "Primitives.h"
#include "Circle.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
namespace Primitives {
#if 0
}
#endif
using namespace std;
template <typename F> void line(int x0, int y0, int x1, int y1, F &f) {
IMatrix matrix = IMatrix::translate(x0, y0);
int dx = x1 - x0;
int dy = y1 - y0;
if (dx < 0) {
matrix = matrix.concat(IMatrix::flipX());
dx = -dx;
}
if (dy < 0) {
matrix = matrix.concat(IMatrix::flipY());
dy = -dy;
}
if (dy > dx) {
matrix = matrix.concat(IMatrix::flipXY());
swap(dy, dx);
}
int error = dx / 2;
int y = 0;
for (int x = 0; x <= dx; x++) {
int xx, yy;
matrix.transform(xx, yy, x, y);
f(xx, yy);
error = error - dy;
if (error < 0) {
y += 1;
error += dx;
}
}
}
template <typename F, typename D>
void drawAll(F &f, const IPoint &p, const D &points, const IMatrix &matrix) {
for (typename D::const_iterator i = points.begin(); i != points.end(); ++i) {
f(((*i) + p).transform(matrix));
}
}
template <typename I, typename DX, typename DXY, typename F>
void line(int x0, int y0, int x1, int y1,
F &f,
const I &initial,
const DX &deltaX,
const DXY &deltaXY) {
IMatrix matrix = IMatrix::translate(x0, y0);
int dx = x1 - x0;
int dy = y1 - y0;
if (dx < 0) {
matrix = matrix.concat(IMatrix::flipX());
dx = -dx;
}
if (dy < 0) {
matrix = matrix.concat(IMatrix::flipY());
dy = -dy;
}
if (dy > dx) {
matrix = matrix.concat(IMatrix::flipXY());
swap(dy, dx);
}
drawAll(f, IPoint(0, 0), initial, matrix);
int error = dx / 2;
IPoint p(0, 0);
for (p.x() = 1; p.x() <= dx; p.x()++) {
error = error - dy;
if (error < 0) {
p.y()++;
error += dx;
drawAll(f, p, deltaXY, matrix);
} else {
drawAll(f, p, deltaX, matrix);
}
}
}
template <typename F>
void line(int x0, int y0, int x1, int y1,
F &f,
const Circle &circle) {
line(x0, y0, x1, y1, f,
circle.points(), circle.getHorizontalDelta(), circle.getDiagonalDelta());
}
inline void rotate(double &nx, double &ny, const double &x, const double &y, const double &theta) {
double cosTheta = cos(theta);
double sinTheta = sin(theta);
nx = x * cosTheta - y * sinTheta;
ny = x * sinTheta + y * cosTheta;
}
#if 0
{
#endif
}
#endif // __Line_h__