Determining the angle between three points.
Different issue, different thread! I'm trying to calculate the angle between two vectors. Two of the points are constant for each trial, (0,0) and (0,300), and the third point is given by the coordinates of participants' touch responses, which are saved as two separate variables, xpos and ypos. I'm trying to adapt this Python snippet to create a function that calculates the angle.
import numpy as np
def get_angle(p0 = np.array([0.0,0.0]), p1 = np.array([0.0,300.0]), p2 = np.array([var.xpos, var.ypos]):
v0 = np.array(p0) - np.array(p1)
v1 = np.array(p2) - np.array(p1)
angle = np.math.atan2(np.linalg.det([v0,v1]),np.dot(v0,v1))
return np.degrees(angle)
I've placed this is the Prepare section of the inline script in which participants perform their responses. Then, in the Run section, I call on the function:
var.angle = get_angle(p0,p1,p2)
However, if I try to run the script, it complains that name 'p0' is not defined. Should p0 and p1 be defined in the Run section rather than in the Prepare section, even though they never change?
Comments
Hi Fabio,
It looks like you are trying to define the variables inside the defintion of the function: all variables in the function definition are merely placeholders, so when you are asking for the function to evaluate 'p0' it does not exist. What would work is the following:
var.angle = get_angle(np.array([0.0,0.0]),np.array([0.0,300.0]), np.array([var.xpos, var.ypos])Alternatively rewrite you function like so:
def get_angle(p2, p0=np.array([0.0,0.0]), p1 = np.array([0.0,300.0])):and get your angle:
var.angle = get_angle(np.array([var.xpos, var.ypos])This way p0 and p1 are default values and p2 requires input, and as long as you do not give them new values in the function call, it will use these defaults.
Hope this helps, let me know if it doesn't work
(I didn't check the math/code for the angle)
Thanks Roelof! It seems to work now - or, at least, it produces some output. However, the output doesn't seem to make sense in either radians or degrees, so I suspect there's definitely something wrong with my trigonometry. For the record, these are the points, P0 and P1 being static, and P2 being variable. I need that angle between 0 deg and X deg. Apologies, the image was put together horribly quickly in Paint.
Hi Fabio,
I understand the question; but I'm not entirely sure what is happening in your function: do you need numpy arrays or would it be fine if the values are just in tuples? I attached a short opensesame experiment that should explain the basic trigonometry involved in calculating an angle based on an (x,y) input and some arbitrary center.
If you want to implement it in a function you need to know what type of x,y coordinates you are going to collect, are they indeed numpy arrays, or lists, or tuples,
If you have to keep using arrays:
A = np.array([1,2])
A[0]
and the same goes for the y-values.
You could rewrite the code in the opesesame experiment into a function that takes 1 argument with an x- and a y-value (var.xpos, var.ypos). If you have any specific questions regarding the implementation don't hesitate to ask
might also be useful:
https://docs.python.org/2/library/math.html
Hi Roelof,
I'm getting the position of point 2 via a mouse.get_click() function, which I think stores it as a tuple. I then unpack the tuple into the separate xpos and ypos to do other stuff with them in the script, but I could in principle still use it. I'm not married to this particular approach, I just grabbed the first function I found on the internet that was seemingly intended to compute angles because I wasn't sure where to begin.
I've looked at the code you attached, and I think I've figured it out. It looks like I don't need the function at all, and that these lines produce meaningful output out of the variables I have:
That was really helpful, thanks!