How to determine the three-point coordinates of a vector arrow

50 Views Asked by At

i want to draw a vector whenever i drag the object which is a ball. I have drawn a segment, the remaining thing is to draw an arrow indicating the direction of the vector. But I can't determine the three-point coordinates to draw the correct direction the arrow is pointing. please help me. Thank you

try:
    import pygame,ctypes
    import pygame.draw as dr
    from math import *
    pygame.init()

    def interaction(xM,yM,x,y):
        if x<=xM and xM<=x+(10*sqrt(12)) and xM!=x:
            if y<=yM and yM<=y+(10*sqrt(2)) and yM!=y:
                return True
            else:return False
        else:return False

    def angle(x,y):
        if x>0:a=80
        else:a=-80
        return degrees(acos((x*a)/(abs(a)*vec_magnitude(x,y))))
    def vec_magnitude(x,y):
        return abs(sqrt((x**2)+(y**2)))
    length=1380
    width=720
    display=pygame.display.set_mode((length,width))
    #mau
    white=(255,255,255)
    black=(0,0,0)
    green=(0, 153, 51)
    red=(204, 0, 0)
    blue=(0, 51, 204)
    yellow=(255, 204, 0)
    xO,yO=80,620
    x,y=xO,yO-10
    running=True
    acceleration=bool(False)
    while running:
        display.fill(black)
        #vat nem
        da_ball=dr.circle(display,green,(x,y),10)
        pos=pygame.mouse.get_pos()
        #print(pos)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:running=False
            #gia toc bong
            if event.type==pygame.MOUSEBUTTONDOWN and interaction(pos[0],pos[1],x-(5*sqrt(2)),y-(5*sqrt(2))):
                acceleration=True
            elif event.type==pygame.MOUSEBUTTONUP:acceleration=False
        if acceleration:
            dr.line(display,yellow,(x,y),(pos[0],pos[1]),3)
            alp=angle(pos[0]-x,pos[1]-y)
            dx=8*sin(alp)
            dy=8*cos(alp)
            #Draw Vector Here
            if pos[0]>x and pos[1]<=y:
                dr.polygon(display,yellow,((pos[0]-dx,pos[1]-dy),(pos[0]+dx,pos[1]+dy),(pos[0]+dy,pos[1]-dx))) 
        pygame.display.update
    pygame.quit()
except Exception as e:
    ctypes.windll.user32.MessageBoxW(0,str(e),"ErrorBox",16)
1

There are 1 best solutions below

2
Fatemeh On

I noticed a couple of issues in your code:

The pygame.display.update function is missing parentheses. It should be pygame.display.update().

You need to handle the case when pos[0] <= x in your angle calculation to avoid division by zero. You can set a to 0 in this case.