how do you find the square root of a number in applescript - applescript

I have a simple calculator code and I want to include square root in it. I am relatively new to applescript and I have no idea how. It might be nooby but thanks anyway!

You can just set the exponent to 0.5.
For example:
set num_ to 25.1
set sqrt_ to num_ ^ 0.5

Related

Need a little help on an equation for a GUI "Slider"

This is a small equation that's giving me a headache, I'm close to solving but- Ugh.
I'll try to be prompt.
I have this:
As you see it is a slider that goes from 0.1x to 3x Difficulty.
I have other sliders like this, for audio for example that just go from 0% to 100%.
That works fine.
However, with a minimum value greater than 0 my math breaks a bit and I'm stuck not being able to
perfectly slide the bar all the way to the bottom because it isn't 0 and it is 0.1 instead.
I want to make it to where even if the minimum value isn't 0, the bar goes all the way to empty.
Here is the relevant equations/calculations at play:
var percent = val/val_max
var adjustment = ((x2-x1)*val_min)-((((x2-x1)*val_min)*percent)*val_min)
var x2_final = (x1+((x2-x1)*percent))-adjustment
percent is the percentage of the current value relative to the max value (0.0 to 1.0)
adjustment is trying to find how much to additionally add/remove from x2_final based on the current value to keep the slider properly scaled when the minimum value isn't 0. (This is where the problem is)
x2_final is the final (in pixels) coordinate where the slider should stop based on the previous calculations.
Initially the slider would over fill when full (that was fixed by the current adjustments) but now the slider doesn't go all the way empty and leaves a "0.1" worth of slider.
I don't usually use forums or stackoverflow as I try to figure out things on my own but so I apologize if my explanation needs work.
Here is what the slider looks like when I set it as low as it will go:
Also, if I have more math related problems, are there any good tools I can use to help simulate my calculations like this so people can run it for them selves or something?
Thanks in advance!
Solved it!
So my equation was a bit wrong for the adjustment.
As now it looks like this:
var percent = val/val_max
var percent_min = val_min/val_max
var adjustment = ((x2-x1)*percent_min)-(((x2-x1)*percent_min)*percent)
var x2_final = (x1+((x2-x1)*percent))-adjustment
And now the slider properly fills to full as well as empties to the bottom,
regardless of what the minimum value is.
But I also noticed then the bar as I was sliding it wasn't following my mouse.
So to fix that I had to go later in my code where I update the current value of the slider as the user clicks and changes it...
var mouse_x_percent = round2((mouse_x-x1+adjustment)/(x2-x1+adjustment),2)
Just had to add the adjustment to both sides of that calculation (getting the mouse_x's percentage relative to the beginning and end of the slider itself which would then be used to calculate the new value by multiplying the mouse_x_percent with the max value).
(Round2() takes two numbers, the first being the number to round, and the second to what decimal place)
Always love solving a problem, I hope this helps someone else.

How to interpolate an NSBezierPath/UIBezierPath and retrieve normal Vector at location?

I have an NSBezierPath. I would like to interpolate along in a way that
interpolate (0) = starting point
interpolate (1) = end point
interpolate (0.5) = middlePoint.
I would like as well a function that provides a normal vector at the point.
I saw a lot of puzzle pieces during my search but nobody offering a full solution in swift. Key problems are:
how can I ensure that interpolate(0.5) is really in the middle of the path? Do I need to consider the length of the overall path? Will it always be the middle point of the middle path segment? I doubt that. However, good approximations are welcome!
how do I retrieve the normal vector on such a point?
Many thanks in advance!
Basically, I used this library: https://github.com/louisdh/bezierpath-length/blob/master/Source/BezierPath%2BLength.swift
It gave me a good approximation of the x that is needed in order to do the interpolation after a small change. With that x and this explanation: https://medium.com/#adrian_cooney/bezier-interpolation-13b68563313a was I able to find the normal vector (the last interpolation is along the tangent, so I only needed the orthogonal vector from that last interpolation).
My code is still a bit messy. I might publish later. But it works!

Get proper absolute rotation after mesh.lookAt

After I call mesh2.lookAt(mesh1.position) mesh2.rotation.y is the same value whether mesh1.position.z is positive or negative, but mesh2.rotation.x and z are either 0 or -PI -- so there actually is some information in the quaternion/matrix.
I tried to manually call update functions for every matrix and getWorldRotation.
What the heck is going on? How to get the absolute rotation?
Thanks in advance
I don't get why yet, but setting the order of the euler (Y first) fixed it.
I'm just rotating around one axis.

Raphael animate based on position not time

I have two path elements. For the sake of description lets just say one is a heart and the other is a square with rounded corners.
I'm trying to get one to interpolate into the other like this:
http://raphaeljs.com/animation.html
This is easy enough to do but I need to do it based off the elements X position and not time.
So as I drag the path element between an Xval of 100 and 200 it will slowly morph into the second path until it finally completes the transformation at an X value of 200.
Does anyone have any idea how I might do this?
I'd use the drag handler (or you could bind to the mousemove event and feed the coordinates in as well) and use that to move between a starting position and a target animation based on the x or y coordinate and using Element.status. Check out the fiddle (based on yours):
And I'm sorry -- I gave you a copy of your own fiddle back originally. Terribly poor form =( Try this one instead:
http://jsfiddle.net/kevindivdbyzero/MqFkP/2/
So far this is what I have come up with:
http://jsfiddle.net/jocose/CkL5F/139/
This works but relies on a delay which I am afraid is going to break on slower machines, and plus it seams really hacky. If anyone knows of a better solution please let me know.

Jigsaw Puzzle, cutting pieces from image

I'm trying to make an application that cuts image into jigsaw puzzles. My problem is that I don't know how to do this (any kind of algorithm). I do want to have male and female endings of puzzles, but not in the same place all the time (like in the middle of puzzle border) so this: https://stackoverflow.com/questions/2755389/how-to-create-jigsaw-image-puzzle-using-c solution doesn't fit me.
Is there any kind of "smart" algorithm to make this happens. I was thinking about using bezier curves, but I don't know how to do this right.
You could use a random offset. You store for each side the kind of interaction (ie male or female), the anchor type (you could use a set of different looking "anchors" (don't know the name), and the offset.
This make for easy check : you must have same anchor, same offset and different kind for the two piece to be able to link.
regards
Guillaume
There is a sketch of how I'd approach it here: How to create jigsaw puzzle from an image using javascript
Randomization is your savior! Won't randomizing the position of the curve help?

Resources