Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

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:

    var.rad_angle = math.atan2((0-var.xpos),(0-var.ypos))
    var.deg_angle = math.degrees(var.rad_angle)*-1
                if 0-(var.xpos) > 0:
                    var.deg_angle = 360-math.degrees(var.rad_angle)
    

    That was really helpful, thanks!

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games