You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to get the derivative of any arbitrary path at a certain sample point (needed for my use case). My approach is to draw a small circle at the sample point, get the intersection with the path and then calculate the difference of the intersection and the sample point.
The problem I have is that I cannot write the derivative value to a variable out of scope. Trying to return a value from inside the canvas fails.
Here is my code:
#import "@preview/cetz:0.4.0"
#set page(width: auto, height: auto, margin: .5cm)
// I want the value to be here
#let derivative = (0, 0)
#cetz.canvas({
import cetz.draw: *
import cetz.vector: *
// sample point from where to get the derivative
// important for my use case
let sample = (2, 2)
// get intersection close to the sample
intersections("i", {
hide(circle(sample, radius: 10e-5))
catmull((2, 2), (2.2, 2.8), (4.4, 4), (4, 4.8), tension: 0.4)
})
// loop over the one intersection
for-each-anchor("i", (name) => {
group(ctx => {
let (ctx, origin) = cetz.coordinate.resolve(ctx, ("i." + name))
let d = sub(origin, sample)
let n = norm(d)
// does not work because of the scope
// derivative = n
line(sample, add(sample, scale(n, 0.3)), stroke: red)
})
})
})
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I want to get the derivative of any arbitrary path at a certain sample point (needed for my use case). My approach is to draw a small circle at the sample point, get the intersection with the path and then calculate the difference of the intersection and the sample point.
The problem I have is that I cannot write the derivative value to a variable out of scope. Trying to return a value from inside the canvas fails.
Here is my code:
Beta Was this translation helpful? Give feedback.
All reactions