Python Forum
Python Turtle. Pointers of the "arrow" direction of the vector.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Turtle. Pointers of the "arrow" direction of the vector.
#1
Please tell me how to position the arrows in the direction of the vector?


[Image: 244280916_5472eec8d5877641013c65d69a0549a8_800.png]

from math import sin, cos, tan, atan, degrees, pi
from random import randint


import turtle


R = 100 # the radius of the circles
r = 25 # the radius of the elements circles
n = 4 # the number of rows of the matrix
Fi = 2*pi/n # the angle of displacement in radians [1.5707963267948966]
p = [] # the coordinates of the circles on the x & y axis
matrix = [[randint(0,9) for _ in range(n)] for _ in range(n)] # generating a random two-dimensional matrix


win = turtle
win.setup(400,400)
win.title("Dependence of matrix elements")

for i in range(n):
    """Calculating the coordinates of the location of circles."""
    p += [[(R * cos(Fi * i)), (R * sin(Fi * i))]] 


for i in range(n):
    """Drawing circles and numbering them."""
    win.speed(10)
    win.teleport(p[i % n][0], p[i % n][1] - r)
    win.circle(r)
    win.teleport(p[i % n][0], p[i % n][1])
    win.dot(5)
    win.teleport(p[i % n][0], p[i % n][1] + r)
    win.write(i+1)


win.speed(1)
for i in range(n):
    for j in range(n):
        if matrix[i][0] != 0 and matrix[i][j] != 0 and i != j:
            """Finding dependencies of matrix elements"""
            angle = tan((p[i][1] - p[j][1]) / (p[i][0] - p[j][0])) # calculation of the slope angle tangent
            angle = atan(angle) # calculation of the catangence
            angle = degrees(angle) # conversion from meridians to degrees
        
            win.teleport(p[i][0], p[i][1]) # setting the turtle to the beginning of the vector
            win.goto(p[j][0], p[j][1]) # draw a vector

            
            win.setheading(angle + 180) # turn the arrow according to the angle of the vector
            win.stamp() #  print an arrow on the canvas


win.done()
Reply


Messages In This Thread
Python Turtle. Pointers of the "arrow" direction of the vector. - by roomONmoon - Dec-09-2023, 05:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to move in entries using the arrow keys by applying the bind and focus? pymn 4 5,058 Apr-06-2022, 04:29 AM
Last Post: pymn
  [Tkinter] arrow key selection for menu knoxvilles_joker 6 6,102 Apr-19-2021, 12:04 AM
Last Post: knoxvilles_joker
  Turtle / turtle question DPaul 2 2,245 Oct-04-2020, 09:23 AM
Last Post: DPaul
  How to work with pointers in PyQt? AlekseyPython 4 4,141 Feb-07-2019, 04:14 AM
Last Post: AlekseyPython
  Can someone point me in the right direction? Aesgarth 1 2,799 Nov-03-2017, 11:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020