Hookkid
Hookkid

Reputation: 178

Kivy Canvas Line Maximum length

I am drawing a line on my Canvas that is basically the trace of where a particular object has been. The Line is drawn with the following code:

with self.canvas:

            Color(0,0,1)

            if self.first:
                self.line = Line(points=[self.car.x,self.car.y],width=2)
                self.first = False

            self.line.points += [self.car.x, self.car.y]

On the app, the line ends up looking like this:

enter image description here

The number you see in white are the number of points in the line. I'd like the line to start 'decaying' when it reaches 1500. So the first point of the line would get deleted but the object would still be appending to the top of the Points list.

To achieve that I used the following code:

            if len(self.line.points)>1500:
               del self.line.points[0]

That works to some extent - the original line starts beeing deleted from its beggining. The problem is that a second line appears and self.line.points continues to append. So, in the end I have something looking like this:

enter image description here

I'm imagining the issue lies in the fact that I HAVE to instantiate the Line with its ORIGINAL position and when I remove that first point, the widget loses track of where it was and starts acting erradically.

Whether that is the case or not, I'd like to know if anyone has had this issue and/or knows how to get around it.

Upvotes: 2

Views: 294

Answers (1)

Tshirtman
Tshirtman

Reputation: 5947

You only remove one coordinate at a time, when each point is constituted from two coordinates, it's not obvious from your code but you may think that your list of points look like this:

[[x1, y1], [x2, y2], [...], [xn, yn]]

while, in fact it looks like this:

[x1, y1, x2, y2, [...], xn, yn]

so when you remove coordinates one by one, the line instruction is a bit confused.

[x1, y1, x2, y2, [...], xn]  # uh what?

the solution would be to delete two items instead of one :)

        if len(self.line.points)>1500:
           del self.line.points[0]
           del self.line.points[0]

I put a working version of the code here (did it to experiment, so might as well post it :)).

https://gist.github.com/tshirtman/603cbda8202103cb7845adb54bb90ee2

Upvotes: 1

Related Questions