stackoverflow.com/questions/52931928 Is there a canonical for this? it's similar to the problem of binding a Button to its own command callback (stackoverflow.com/questions/10865116), but when you actually get an event from Tkinter it contains the widget information so you don't need to do your own binding.
Hi, would somebody like to have a look at my MWE for an animation with matplotib in a tkinter GUI by any chance? It is in post 3 and my last post contains some other explanations. discourse.matplotlib.org/t/…. Many thanks
Hi. Is there a way to replace: x in ['str1', 'str2'] with something like x in AllowedStringsEnum? My goal is to ensure that str1 and str2 are only typed once (when defined) and all checks of them are done in a safe way. (i know i can define a list, but i d like to know if it can be done with an Enum)
@Skyler There are a few posts about drawing circular arcs using Bézier curves on Math.SE. Here's one for cubic Béziers: math.stackexchange.com/q/873224/207316
But what even is the point of a string enum where the name of each member is the same as the value? Isn't this just an over-engineered set? Why not just ALLOWED_STRINGS = {'str1', 'str2'}?
Much of that Math.SE post is not comprehensible to me, but I'm guessing it implies that it's impossible to draw a perfect circular arc using bezier curves. I wondered whether that might be the case last night. I tried making a couple half-circles using MS Paint's curve tool, but they were all a bit off.
It's easy enough to draw a C shaped curve whose endpoints are tangent to a horizontal line, but I wasn't sure how to get the width of the curve to be exactly half of the height of the curve
Maybe I can find it algebraically... Let me get my cocktail napkins
@Aran-Fey Good point. But I need to use them in other parts of my code like so: if y is AllowedStringsEnum.str1: ..do something. When it's an Enum (instead of a str) refactoring becomes easier and less error prone.
The math cancels out nicely for bezier curves whose control points form a rectangle. the width of the final curve is 3/4ths of the width of the control point box. Here, the box is 4 arbitrary units wide; so the curve peaks at a width of 3 arbitrary units.
@Hakaishin Yes, but only to the human eye. if you overlay them and zoom in, the paths are noticeably different.
There's a little bit of red visible under the green. I was careful to place the circle and control points with pixel perfect precision, so any divergence is the fault of the algorithm
@Hakaishin Did you look at my demo? At the bottom of the page there's a link to a SVG version, which makes it easier to zoom in & see the detail. I draw a thick blue circle, then draw the Bézier arcs over the top of that. If you select 2 sectors, the error's obvious. With 8 sectors, it's virtually invisible.
The Bézier curves are using cubic polynomials for x & y to approximate y = sqrt(r**2 - x**2). It can get very close, but it can't get an exact match. You'd need an infinite number of polynomial terms for an exact match.
OTOH, Bézier arcs are quite good when the sector angle is small. And they're fast to calculate because they avoid square roots. PostScript & PDF always use Bézier curves to draw circular arcs, and I assume SVG does too. You just need to make the sectors small enough so that the error is less than a pixel.
I mostly draw Bézier curves by calling library functions that do the low-level arithmetic. But sometimes I do the polynomial calculations myself. However, there's a neat algorithm that draws Béziers by reducing their degree until you get to the 1st degree, which is just a line. De Casteljau's Algorithm
When I approximate a half-circle of radius 1 using a bezier curve, it has a radius of 1 at t=0 and t=1/2 and t=1. But at t=1/4, it has a radius of exactly sqrt(265/256).
(insofar as you can say that a bezier curve has a "radius". More accurately, I mean the distance between the origin and a point on the curve, uniquely identified by t)
The table of contents for w3.org/TR/SVG/paths.html#PathData mentions "cubic bezier curve", "quadratic bezier curve", and "elliptical arc curve". I might have spotted others during my expedition, but no guarantees.
Well, elliptical arcs are just scaled circular arcs. And as I said above, under the hood, they probably do circular (and elliptical) arcs using Bézier.
Sounds reasonable to me :-) I think the spec leaves such decisions up to the implementation.
There are other parts of the spec that suggest providing the mutually exclusive flags "fast" and "accurate" for various primitives. Perhaps an implementation might use a bezier curve for a fast <circle>, and laboriously calculate the exact circular path for an accurate <circle>.
I appreciate that the document has anticipated that many implementations will do a less than perfect job. "You're going to screw this up, no doubt. But at least let the user customize the type of screw-up they prefer"
There's actually a way of drawing exact circles without square roots, but it uses rational functions, not polynomials. I posted a demo the other day in Math chat.
(Here I categorize "render perfectly, but take a long time" as a screw-up. Mathematics says it's fine, but business says it's bad for customer retention)
It's pretty handy when you're doing mathematical stuff, especially for mathematicians familiar with Latex. And of course Sage can automatically format expressions as Latex, although they sometimes need tweaking if the expression is complicated.
Sage can do amazing things, but because it's so huge, and built on top of multiple systems, it can be difficult to discover stuff. Fortunately, a lot of it is intuitive, once you've had a bit of experience with it. But it's not unusual to stumble across really handy stuff in obscure corners of the docs.
It'd be pretty hard to replace LaTeX, it's too entrenched. And it's not easy to get mathematicians to learn any form of coding language. Sure, there are some mathematicians who are also excellent coders in mainstream languages, but they are the exception, not the rule. A fair few mathematicians do use things like Mathematica, though.
Also, it's better to just deal with the annoying parts of LaTeX than to introduce a new system & have competing standards. xkcd.com/927
Some people attempt to typeset mathematics with Word. But that's generally regarded as an abomination. ;)
Vague geometry problem: I have a closed curve, and a point P. Call a point Q on the curve "lucky" if line PQ is exactly perpendicular to the curve. How do I find all lucky points on the curve?
Here's an example. The figure eight is the curve, the dot in the upper left is point P, and the red line segments intersect the curve at right angles. Or as close as I could get with my imperfect human eyes.
if it's parametric then you can differentiate with respect to the parameter to get the velocity (tangent vector), and then check for each parameter the dot product of the tangent vector with P - P'
For instance a circle with P on its "rotational axis" is made up entirely of lucky points. If you move P a bit (off axis), you end up with fewer points (2? maybe more?)
You might get lots of lucky points, which implies the solution is an equation of high degree, and those are generally only solvable via numerical methods.
I think you'll get 2 lucky points from a circle, if P isn't the center. For vaguely the same reason that you can draw two lines tangent to the circle passing through p.
... Except you can't do that if P is inside the circle. Then it has zero tangents and 2 lucky points. Hmm.
I've considered the sliding circle approach for some of my own graphical projects, but I find it inelegant
Especially if you're a vector graphics library, because a sliding circle is so... Raster-y.
Of course, a vector graphics library will necessarily produce rastered output. But you want to keep all the rasterness in the Hot Zone, and only touch it after you've put on your hazmat suit
Indeed. Slightly less raster-y is to draw your curved path multiple times, with its origin tracing out a circle. Like drawing the circle with a curve-shaped brush.
@0x263A Are you familiar with Wildfire by Mandolin Orange (now known as Watchhouse)? It's a beautifully poignant song about the aftermath of the US Civil War, written by a man from North Carolina.
Love Mandolin Orange! Old Ties and Companions is such a great tune. They have quite a knack for songs that land somewhere between melancholy and hopeful.
@0x263A He's a great songwriter, and I love the mood they evoke. Check out this new version, by Watchhouse combined with The Punch Brothers and Sarah Jarosz: youtu.be/lXHya-wkAQU
Is the rule of up/down-vote being locked after a certain time, apply to the OP too? I noticed that I lost 10 rep, which might be the OP removing their vote after almost a month
I have a combobox within a scrollable canvas frame- when I open the combobox and attempt to scroll through the options, the combobox and the entire window both scroll together. It would be nice to pause canvas scrolling while the combobox is open, but unbinding the mousewheel scroll from the comb...
The demo only needs to change the capitalization of Tkinter to be 3.X compatible. here is a version with that change, plus a print statement so you can see what the event object contains.
On my Windows 10 machine, event.num is always "??', and event.delta is always either 120 or -120.
> Only Windows and macOS Aqua typically fire MouseWheel and Shift-MouseWheel events. On X11 vertical scrolling is rather supported through Button-4 and Button-5 events, and horizontal scrolling through Shift-Button-4 and Shift-Button-5 events.
If it's possible to listen for touch events, it's not as easy as the common events listed on that page. I 75% suspect that you can still do it, if you're determined
Probably gotta pump the event queue yourself and look for WM_TOUCHSCREEN_WAS_TOUCHED or similar
Direct Manipulation, which I have never heard of before now, ostensibly lets you define touch behavior for your application without writing fiddly handlers for your various WM_ events. But it requires COM, so it's hard to say with a straight face that it's easier in the long run.
To be honest, handling touch input (beyond simple equivalents to mouse click & drag events) gets complicated no matter how it's organised. Especially if your program has to work both with & without a mouse.
@Kevin I would start by finding a parametric equation for the curve, offsetting so that P is at the origin, and then taking derivatives. it should be possible to work out some equation in terms of x(t), y(t), dx/dt, dy(dt) and then solve for t and then plug it back in
it seems more like a calculus problem than an algebra one to me