Skip to content

Commit

Permalink
Wang Brush: Added line drawing with Shift modifier
Browse files Browse the repository at this point in the history
As previously supported by the Terrain Brush, but now also supporting
the drawing of thinner lines in "Corner" and "Edge" modes.

Also make lines when moving fast with the mouse, to avoid gaps. This is
unfortunately not supported yet in "PaintEdgeAndCorner" mode.
  • Loading branch information
bjorn committed Oct 8, 2020
1 parent 2979823 commit 5dc79c7
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 152 deletions.
17 changes: 11 additions & 6 deletions src/tiled/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ QRegion ellipseRegion(int x0, int y0, int x1, int y1)
* This is an implementation of Bresenham's line algorithm, initially copied
* from http://en.wikipedia.org/wiki/Bresenham's_line_algorithm#Optimization
* changed to C++ syntax.
*
* When the \a manhattan option (named after "Manhattan distance") is set to
* true, the points on the line can't take diagonal steps.
*/
QVector<QPoint> pointsOnLine(int x0, int y0, int x1, int y1)
QVector<QPoint> pointsOnLine(int x0, int y0, int x1, int y1, bool manhattan)
{
QVector<QPoint> ret;

Expand All @@ -197,15 +200,17 @@ QVector<QPoint> pointsOnLine(int x0, int y0, int x1, int y1)
else
ystep = -1;

for (int x = x0; x < x1 + 1 ; x++) {
if (steep)
ret += QPoint(y, x);
else
ret += QPoint(x, y);
ret.reserve(deltax + 1 + (manhattan ? deltay : 0));

for (int x = x0; x <= x1; x++) {
ret += steep ? QPoint(y, x) : QPoint(x, y);
error = error - deltay;
if (error < 0) {
y = y + ystep;
error = error + deltax;

if (manhattan && x < x1)
ret += steep ? QPoint(y, x) : QPoint(x, y);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/tiled/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ namespace Tiled {

QVector<QPoint> pointsOnEllipse(int x0, int y0, int x1, int y1);
QRegion ellipseRegion(int x0, int y0, int x1, int y1);
QVector<QPoint> pointsOnLine(int x0, int y0, int x1, int y1);
QVector<QPoint> pointsOnLine(int x0, int y0, int x1, int y1, bool manhattan = false);

inline QVector<QPoint> pointsOnEllipse(QPoint a, QPoint b)
{ return pointsOnEllipse(a.x(), a.y(), b.x(), b.y()); }

inline QVector<QPoint> pointsOnLine(QPoint a, QPoint b)
{ return pointsOnLine(a.x(), a.y(), b.x(), b.y()); }
inline QVector<QPoint> pointsOnLine(QPoint a, QPoint b, bool manhattan = false)
{ return pointsOnLine(a.x(), a.y(), b.x(), b.y(), manhattan); }

QVector<QRegion> coherentRegions(const QRegion &region);

Expand Down
Loading

0 comments on commit 5dc79c7

Please sign in to comment.