Dashed line "ant trail"


I’m trying to animate a dashed line kind of like an ant trail. So I’ve got a line with two endpoints and the dashes should move in one direction. I got this working but I hit an assertion in Graphics::drawDashedLine() that reminds me not to have zero length dashes. But I think I need zero length dashes for this. Here’s what I do to calculate the dash lengths:

float progress = getProgress() * 2; // getProgress return a value from 0 to 1
float dashLength = 5;
float dl1, dl2, dl3, dl4;

if(progress < 1.f) {
    dl1 = 0;
    dl2 = progress * dashLength;
    dl3 = dashLength;
    dl4 = (1.f - progress) * dashLength;
} else {
    progress = progress - 1.f;
    dl1 = progress * dashLength;
    dl2 = dashLength;
    dl3 = (1.f - progress) * dashLength;
    dl4 = 0;

std::array<float, 4> dashLengths { dl1, dl2, dl3, dl4 };

Has anybody got an idea what’s the best way of doing this? Or is it maybe possible to change the assertion to check if all dashes have zero length?
Easiest way would be to have an offset for the dashes, but I guess that would mess up the api.


Unfortunately I don’t have time to work through your code, but I tried to construct how this would work in my own head…

You do need zero length dashes for the case where the trail is at the zero phase. For that, simply just feed less dashes to the draw function via the numDashLengths parameter.


I don’t think thats gonna work. The function doesn’t accept an odd number of dashlengths.


I just found out that it works when using a path instead of drawing the line directly.
Although I’m wondering how efficient that is. Guess I’ll find out, when I start optimizing.