fillPoly support in DrawBuf

This commit is contained in:
Vadim Lopatin 2016-11-07 15:33:51 +03:00
parent 1b80fbb77a
commit 1814424ec5
2 changed files with 42 additions and 1 deletions

View File

@ -1104,6 +1104,8 @@ void main()
//buf.fillTriangleF(vec2(x+80, y+150), vec2(x+280, y+250), vec2(x+80, y+200), 0xC0008080);
//buf.clipRect = oldClip;
canvas.font.drawText(buf, x + 190, y + 260, "polyLineF()"d, 0x603010);
PointF[] poly2 = [vec2(x+330, y+250), vec2(x+440, y+180), vec2(x+370, y+270), vec2(x+480, y+300), vec2(x+520, y+200), vec2(x+420, y+450), vec2(x+380, y+430)];
buf.fillPolyF(poly2, 0x80203050);
};
tabs.addTab(canvas, "TAB_CANVAS"c);

View File

@ -621,7 +621,7 @@ class DrawBuf : RefCountedObject {
}
/// draw poly line of arbitrary width in float coordinates; when cycled is true, connect first and last point
void polyLineF(PointF[] points, float width, uint colour, bool cycled) {
void polyLineF(PointF[] points, float width, uint colour, bool cycled, uint innerAreaColour = COLOR_TRANSPARENT) {
if (points.length < 2)
return;
for(int i = 0; i + 1 < points.length; i++) {
@ -634,6 +634,45 @@ class DrawBuf : RefCountedObject {
}
}
/// draw filled polyline (vertexes must be in clockwise order)
void fillPolyF(PointF[] points, uint colour) {
if (points.length < 3) {
return;
}
if (points.length == 3) {
fillTriangleF(points[0], points[1], points[2], colour);
return;
}
PointF[] list = points.dup;
bool moved;
while (list.length > 3) {
moved = false;
for (int i = 0; i < list.length; i++) {
PointF p1 = list[i + 0];
PointF p2 = list[(i + 1) % list.length];
PointF p3 = list[(i + 2) % list.length];
float cross = (p2 - p1).crossProduct(p3 - p2);
if (cross >= 0) {
// draw triangle
fillTriangleF(p1, p2, p3, colour);
int indexToRemove = (i + 1) % (cast(int)list.length);
// remove triangle from poly
for (int j = indexToRemove; j + 1 < list.length; j++)
list[j] = list[j + 1];
list.length = list.length - 1;
i += 1;
moved = true;
}
}
if (list.length == 3) {
fillTriangleF(list[0], list[1], list[2], colour);
break;
}
if (!moved)
break;
}
}
/// draw poly line of width == 1px; when cycled is true, connect first and last point
void polyLine(Point[] points, uint colour, bool cycled) {
if (points.length < 2)