Q: Update a variable when a different one changes?

Dear Desmos CL folks,

I would like to update the value of a variable whenever a different variable changes.

In this particular case, that change comes from a point getting dragged. I can execute an action when a point gets clicked, but I don’t seem to be able to do that when it gets dragged.

Another possibility might be to record the old value of the variable, and to execute an action whenever that var’s new value differs from its old value. However, I’m finding that tricky to get working too.

Any help or advice greatly appreciated!
Many thanks,

Raj

Our good friend the Ticker is the solution to this. Please see below for a rudimentary example of how to (a) update an variable to exactly match another, and (b) update a variable whenever another changes.

Thanks! That’s just what I needed. Much appreciated!

I find it interesting that you tested for a mismatch between x1 and x2 using two different inequalities, namely x1 > x2 and x1 < x2. I had been trying to detect a mismatch using a single abs-value inequality of the form | x1 - x2 | > 0.

That wasn’t working for me, but I couldn’t figure out why not. It looks like you might have been avoiding that sort of abs-value inequality by using the the two separate conditions x1 > x2 and x1 < x2. Is there a reason why the abs-value version doesn’t work? It should be equivalent, as far as I can see, but it doesn’t appear to act equivalently.

Thanks again,

Raj

Your method will work and is a little more terse. Slightly better imo.

The reason I went with two inequalities is simply because I didn’t think of your method :laughing: - that’s far more efficient!

However, I’ve tried using your absolute value method and it still works for me - I’ve updated the original link to reflect this.

Not sure why you didn’t have any success yourself. Sometimes Desmos can be a bit particular about the two different forms:

{ condition : action }
vs.
action{ condition }

But beyond that I can’t see a reason why your initial attempt shouldn’t have worked…

Thanks!

You are right: the |x1-x2|>0 version does turn out to work ok.

I had mistakenly thought that it wasn’t working, because the action I was trying to trigger was setting a list to be empty, e.g. L_ist → [ ] {|x1-x2|>0}

Something about that syntax doesn’t work, but the problem wasn’t the absolute value part, despite what I initially thought. I think the problem might be Desmos having difficulty parsing the square bracket next to curly bracket ] { sequence?

Go with { |x1-x2|>0 : L_ist → [ ] } instead and it should work fine…

More for curiosity… Why can’t you just use the same variable? (i.e. x1=x2)

That’s a good question! In this case, the variable that changes is the size of a billiard table (changed by the user dragging on a corner), and the variable that needs to get changed as a result of that is a list of squares that show the track of a ball bouncing around the table.

Here’s the Desmos activity:

The way I get the ball to leave a trail behind it as it bounces around the table is really an ugly hack. I pre-draw a bunch of diagonal lines as little images, set them all to be transparent, and then set them to opaque when the ball passes over them! I couldn’t think of a less ugly way of doing it, as Desmos doesn’t really lend itself too easily to that.

The same set up is much easier and cleaner to do in Python Turtle:

However, that doesn’t lend itself so easily to drag-to-resize for the table, and you can’t wrap it inside a nice set of Desmos activity screens and explanations.

Raj

One possibility I can think of is to keep lists of x and y coordinates, and have a ticker watching and an action that adds current location of the ball to the lists. Really you would only need to add a value when the direction changes. And then you can have it draw lines between the points in the list.

I was thinking something similar to @Daniel_Wekselgreene. If you have a list of points of the path of the ball, you can restrict the list based on a ticker or time variable:

#list of points
P=(X,Y)
P[1...n] #where n is increased by a ticker or button time

That’s a good idea, thanks. I’m pretty sure that my hacky approach could have been more elegant, and your approach definitely does sound better.

That said, my hacky approach eventually worked, so I’m going to leave it as is for now! :slight_smile:

I like to use a parametric function based on zigzags to implement bouncing objects like this, like in this example graph: Bouncing Billiards Ball | Desmos

1 Like