Dec-09-2023, 05:47 PM
(This post was last modified: Dec-09-2023, 05:47 PM by roomONmoon.)
Please tell me how to position the arrows in the direction of the vector?
![[Image: 244280916_5472eec8d5877641013c65d69a0549a8_800.png]](https://otvet.imgsmail.ru/download/244280916_5472eec8d5877641013c65d69a0549a8_800.png)
![[Image: 244280916_5472eec8d5877641013c65d69a0549a8_800.png]](https://otvet.imgsmail.ru/download/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()
